-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.babel.js
executable file
·194 lines (170 loc) · 6.17 KB
/
webpack.config.babel.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
import parameterize from 'parameterize';
import appConfig from './config/appConfig';
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
// const HtmlWebpackExcludeAssetsPlugin = require('html-webpack-exclude-assets-plugin');
// const AssetsPlugin = require('assets-webpack-plugin');
const ChunkManifestPlugin = require('chunk-manifest-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const chalk = require('chalk');
const postCss = require('postcss-cssnext');
const argv = require('yargs')(process.argv);
const localesOverride = (argv.env('env').array('locale').argv.env || { locale: null }).locale;
const IS_PROD = ['prod', 'production'].includes(process.env.NODE_ENV.toLowerCase());
const IS_DEV = !IS_PROD;
const RGXP_VENDOR_LOCALES = /(?:moment[\\/]locale)|(?:intl[\\/]locale-data(?:[\\/]jsonp)?)$/;
const APP_MAIN_CHUNK_NAME = parameterize(appConfig.appName);
const locales = (Array.isArray(localesOverride) ? localesOverride : appConfig.locales);
if (Array.isArray(localesOverride)) {
// eslint-disable-next-line
console.warn(
chalk.bold.bgRed.white(
' ⚠ -- CAUTION `--env.locale` Flags are solely meant for testing purposes ',
),
);
// eslint-disable-next-line
console.info(
chalk.blue(`Building as if appConfig would set locales to "${locales.join('", "')}"`),
);
}
/**
* Determine which locales moment.js should actually load
*/
let localePatterns = locales.map(
// Regex matching 'en' or 'en-US' and 'de' or 'de-DE' -- part after the '-' is optional
locale => locale.toLowerCase().replace(/-([a-z]+)$/, '(?:-$1)?'),
);
localePatterns = localePatterns.map(pattern => `(?:${pattern})`);
const REGEX_DESIRED_LANGUAGES = new RegExp(`^\\./(${localePatterns.join('|')})$`, 'i');
// eslint-disable-next-line
console.info(
`
ContextReplacementPlugin is configured to limit moment.js locales matching
RegExp(${REGEX_DESIRED_LANGUAGES})
`,
);
const mainEntrypoint = [
IS_DEV ? 'react-hot-loader/patch' : null,
// activate HMR for React
// IS_DEV ? 'webpack-hot-middleware/client?http://localhost:8080' : null,
// obsolete ? see http://stackoverflow.com/questions/41342144/webpack-hmr-webpack-hmr-404-not-found#answer-41483088
// // bundle the client for webpack-dev-server
// // and connect to the provided endpoint
IS_DEV ? 'webpack/hot/only-dev-server' : null,
// bundle the client for hot reloading
// only- means to only hot reload for successful updates
'./src/index',
// finally, the main entry point,,
].filter(file => !!file); // get rid of `null` entries, keep truthies
let stylesLoader = [
'style-loader',
'css-loader',
{ loader: 'postcss-loader', options: { plugins: () => [postCss] } },
'sass-loader',
];
// if production wrap styles in ExtractTextPlugin
if (IS_PROD) {
const fallbackLoader = stylesLoader.shift();
const loader = stylesLoader;
stylesLoader = ExtractTextPlugin.extract({ fallbackLoader, loader });
}
const config = {
name: 'client',
entry: {
'libs-react': [
'react',
'react-dom',
'react-router',
],
'libs-moment': [
'moment',
'moment-timezone',
],
'libs-util': [
'humps',
],
[APP_MAIN_CHUNK_NAME]: mainEntrypoint,
},
output: {
path: path.join(__dirname, './static'),
filename: IS_PROD ? '[name]-[chunkhash:6].js' : '[name].[id].js',
publicPath: '',
libraryTarget: 'this',
library: '__init__',
devtoolModuleFilenameTemplate: '/[resource-path]',
},
devServer: {
hot: true,
// enable HMR on the server
contentBase: path.join(__dirname, 'static'),
// match the output path
publicPath: '/',
// match the output `publicPath`
// compress: true,
// enable gzip
// clientLogLevel: 'info' // none, error, warning or info,,
},
resolve: { extensions: ['.js', '.jsx'] },
module: {
rules: [
{ test: /\.jsx?$/, loader: 'babel-loader' },
{ test: require.resolve('react'), loader: 'expose-loader?React' },
{ test: /\.s?css$/, loader: stylesLoader },
{ test: /\.(jpg|gif|png|eot|otf|woff2?|ttf)$/, loader: 'file-loader' },
{
test: /\.svg/,
use: [
{ loader: 'svg-url-loader', options: { dataUrlLimit: 1024 } },
{
loader: 'svgo-loader',
options: {
plugins: [
{ removeTitle: true },
{ convertColors: { shorthex: false } },
{ convertPathData: false },
],
},
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html',
inject: 'body',
filename: 'index.html',
}),
IS_DEV ? new webpack.HotModuleReplacementPlugin() : null,
// enable HMR globally
IS_PROD ? new webpack.optimize.AggressiveMergingPlugin() : null,
new webpack.ContextReplacementPlugin(RGXP_VENDOR_LOCALES, REGEX_DESIRED_LANGUAGES),
new ChunkManifestPlugin({ filename: 'manifest.json', manifestVariable: 'webpackManifest' }),
new webpack.optimize.CommonsChunkPlugin({
names: ['libs-react', 'libs-moment', 'libs-util'],
minChunks: Infinity,
}),
new webpack.optimize.CommonsChunkPlugin('manifest'),
new WebpackMd5Hash(),
new ExtractTextPlugin((IS_PROD ? '[name]-[hash:6].css' : '[name].css')),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) }),
new webpack.DefinePlugin({
appConfig: JSON.stringify(appConfig),
// localeMap: JSON.stringify(localeMap),
'process.env': JSON.stringify({ IS_DEV, IS_PROD }),
}),
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: 'defer',
}),
// Need this plugin for deterministic hashing
// until this issue is resolved: https://github.com/webpack/webpack/issues/1315
// for more info: https://webpack.js.org/how-to/cache/
new WebpackMd5Hash(),
].filter(plugin => !!plugin), // drop *null* entries
devtool: IS_PROD ? 'source-maps' : 'cheap-module-eval-source-map',
};
module.exports = config;