forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
309 lines (290 loc) · 9.4 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
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
309
/** @format */
/**
**** WARNING: No ES6 modules here. Not transpiled! ****
*/
/**
* External dependencies
*/
const _ = require( 'lodash' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const fs = require( 'fs' );
const HappyPack = require( 'happypack' );
const HardSourceWebpackPlugin = require( 'hard-source-webpack-plugin' );
const path = require( 'path' );
const webpack = require( 'webpack' );
const NameAllModulesPlugin = require( 'name-all-modules-plugin' );
const AssetsPlugin = require( 'assets-webpack-plugin' );
const UglifyJsPlugin = require( 'uglifyjs-webpack-plugin' );
const prism = require( 'prismjs' );
/**
* Internal dependencies
*/
const cacheIdentifier = require( './server/bundler/babel/babel-loader-cache-identifier' );
const config = require( './server/config' );
/**
* Internal variables
*/
const calypsoEnv = config( 'env_id' );
const bundleEnv = config( 'env' );
const isDevelopment = bundleEnv === 'development';
const shouldMinify = process.env.hasOwnProperty( 'MINIFY_JS' )
? process.env.MINIFY_JS === 'true'
: ! isDevelopment;
// load in the babel config from babelrc and disable commonjs transform
// this enables static analysis from webpack including treeshaking
// also disable add-module-exports. TODO: remove add-module-exports from babelrc. requires fixing tests
const babelConfig = JSON.parse( fs.readFileSync( './.babelrc', { encoding: 'utf8' } ) );
const babelPresetEnv = _.find( babelConfig.presets, preset => preset[ 0 ] === 'env' );
babelPresetEnv[ 1 ].modules = false;
_.remove( babelConfig.plugins, elem => elem === 'add-module-exports' );
// remove the babel-lodash-es plugin from env.test -- it's needed only for Jest tests.
// The Webpack-using NODE_ENV=test build doesn't need it, as there is a special loader for that.
_.remove( babelConfig.env.test.plugins, elem => /babel-lodash-es/.test( elem ) );
/**
* This function scans the /client/extensions directory in order to generate a map that looks like this:
* {
* sensei: 'absolute/path/to/wp-calypso/client/extensions/sensei',
* woocommerce: 'absolute/path/to/wp-calypso/client/extensions/woocommerce',
* ....
* }
*
* Providing webpack with these aliases instead of telling it to scan client/extensions for every
* module resolution speeds up builds significantly.
*/
function getAliasesForExtensions() {
const extensionsDirectory = path.join( __dirname, 'client', 'extensions' );
const extensionsNames = fs
.readdirSync( extensionsDirectory )
.filter( filename => filename.indexOf( '.' ) === -1 ); // heuristic for finding directories
const aliasesMap = {};
extensionsNames.forEach(
extensionName =>
( aliasesMap[ extensionName ] = path.join( extensionsDirectory, extensionName ) )
);
return aliasesMap;
}
const babelLoader = {
loader: 'babel-loader',
options: Object.assign( {}, babelConfig, {
babelrc: false,
cacheDirectory: path.join( __dirname, 'build', '.babel-client-cache' ),
cacheIdentifier: cacheIdentifier,
plugins: [
...babelConfig.plugins,
[
path.join(
__dirname,
'server',
'bundler',
'babel',
'babel-plugin-transform-wpcalypso-async'
),
{ async: config.isEnabled( 'code-splitting' ) },
],
'inline-imports.js',
],
} ),
};
const webpackConfig = {
bail: ! isDevelopment,
entry: {},
devtool: isDevelopment ? '#eval' : process.env.SOURCEMAP || false, // in production builds you can specify a source-map via env var
output: {
path: path.join( __dirname, 'public' ),
publicPath: '/calypso/',
filename: '[name].[chunkhash].min.js', // prefer the chunkhash, which depends on the chunk, not the entire build
chunkFilename: '[name].[chunkhash].min.js', // ditto
devtoolModuleFilenameTemplate: 'app:///[resource-path]',
},
module: {
// avoids this warning:
// https://github.com/localForage/localForage/issues/577
noParse: /[\/\\]node_modules[\/\\]localforage[\/\\]dist[\/\\]localforage\.js$/,
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules[\/\\](?!notifications-panel)/,
loader: [ 'happypack/loader' ],
},
{
test: /node_modules[\/\\](redux-form|react-redux)[\/\\]es/,
loader: 'babel-loader',
options: {
babelrc: false,
plugins: [ path.join( __dirname, 'server', 'bundler', 'babel', 'babel-lodash-es' ) ],
},
},
{
test: /extensions[\/\\]index/,
exclude: path.join( __dirname, 'node_modules' ),
loader: path.join( __dirname, 'server', 'bundler', 'extensions-loader' ),
},
{
include: path.join( __dirname, 'client/sections.js' ),
loader: path.join( __dirname, 'server', 'bundler', 'sections-loader' ),
},
{
test: /\.html$/,
loader: 'html-loader',
},
{
include: require.resolve( 'tinymce/tinymce' ),
use: 'exports-loader?window=tinymce',
},
{
test: /node_modules[\/\\]tinymce/,
use: 'imports-loader?this=>window',
},
{
test: /README\.md$/,
use: [
{ loader: 'html-loader' },
{
loader: 'markdown-loader',
options: {
sanitize: true,
highlight: function( code, language ) {
const syntax = prism.languages[ language ];
return syntax ? prism.highlight( code, syntax ) : code;
},
},
},
],
},
],
},
resolve: {
extensions: [ '.json', '.js', '.jsx' ],
modules: [ path.join( __dirname, 'client' ), 'node_modules' ],
alias: Object.assign(
{
'react-virtualized': 'react-virtualized/dist/commonjs',
'social-logos/example': 'social-logos/build/example',
},
getAliasesForExtensions()
),
},
node: {
console: false,
process: true,
global: true,
Buffer: true,
__filename: 'mock',
__dirname: 'mock',
fs: 'empty',
crypto: false,
stream: false,
},
plugins: _.compact( [
new webpack.DefinePlugin( {
'process.env.NODE_ENV': JSON.stringify( bundleEnv ),
PROJECT_NAME: JSON.stringify( config( 'project' ) ),
} ),
new webpack.IgnorePlugin( /^props$/ ),
new CopyWebpackPlugin( [
{ from: 'node_modules/flag-icon-css/flags/4x3', to: 'images/flags' },
] ),
new HappyPack( {
loaders: _.compact( [
isDevelopment && config.isEnabled( 'webpack/hot-loader' ) && 'react-hot-loader',
babelLoader,
] ),
} ),
new webpack.NamedModulesPlugin(),
new webpack.NamedChunksPlugin( chunk => {
if ( chunk.name ) {
return chunk.name;
}
return chunk.modules.map( m => path.relative( m.context, m.request ) ).join( '_' );
} ),
new NameAllModulesPlugin(),
new AssetsPlugin( {
filename: 'assets.json',
path: path.join( __dirname, 'server', 'bundler' ),
} ),
process.env.NODE_ENV === 'production' && new webpack.optimize.ModuleConcatenationPlugin(),
] ),
externals: [ 'electron' ],
};
if ( calypsoEnv === 'desktop' ) {
// no chunks or dll here, just one big file for the desktop app
webpackConfig.output.filename = '[name].js';
} else {
// vendor chunk
webpackConfig.entry.vendor = [
'classnames',
'create-react-class',
'gridicons',
'i18n-calypso',
'immutable',
'lodash',
'moment',
'page',
'prop-types',
'react',
'react-dom',
'react-redux',
'redux',
'redux-thunk',
'social-logos',
'store',
'wpcom',
];
// for details on what the manifest is, see: https://webpack.js.org/guides/caching/
// tldr: webpack maintains a mapping from chunk ids --> filenames. whenever a filename changes
// then the mapping changes. By providing a non-existing chunkname to CommonsChunkPlugin,
// it extracts the "runtime" so that the frequently changing mapping doesn't break caching of the entry chunks
// NOTE: order matters. vendor must be before manifest.
webpackConfig.plugins = webpackConfig.plugins.concat( [
new webpack.optimize.CommonsChunkPlugin( { name: 'vendor', minChunks: Infinity } ),
new webpack.optimize.CommonsChunkPlugin( {
async: 'tinymce',
minChunks: ( { resource } ) => resource && /node_modules[\/\\]tinymce/.test( resource ),
} ),
new webpack.optimize.CommonsChunkPlugin( { name: 'manifest' } ),
] );
// jquery is only needed in the build for the desktop app
// see electron bug: https://github.com/atom/electron/issues/254
webpackConfig.externals.push( 'jquery' );
}
if ( isDevelopment ) {
// we should not use chunkhash in development: https://github.com/webpack/webpack-dev-server/issues/377#issuecomment-241258405
// also we don't minify so dont name them .min.js
webpackConfig.output.filename = '[name].js';
webpackConfig.output.chunkFilename = '[name].js';
webpackConfig.plugins = webpackConfig.plugins.concat( [
new webpack.HotModuleReplacementPlugin(),
new webpack.LoaderOptionsPlugin( { debug: true } ),
] );
webpackConfig.entry.build = [
'webpack-hot-middleware/client',
path.join( __dirname, 'client', 'boot', 'app' ),
];
webpackConfig.devServer = { hot: true, inline: true };
} else {
webpackConfig.entry.build = path.join( __dirname, 'client', 'boot', 'app' );
}
if ( ! config.isEnabled( 'desktop' ) ) {
webpackConfig.plugins.push(
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]desktop$/, 'lodash/noop' )
);
}
if ( config.isEnabled( 'webpack/persistent-caching' ) ) {
webpackConfig.recordsPath = path.join( __dirname, '.webpack-cache', 'client-records.json' );
webpackConfig.plugins.unshift(
new HardSourceWebpackPlugin( {
cacheDirectory: path.join( __dirname, '.webpack-cache', 'client' ),
} )
);
}
if ( shouldMinify ) {
webpackConfig.plugins.push(
new UglifyJsPlugin( {
cache: 'docker' !== process.env.CONTAINER,
parallel: true,
uglifyOptions: { ecma: 5 },
sourceMap: Boolean( process.env.SOURCEMAP ),
} )
);
}
module.exports = webpackConfig;