-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.prod.js
executable file
·308 lines (273 loc) · 9.58 KB
/
webpack.prod.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'use strict';
// Helper: root(), and rootDir() are defined at the bottom
var path = require('path');
var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
// Webpack Plugins
var HtmlWebpackPlugin = require('html-webpack-plugin');
var autoprefixer = require('autoprefixer');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var CopyWebpackPlugin = require('copy-webpack-plugin');
var hotMiddleWarePort = 3001;
var DEFAULT_TARGET = 'app';
var target = process.env.TARGET || DEFAULT_TARGET;
// --optimize-dedupe not needed https://webpack.js.org/guides/migrating/#dedupeplugin-has-been-removed
// --optimize-occurence-order is on by default
// https://webpack.js.org/guides/migrating/#occurrenceorderplugin-is-now-on-by-default
/**
* Env
* Get npm lifecycle event to identify the environment
*/
var ENV = process.env.npm_lifecycle_event;
// Helper functions
const root = function(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
};
const prepend = function(extensions, args) {
args = args || [];
if (!Array.isArray(args)) {
args = [args];
}
return extensions.reduce(function(memo, val) {
return memo.concat(val, args.map(function(prefix) {
return prefix + val;
}));
}, []); // webpack2 doesn't like the first entry being empty
};
const baseConfig = () => {
const config = {};
ENV = 'prebuild';
var isTestEnv = ENV === 'test' || ENV === 'test-watch';
/**
* Devtool
* Reference: http://webpack.github.io/docs/configuration.html#devtool
* Type of sourcemap to use per build type
*/
if (isTestEnv) {
config.devtool = 'inline-eval-source-map';
} else if (ENV === 'prebuild') {
config.devtool = false;
} else {
config.devtool = 'inline-eval-source-map';
}
config.target = 'web';
/**
* Entry
* Reference: http://webpack.github.io/docs/configuration.html#entry
*/
config.entry = isTestEnv || ENV === 'prebuild' ? {
app: ['./src/client/index.js'],
vendor: ['./src/client/vendor.js']
} : [
'webpack-dev-server/client?http://localhost:' + hotMiddleWarePort,
'./src/client/vendor.js',
'./src/client/index.js'
];
config.output = {
path: root('dist'),
publicPath: '/',
filename: 'js/[name].js',
chunkFilename: '[id].chunk.js'
};
config.resolve = {
unsafeCache: !isTestEnv,
modules: [
root(),
'node_modules'
],
// only discover files that have those extensions
// ensure .async.js etc also works
extensions: prepend(['.jsx', '.js', '.json', '.css', '.scss', '.html'], '.async'),
alias: {
app: 'src/app',
common: 'src/common',
'common-styles': path.resolve(__dirname, 'node_modules/tools-common-styles/lib')
}
};
config.module = {
exprContextCritical: false,
rules: [
{
test: /\.less$/,
use: [{loader: 'style-loader'},
{loader: 'css-loader'},
{loader: 'postcss-loader'},
{loader: 'less-loader'}]
},
{
// I combined the two, because why not?
test: /\.(css|scss)$/,
use: [{loader: 'style-loader'},
{loader: 'css-loader'},
{loader: 'postcss-loader'},
{
loader: 'sass-loader',
options: {},
}]
},
isTestEnv ? {} : {
enforce: 'pre',
test: /\.jsx?$/,
loader: 'eslint-loader',
exclude: /(node_modules)/,
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
'react-hot-loader/webpack',
{
loader: 'babel-loader',
options: {
presets: [
'es2015',
'stage-2',
'react'
]
}
}],
include: path.join(__dirname, 'src')
},
{
test: /\.(woff|woff2|ttf|eot|svg)(\?v=[a-z0-9]\.[a-z0-9]\.[a-z0-9])?$/,
loader: 'url-loader?limit=100000'
},
{
test: /\.(png|jpg|jpeg|gif)$/,
loader: 'url-loader?limit=8192'
}
],
// noParse: ['filetoignore.js']
};
config.plugins = [
new webpack.DefinePlugin({
// Environment helpers
'process.env': {
ENV: JSON.stringify(ENV),
NODE_ENV: JSON.stringify('production'),
}
}),
// new webpack.DefinePlugin({
// "process.env": {
// NODE_ENV: JSON.stringify("production")
// }
// }),
// new webpack.optimize.DedupePlugin(), //dedupe similar code
new webpack.optimize.UglifyJsPlugin(), //minify everything
// new webpack.optimize.AggressiveMergingPlugin(), //Merge chunks
new webpack.SourceMapDevToolPlugin({
filename: '[file].js.map'
}),
new HtmlWebpackPlugin({
template: './public/index.html',
inject: 'body',
title: 'App - ' + target
}),
new webpack.LoaderOptionsPlugin({
debug: ENV !== 'prebuild' || !isTestEnv,
options: {
context: '/',
postcss: [
autoprefixer({
browsers: ['last 2 versions']
})
]
}
})
];
if (!isTestEnv) {
config.plugins.push(
// Generate common chunks if necessary
// Reference: https://webpack.github.io/docs/code-splitting.html
// Reference: https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
filename: 'js/[name].js',
minChunks: Infinity
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: 'js/[name].js',
minChunks: 2,
chunks: ['app', 'vendor']
}),
// Inject script and link tags into html files
// Reference: https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
template: './public/index.html',
inject: 'body',
title: 'App - ' + target,
chunksSortMode: function compare(a, b) {
// common always first
if (a.names[0] === 'common') {
return -1;
}
// app always last
if (a.names[0] === 'app') {
return 1;
}
// vendor before app
if (a.names[0] === 'vendor' && b.names[0] === 'app') {
return -1;
} else {
return 1;
}
// a must be equal to b
/* eslint-disable no-unreachable */
return 0;
/* eslint-disable no-unreachable */
}
}),
new webpack.DefinePlugin({
__WEBPACK__: true, // say we're the webpack
__DEV__: process.env.BUILD_DEV // dev environment indication
}),
// Extract css files
// Reference: https://github.com/webpack/extract-text-webpack-plugin
// Disabled when in test mode or not in build mode
new ExtractTextPlugin({filename: 'css/[name].[hash].css', disable: ENV !== 'prebuild'})
);
}
config.stats = {
warnings: false
};
// config.devServer = {
// port: hotMiddleWarePort,
// contentBase: './public',
// historyApiFallback: true,
// stats: 'minimal', // none (or false), errors-only, minimal, normal (or true) and verbose
// headers: {
// "Access-Control-Allow-Origin": "*",
// "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
// "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
// },
// proxy: [
// {
// path: '/assets/common-styles.css',
// target: `http://localhost:3001`,
// secure: false,
// //changeOrigin: true
// },
// {
// path: '/api/login',
// pathRewrite: {'^/api/login': '/api/login/v2'},
// target: `http://localhost:3002`,
// secure: false,
// //changeOrigin: true
// }
// ]
// };
return config;
};
const targets = ['web' /*, 'webworker', 'node', 'async-node', 'node-webkit', 'electron-main'*/].map((target) => {
const base = webpackMerge(baseConfig(), {
target: target,
output: {
path: path.resolve(__dirname, 'dist/' + target),
filename: '[name].' + target + '.js'
}
});
return base;
});
module.exports = targets;