forked from RueNetwork/RueClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
352 lines (329 loc) · 12.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
**** WARNING: No ES6 modules here. Not transpiled! ****
*/
/* eslint-disable import/no-nodejs-modules */
/**
* External dependencies
*/
const _ = require( 'lodash' );
const path = require( 'path' );
// eslint-disable-next-line import/no-extraneous-dependencies
const webpack = require( 'webpack' );
const AssetsWriter = require( './server/bundler/assets-writer' );
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const CircularDependencyPlugin = require( 'circular-dependency-plugin' );
// eslint-disable-next-line import/no-extraneous-dependencies
const DuplicatePackageCheckerPlugin = require( 'duplicate-package-checker-webpack-plugin' );
const FileConfig = require( '@automattic/calypso-build/webpack/file-loader' );
const MomentTimezoneDataPlugin = require( 'moment-timezone-data-webpack-plugin' );
const InlineConstantExportsPlugin = require( '@automattic/webpack-inline-constant-exports-plugin' );
const Minify = require( '@automattic/calypso-build/webpack/minify' );
const SassConfig = require( '@automattic/calypso-build/webpack/sass' );
const TranspileConfig = require( '@automattic/calypso-build/webpack/transpile' );
const { cssNameFromFilename } = require( '@automattic/calypso-build/webpack/util' );
const ExtensiveLodashReplacementPlugin = require( '@automattic/webpack-extensive-lodash-replacement-plugin' );
/**
* Internal dependencies
*/
const cacheIdentifier = require( './server/bundler/babel/babel-loader-cache-identifier' );
const config = require( './server/config' );
const { workerCount } = require( './webpack.common' );
const getAliasesForExtensions = require( './config/webpack/extensions' );
/**
* Internal variables
*/
const calypsoEnv = config( 'env_id' );
const bundleEnv = config( 'env' );
const isDevelopment = bundleEnv !== 'production';
const shouldMinify =
process.env.MINIFY_JS === 'true' ||
( process.env.MINIFY_JS !== 'false' && bundleEnv === 'production' && calypsoEnv !== 'desktop' );
const shouldEmitStats = process.env.EMIT_STATS && process.env.EMIT_STATS !== 'false';
const shouldEmitStatsWithReasons = process.env.EMIT_STATS === 'withreasons';
const shouldCheckForCycles = process.env.CHECK_CYCLES === 'true';
const isCalypsoClient = process.env.BROWSERSLIST_ENV !== 'server';
const isDesktop = calypsoEnv === 'desktop' || calypsoEnv === 'desktop-development';
const defaultBrowserslistEnv = isCalypsoClient && ! isDesktop ? 'evergreen' : 'defaults';
const browserslistEnv = process.env.BROWSERSLIST_ENV || defaultBrowserslistEnv;
const extraPath = browserslistEnv === 'defaults' ? 'fallback' : browserslistEnv;
if ( ! process.env.BROWSERSLIST_ENV ) {
process.env.BROWSERSLIST_ENV = browserslistEnv;
}
/*
* Create reporter for ProgressPlugin (used with EMIT_STATS)
*/
function createProgressHandler() {
const startTime = Date.now();
let lastShownBuildingMessageTime = null;
let lastUnshownBuildingMessage = null;
return ( percentage, msg, ...details ) => {
const nowTime = Date.now();
const timeString = ( ( nowTime - startTime ) / 1000 ).toFixed( 1 ) + 's';
const percentageString = `${ Math.floor( percentage * 100 ) }%`;
const detailsString = details
.map( detail => {
if ( ! detail ) {
return '';
}
if ( detail.length > 40 ) {
return `…${ detail.substr( detail.length - 39 ) }`;
}
return detail;
} )
.join( ' ' );
const message = `${ timeString } ${ percentageString } ${ msg } ${ detailsString }`;
// There are plenty of 'building' messages that make the log too long for CircleCI web UI.
// Let's throttle the 'building' messages to one per second, while always showing the last one.
if ( msg === 'building' ) {
if ( lastShownBuildingMessageTime && nowTime - lastShownBuildingMessageTime < 1000 ) {
// less than 1000ms since last message: ignore, but store for case it's the last one
lastUnshownBuildingMessage = message;
return;
}
// the message will be shown and its time recorded
lastShownBuildingMessageTime = nowTime;
lastUnshownBuildingMessage = null;
} else if ( lastUnshownBuildingMessage ) {
// The last 'building' message should always be shown, no matter the timing.
// We do that on the first non-'building' message.
console.log( lastUnshownBuildingMessage ); // eslint-disable-line no-console
lastUnshownBuildingMessage = null;
}
console.log( message ); // eslint-disable-line no-console
};
}
const nodeModulesToTranspile = [
// general form is <package-name>/.
// The trailing slash makes sure we're not matching these as prefixes
// In some cases we do want prefix style matching (lodash. for lodash.assign)
'd3-array/',
'd3-scale/',
'debug/',
];
/**
* Check to see if we should transpile certain files in node_modules
* @param {String} filepath the path of the file to check
* @returns {Boolean} True if we should transpile it, false if not
*
* We had a thought to try to find the package.json and use the engines property
* to determine what we should transpile, but not all libraries set engines properly
* (see [email protected]). Instead, we transpile libraries we know to have dropped Node 4 support
* are likely to remain so going forward.
*/
function shouldTranspileDependency( filepath ) {
// find the last index of node_modules and check from there
// we want <working>/node_modules/a-package/node_modules/foo/index.js to only match foo, not a-package
const marker = '/node_modules/';
const lastIndex = filepath.lastIndexOf( marker );
if ( lastIndex === -1 ) {
// we're not in node_modules
return false;
}
const checkFrom = lastIndex + marker.length;
return _.some(
nodeModulesToTranspile,
modulePart => filepath.substring( checkFrom, checkFrom + modulePart.length ) === modulePart
);
}
let outputFilename = '[name].[chunkhash].min.js'; // prefer the chunkhash, which depends on the chunk, not the entire build
let outputChunkFilename = '[name].[chunkhash].min.js'; // ditto
// 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
//
// Desktop: no chunks or dll here, just one big file for the desktop app
if ( isDevelopment || isDesktop ) {
outputFilename = '[name].js';
outputChunkFilename = '[name].js';
}
const cssFilename = cssNameFromFilename( outputFilename );
const cssChunkFilename = cssNameFromFilename( outputChunkFilename );
const webpackConfig = {
bail: ! isDevelopment,
context: __dirname,
entry: {
'entry-main': [ path.join( __dirname, 'client', 'boot', 'app' ) ],
'entry-domains-landing': [ path.join( __dirname, 'client', 'landing', 'domains' ) ],
},
mode: isDevelopment ? 'development' : 'production',
devtool: process.env.SOURCEMAP || ( isDevelopment ? '#eval' : false ),
output: {
path: path.join( __dirname, 'public', extraPath ),
pathinfo: false,
publicPath: `/calypso/${ extraPath }/`,
filename: outputFilename,
chunkFilename: outputChunkFilename,
devtoolModuleFilenameTemplate: 'app:///[resource-path]',
},
optimization: {
splitChunks: {
chunks: 'all',
name: !! ( isDevelopment || shouldEmitStats ),
maxAsyncRequests: 20,
maxInitialRequests: 5,
},
runtimeChunk: isDesktop ? false : { name: 'manifest' },
moduleIds: 'named',
chunkIds: isDevelopment || shouldEmitStats ? 'named' : 'natural',
minimize: shouldMinify,
minimizer: Minify( {
cache: process.env.CIRCLECI
? `${ process.env.HOME }/terser-cache/${ extraPath }`
: 'docker' !== process.env.CONTAINER,
parallel: workerCount,
sourceMap: Boolean( process.env.SOURCEMAP ),
terserOptions: {
mangle: ! isDesktop,
},
} ),
},
module: {
rules: [
TranspileConfig.loader( {
workerCount,
configFile: path.join( __dirname, 'babel.config.js' ),
cacheDirectory: path.join( __dirname, 'build', '.babel-client-cache', extraPath ),
cacheIdentifier,
exclude: /node_modules\//,
} ),
TranspileConfig.loader( {
workerCount,
configFile: path.resolve( __dirname, 'babel.dependencies.config.js' ),
cacheDirectory: path.join( __dirname, 'build', '.babel-client-cache', extraPath ),
cacheIdentifier,
include: shouldTranspileDependency,
} ),
SassConfig.loader( {
preserveCssCustomProperties: true,
includePaths: [ path.join( __dirname, 'client' ) ],
prelude: `@import '${ path.join( __dirname, 'assets/stylesheets/shared/_utils.scss' ) }';`,
} ),
{
include: path.join( __dirname, 'client/sections.js' ),
loader: path.join( __dirname, 'server', 'bundler', 'sections-loader' ),
options: {
include: process.env.SECTION_LIMIT ? process.env.SECTION_LIMIT.split( ',' ) : null,
},
},
{
test: /\.html$/,
loader: 'html-loader',
},
FileConfig.loader(),
{
include: require.resolve( 'tinymce/tinymce' ),
use: 'exports-loader?window=tinymce',
},
{
test: /node_modules[/\\]tinymce/,
use: 'imports-loader?this=>window',
},
],
},
resolve: {
extensions: [ '.json', '.js', '.jsx', '.ts', '.tsx' ],
modules: [ path.join( __dirname, 'client' ), 'node_modules' ],
alias: Object.assign(
{
'react-virtualized': 'react-virtualized/dist/es',
debug: path.resolve( __dirname, 'node_modules/debug' ),
store: 'store/dist/store.modern',
gridicons$: path.resolve( __dirname, 'client/components/gridicon' ),
},
getAliasesForExtensions( {
extensionsDirectory: path.join( __dirname, 'client', 'extensions' ),
} )
),
},
node: false,
plugins: _.compact( [
new webpack.DefinePlugin( {
'process.env.NODE_ENV': JSON.stringify( bundleEnv ),
'process.env.FORCE_REDUCED_MOTION': JSON.stringify(
!! process.env.FORCE_REDUCED_MOTION || false
),
global: 'window',
} ),
new webpack.NormalModuleReplacementPlugin( /^path$/, 'path-browserify' ),
isCalypsoClient && new webpack.IgnorePlugin( /^\.\/locale$/, /moment$/ ),
...SassConfig.plugins( {
chunkFilename: cssChunkFilename,
filename: cssFilename,
minify: ! isDevelopment,
} ),
isCalypsoClient &&
new AssetsWriter( {
filename:
browserslistEnv === 'defaults'
? 'assets-fallback.json'
: `assets-${ browserslistEnv }.json`,
path: path.join( __dirname, 'server', 'bundler' ),
assetExtraPath: extraPath,
} ),
new DuplicatePackageCheckerPlugin(),
shouldCheckForCycles &&
new CircularDependencyPlugin( {
exclude: /node_modules/,
failOnError: false,
allowAsyncCycles: false,
cwd: process.cwd(),
} ),
shouldEmitStats &&
new BundleAnalyzerPlugin( {
analyzerMode: 'disabled', // just write the stats.json file
generateStatsFile: true,
statsFilename: path.join( __dirname, 'stats.json' ),
statsOptions: {
source: false,
reasons: shouldEmitStatsWithReasons,
optimizationBailout: false,
chunkOrigins: false,
chunkGroups: true,
},
} ),
shouldEmitStats && new webpack.ProgressPlugin( createProgressHandler() ),
new MomentTimezoneDataPlugin( {
startYear: 2000,
cacheDir: path.join(
__dirname,
'build',
'.moment-timezone-data-webpack-plugin-cache',
extraPath
),
} ),
isCalypsoClient && new InlineConstantExportsPlugin( /\/client\/state\/action-types.js$/ ),
] ),
externals: [ 'electron' ],
};
if ( isDevelopment ) {
webpackConfig.plugins.push( new webpack.HotModuleReplacementPlugin() );
for ( const entrypoint of Object.keys( webpackConfig.entry ) ) {
webpackConfig.entry[ entrypoint ].unshift( 'webpack-hot-middleware/client' );
}
}
if ( ! config.isEnabled( 'desktop' ) ) {
webpackConfig.plugins.push(
new webpack.NormalModuleReplacementPlugin( /^lib[/\\]desktop$/, 'lodash-es/noop' )
);
}
// Replace `lodash` with `lodash-es`.
if ( isCalypsoClient ) {
webpackConfig.plugins.push( new ExtensiveLodashReplacementPlugin() );
}
// List of polyfills that we skip including in the evergreen bundle.
// CoreJS polyfills are automatically dropped using the browserslist definitions; no need to include them here.
const polyfillsSkippedInEvergreen = [
// Local storage used to throw errors in Safari private mode, but that's no longer the case in Safari >=11.
/^lib[/\\]local-storage-polyfill$/,
// The SVG external content polyfill (svg4everybody) isn't needed for evergreen browsers.
/^svg4everybody$/,
// The fetch polyfill isn't needed for evergreen browsers, as they all support it.
/^isomorphic-fetch$/,
];
if ( browserslistEnv === 'evergreen' ) {
for ( const polyfill of polyfillsSkippedInEvergreen ) {
webpackConfig.plugins.push(
new webpack.NormalModuleReplacementPlugin( polyfill, 'lodash-es/noop' )
);
}
}
module.exports = webpackConfig;