-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
82 lines (70 loc) · 1.67 KB
/
webpack.config.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
const path = require('path')
const webpack = require('webpack')
const merge = require('webpack-merge')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const fs = require('fs')
const arrToObj = (arr, setKey, setValue) => (
arr.reduce((acc, filename) => Object.assign(acc, { [setKey(filename)]: setValue(filename) }), {})
)
const dirs = fs.readdirSync(path.resolve(__dirname, './src'))
const filesJs = dirs.filter(file => path.parse(file).ext === '.js')
//path.basename
const entrys = arrToObj(
filesJs,
filename => path.basename(filename, '.bs.js'),
filename => `./src/${filename}`
)
const common = {
entry: entrys,
context: __dirname,
output: {
path: path.resolve(__dirname, './public/'),
filename: '[name].js',
library: '[name]',
},
resolve: {
extensions: ['.js'],
},
module: {
rules: [
{
test: /\.html$/,
loader: 'file-loader',
include: path.resolve(__dirname, './src'),
options: {
context: path.resolve(__dirname, './src'),
name: '[path][name].[ext]',
},
},
]
},
}
const dev = {
devtool: 'cheap-module-eval-source-map',
watch: true,
devServer: {
contentBase: path.join(__dirname, 'public'),
publicPath: '/',
compress: true,
watchContentBase: true,
host: '0.0.0.0',
port: 9000,
historyApiFallback: {
from: '/',
to: 'main/index.html'
}
}
}
const prod = {
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}),
new UglifyJsPlugin(),
],
}
module.exports = (env) => merge(common, ({
'production': prod
}[env && env.NODE_ENV]) || dev)