-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
153 lines (141 loc) · 4.45 KB
/
webpack.config.js
File metadata and controls
153 lines (141 loc) · 4.45 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const OpenBrowserPlugin = require('open-browser-webpack-plugin')
const merge = require('webpack-merge')
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
function parseNpmLifecycleEvent(event) {
if (typeof event !== 'string') {
throw TypeError('you are not passing the npm_lifecycle_event, got: ' + event.toString())
}
const op = event.split(':')[0]
const target = event.split(':')[1]
return {
op,
target
}
}
const npmEvent = parseNpmLifecycleEvent(process.env.npm_lifecycle_event)
const production = process.env.NODE_ENV === 'production';
if (!npmEvent.target) {
throw new Error(
'pass a target right after `npm start` or `npm run build` cmd.\n' +
'expect: npm run start:target, actual: ' + npmEvent.op
)
}
const PATH = {
root: path.join(__dirname, 'pages'),
base: path.join(__dirname, 'dist'),
src: path.join(__dirname, `pages/${npmEvent.target}`),
dist: path.join(__dirname, `dist/${npmEvent.target}`)
}
const common = {
entry: {
app: PATH.src,
},
output: {
path: PATH.dist,
filename: '[name].js',
publicPath: production ? `http://resource.alphagif.com/dist/${npmEvent.target}` : '',
chunkFilename: '[name].js'
},
resolve: {
extensions: ['.js', '.jsx', '.sass'],
alias: {
"@components": path.resolve(__dirname, `pages/${npmEvent.target}/components`),
"@utils": path.resolve(__dirname, `pages/${npmEvent.target}/utils`),
"@publicComponents": path.resolve(__dirname, `pages/demo/components`)
}
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
}),
new HtmlWebpackPlugin({
template: path.join(PATH.src, 'assets/tpl.html'),
filename: `${npmEvent.target}.html`,
hash: true,
chunks: ['app', 'commons']
}),
],
module: {
rules: [{
test: /\.scss/,
use: [MiniCssExtractPlugin.loader, {
loader: 'css-loader',
options: {
minimize: true, //css压缩
importLoaders: 1
}
}, { loader: "sass-loader" }]
},
{
test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif|mp4|webm)(\?\S*)?$/,
include: PATH.root,
loader: 'url-loader?limit=8192'
},
{
test: /\.(js|jsx)$/,
include: PATH.root,
loader: 'babel-loader'
}
]
}
}
if (npmEvent.op === 'start') {
module.exports = merge(common, {
devServer: {
hot: true,
inline: true,
stats: 'errors-only',
contentBase: PATH.base
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new OpenBrowserPlugin({
url: `http://localhost:8080/${npmEvent.target}.html`
})
],
devtool: 'source-map'
})
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"development"'
})
}
if (npmEvent.op === 'build') {
module.exports = merge(common, {
optimization: {
splitChunks: {
chunks: 'all',
minSize: 30000,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
automaticNameDelimiter: '-',
name: true,
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'commons',
priority: -10,
chunks: 'all'
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
}
},
}
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
new BundleAnalyzerPlugin()
]
})
}