-
Notifications
You must be signed in to change notification settings - Fork 48
/
createConfig.js
97 lines (87 loc) 路 2.14 KB
/
createConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const path = require('path')
const clone = require('clone')
const webpack = require('webpack')
const cwd = process.cwd()
const baseConfig = {
output: {
filename: '[name].js'
},
mode: 'development',
target: 'web',
performance: { hints: false },
devtool: 'inline-cheap-module-source-map',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
sourceType: 'unambiguous',
plugins: [
require.resolve('@babel/plugin-syntax-object-rest-spread'),
require.resolve('@babel/plugin-proposal-class-properties'),
require.resolve('@babel/plugin-transform-runtime'),
],
presets: [
require.resolve('@babel/preset-env'),
require.resolve('@babel/preset-react')
]
}
}
]
}
]
},
resolve: {
alias: {
'@': process.cwd()
}
},
plugins: []
}
module.exports = function createConfig (conf, watch) {
const wc = clone(baseConfig)
wc.entry = {
[path.basename(conf.in, '.js')]: path.resolve(cwd, conf.in)
}
/**
* merge output as an object,
* or resolve a simple path
*/
wc.output = Object.assign(
wc.output,
typeof conf.out === 'object' ? conf.out : {
path: path.resolve(cwd, conf.out)
}
)
wc.resolve.alias = Object.assign(wc.resolve.alias, conf.alias || {})
wc.plugins = wc.plugins.concat([
new webpack.DefinePlugin(conf.env || {}),
conf.banner && new webpack.BannerPlugin({
banner: conf.banner,
raw: true,
entryOnly: true,
exclude: /\.(sa|sc|c)ss$/
})
].filter(Boolean))
;[].concat(conf.presets || [])
.map(p => {
const props = {
config: wc,
watch
}
try {
typeof p === 'function' ? p(props) : (
Array.isArray(p) ? (
require(`../presets/${p[0]}.js`)(p[1])(props)
) : (
require(`../presets/${p}.js`)()(props)
)
)
} catch (e) {}
})
return wc
}