-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwebpack.config.js
115 lines (99 loc) · 3.12 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
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
const path = require('path');
const webpack = require('webpack');
const HtmlPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const BabiliPlugin = require('babel-minify-webpack-plugin');
const WorkboxPlugin = require('workbox-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const secureFilters = require('secure-filters');
const config = require('./src/config');
const publicDir = config.get('paths.public');
const clientSrcDir = config.get('paths.clientSrc');
const assetsDir = path.join(clientSrcDir, 'assets');
const ENV_PROD = 'production';
const ENV_DEV = 'development';
const ENV_ANALYSIS = 'analysis';
module.exports = function(env = process.env.NODE_ENV){
let cfg = {
devtool: false,
plugins: [],
entry: {
bundle: path.join(__dirname, clientSrcDir, 'index.js'),
styles: path.join(__dirname, clientSrcDir, '/styles/index.scss')
},
output: {
filename: '[chunkhash]-[name].js',
path: path.join(__dirname, publicDir)
},
module: {
rules: [{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
use: [
{ loader: 'css-loader' },
{ loader: 'sass-loader' }
]
})
}]
}
};
if (env === ENV_PROD || env === ENV_ANALYSIS){
cfg.plugins.push(new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify('production') }
}));
cfg.plugins.push(new BabiliPlugin());
if (env === ENV_ANALYSIS){
cfg.plugins.push(new BundleAnalyzerPlugin({
analyzerMode: 'server',
analyzerPort: 8888,
openAnalyzer: true,
generateStatsFile: true,
statsFilename: 'stats.json'
}));
}
} else {
cfg.devtool = 'inline-source-map';
}
cfg.plugins.push(new CopyPlugin([
assetsDir,
{ from: require.resolve('workbox-sw'), to: 'workbox-sw.prod.js' }
]));
cfg.plugins.push(new ExtractTextPlugin({ filename: '[chunkhash]-[name].css' }));
cfg.plugins.push(new HtmlPlugin({
template: path.join(assetsDir, 'index.html'),
excludeJS: ['styles.js'], // TODO (mirande): https://github.com/webpack/webpack/issues/1967
clientConfigs: getClientConfigs(config),
inject: false,
hash: false
}));
cfg.plugins.push(new WorkboxPlugin({
globDirectory: path.join(__dirname, publicDir),
globPatterns: ['**/*.{html,js,css,ico}'],
globIgnores: ['*styles.js'], // TODO (mirande): https://github.com/webpack/webpack/issues/1967
swSrc: path.join(clientSrcDir, 'service-worker.js'),
swDest: path.join(publicDir, 'sw.js')
}));
return cfg;
};
module.exports.get = function(env){
return module.exports(env);
};
module.exports.getForProduction = function(){
return module.exports(ENV_PROD);
};
module.exports.getForAnalysis = function(){
return module.exports(ENV_ANALYSIS);
};
module.exports.getForDevelopment = function(){
return module.exports(ENV_DEV);
};
// INTERNAL UTILS /////////////////////////////////////////////////////////////
function getClientConfigs(cfg){
return secureFilters.jsObj({
clientId: cfg.get('client.id'),
apiUrl: cfg.get('api.url'),
repoUrl: cfg.get('urls.repo'),
appUrl: cfg.get('urls.app')
});
}