-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
226 lines (212 loc) · 6.37 KB
/
webpack.config.js
File metadata and controls
226 lines (212 loc) · 6.37 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const path = require('path');
const glob = require('glob-all');
const chalk = require('chalk');
const webpack = require('webpack');
const webpackTool = require('webpack-tool');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const WebpackChunkHash = require('webpack-chunk-hash');
const nodeExternals = require('webpack-node-externals');
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
const ZipPlugin = require('zip-webpack-plugin');
const HotModuleReplacementPlugin = webpack.HotModuleReplacementPlugin;
const WatchIgnorePlugin = webpack.WatchIgnorePlugin;
const UglifyJSPlugin = webpack.optimize.UglifyJsPlugin;
const CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;
const getWebpackConfig = options => {
options = options || {};
const isServer = options.target === 'node';
const isProduction = options.env === 'production';
const output = isServer ? './server' : './dist';
let config = {
entry: {
include: [
'./src/page/**/index.js',
'!./src/page/**/component/**',
],
},
output: {
// output dir
path: path.join(__dirname, output),
// js output path
filename: `js/[name]${isProduction && !isServer ? '.[chunkhash:8]' : ''}.js`,
publicPath: isProduction ? '//127.0.0.1:3000/' : '/',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
'babel-loader',
],
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
interpolate: 1, // supported html `${}` sytax
attrs: [ ':src' ],
},
},
],
},
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: isProduction && !isServer,
},
},
].concat(isServer ? [] : [
'autoprefixer-loader',
'postcss-loader',
'sass-loader',
]),
}),
},
{
test: /\.(png|svg|jpg|gif)$/,
use: {
loader: 'url-loader',
options: {
limit: 2048,
fallback: 'file-loader',
// img output path
name: 'img/[name].[hash:8].[ext]',
},
},
},
],
},
plugins: [
new CleanWebpackPlugin([ output ]),
// postcss-sprites will create tmp images to dist/xx
new WatchIgnorePlugin([ path.join(__dirname, 'dist') ]),
// js use content md5
new WebpackChunkHash(),
new webpack.EnvironmentPlugin({
NODE_ENV: 'development',
DEBUG: false,
}),
new webpack.ProgressPlugin((percentage, msg, modules) => {
const stream = process.stderr;
if (stream.isTTY && percentage < 0.71) {
stream.cursorTo(0);
stream.write(chalk.magenta(`📦 ${(modules || '').split(' ')[0]} ${msg} `));
stream.clearLine(1);
}
}),
new HtmlWebpackInlineSourcePlugin(),
// css output path
new ExtractTextPlugin(`css/[name]${isProduction ? '.[contenthash:8]' : ''}.css`),
!isServer && new CommonsChunkPlugin({
// common chunk name
name: 'common',
}),
isProduction && !isServer && new UglifyJSPlugin(),
isProduction && !isServer && new HtmlWebpackExternalsPlugin({
externals: [
{
module: 'react',
global: 'React',
},
{
module: 'react-dom',
global: 'ReactDOM',
},
],
}),
isProduction && !isServer && new ZipPlugin({
path: 'zip',
filename: 'offline.zip',
}),
].filter(item => item),
};
const setEntry = () => {
const entry = {};
glob.sync(config.entry.include).forEach(item => {
const name = item.replace(/^(\.?[\/\\])?src[\/\\]page[\/\\]/, '').replace(/[\\\/][^\/\\]*$/, '');
if (isServer || isProduction) {
entry[name] = [ item ];
} else {
entry[name] = [ 'webpack-hot-middleware/client', item ];
}
});
config.entry = entry;
};
const setHTMLPlugin = () => {
let commonsChunk = [];
for (let i = 0; i < config.plugins.length; i++) {
const plugin = config.plugins[i];
if (plugin instanceof CommonsChunkPlugin) {
commonsChunk = commonsChunk.concat(plugin.chunkNames);
}
}
Object.keys(config.entry).forEach(chunk => {
const entries = config.entry[chunk];
const entry = entries instanceof Array ? entries[entries.length - 1] : entries;
config.plugins.push(new HtmlWebpackPlugin({
inlineSource: isProduction ? '\\.css$' : undefined,
// html output path
filename: `html/${chunk}.html`,
// template path
template: entry.replace(/\.js$/, '.html'),
chunks: commonsChunk.concat([ chunk ]),
minify: isProduction ? {
minifyJS: true,
minifyCSS: true,
collapseWhitespace: true,
preserveLineBreaks: true,
} : false,
}));
});
};
const setTarget = () => {
if (isServer) {
config = webpackTool.merge(config, {
target: 'node',
externals: [ nodeExternals() ],
output: {
libraryTarget: 'commonjs2',
},
});
}
};
const setDevTool = () => {
if (!isProduction) {
config.devtool = 'eval';
if (!isServer) {
config.devServer = {
contentBase: output,
hot: true,
};
config.plugins.push(new HotModuleReplacementPlugin());
}
}
};
setEntry();
setHTMLPlugin();
setTarget();
setDevTool();
return config;
};
module.exports = [
getWebpackConfig({
env: process.env.NODE_ENV,
}),
getWebpackConfig({
env: process.env.NODE_ENV,
target: 'node',
}),
];