forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.node.js
168 lines (155 loc) · 5.59 KB
/
webpack.config.node.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
/**
* WARNING: No ES6 modules here. Not transpiled! *
*/
/* eslint-disable import/no-nodejs-modules */
const path = require( 'path' );
const FileConfig = require( '@automattic/calypso-build/webpack/file-loader' );
const TranspileConfig = require( '@automattic/calypso-build/webpack/transpile' );
const { shouldTranspileDependency } = require( '@automattic/calypso-build/webpack/util' );
const webpack = require( 'webpack' );
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const nodeExternals = require( 'webpack-node-externals' );
const cacheIdentifier = require( '../build-tools/babel/babel-loader-cache-identifier' );
const { packagesInMonorepo } = require( '../build-tools/lib/monorepo' );
const ExternalModulesWriter = require( './server/bundler/external-modules' );
const config = require( './server/config' );
const bundleEnv = config( 'env' );
const { workerCount } = require( './webpack.common' );
/**
* Internal variables
*/
const isDevelopment = bundleEnv === 'development';
const devTarget = process.env.DEV_TARGET || 'evergreen';
const shouldEmitStats = process.env.EMIT_STATS && process.env.EMIT_STATS !== 'false';
const shouldEmitStatsWithReasons = process.env.EMIT_STATS === 'withreasons';
const shouldConcatenateModules = process.env.CONCATENATE_MODULES !== 'false';
const cacheDirectory = path.resolve( '.cache', 'babel-server' );
const fileLoader = FileConfig.loader( {
// The final URL of the image is `${publicPath}${outputPath}/${fileName}` (note the slashes)
publicPath: isDevelopment ? `/calypso/${ devTarget }/` : '/calypso/',
outputPath: 'images/',
emitFile: false, // On the server side, don't actually copy files
} );
const commitSha = process.env.hasOwnProperty( 'COMMIT_SHA' ) ? process.env.COMMIT_SHA : '(unknown)';
/**
* This lists modules that must use commonJS `require()`s
* All modules listed here need to be ES5.
*
* @returns {Array} list of externals
*/
function getExternals() {
return [
// Don't bundle any node_modules, both to avoid a massive bundle, and problems
// with modules that are incompatible with webpack bundling.
nodeExternals( {
allowlist: [
// Force all monorepo packages to be bundled. We can guarantee that they are safe
// to bundle, and can avoid shipping the entire contents of the `packages/` folder
// (there are symlinks into `packages/` from the `node_modules` folder)
...packagesInMonorepo().map( ( pkg ) => new RegExp( `^${ pkg.name }(/|$)` ) ),
// bundle the core-js polyfills. We pick only a very small subset of the library
// to polyfill a few things that are not supported by the latest LTS Node.js,
// and this avoids shipping the entire library which is fairly big.
/^core-js\//,
// Ensure that file-loader files imported from packages in node_modules are
// _not_ externalized and can be processed by the fileLoader.
fileLoader.test,
/^calypso\//,
],
} ),
// Some imports should be resolved to runtime `require()` calls, with paths relative
// to the path of the `build/server.js` bundle.
{
// Don't bundle webpack.config, as it depends on absolute paths (__dirname)
'calypso/webpack.config': {
commonjs: '../client/webpack.config.js',
},
},
];
}
const buildDir = path.resolve( 'build' );
const webpackConfig = {
devtool: 'source-map',
entry: path.join( __dirname, 'server' ),
target: 'node',
output: {
path: buildDir,
filename: 'server.js',
chunkFilename: 'server.[name].js',
},
mode: isDevelopment ? 'development' : 'production',
optimization: {
concatenateModules: shouldConcatenateModules,
minimize: false,
},
module: {
rules: [
{
include: path.join( __dirname, 'sections.js' ),
use: {
loader: path.join( __dirname, '../build-tools/webpack/sections-loader' ),
options: { useRequire: true, onlyIsomorphic: true },
},
},
TranspileConfig.loader( {
workerCount,
configFile: path.resolve( 'babel.config.js' ),
cacheDirectory,
cacheIdentifier,
cacheCompression: false,
exclude: /node_modules\//,
} ),
TranspileConfig.loader( {
workerCount,
presets: [ require.resolve( '@automattic/calypso-build/babel/dependencies' ) ],
cacheDirectory,
cacheIdentifier,
cacheCompression: false,
include: shouldTranspileDependency,
} ),
fileLoader,
{
test: /\.(sc|sa|c)ss$/,
loader: 'ignore-loader',
},
],
},
resolve: {
extensions: [ '.json', '.js', '.jsx', '.ts', '.tsx' ],
mainFields: [ 'calypso:src', 'module', 'main' ],
modules: [ path.join( __dirname, 'extensions' ), 'node_modules' ],
alias: {
'@automattic/calypso-config': 'calypso/server/config',
},
},
node: {
// Tell webpack we want to supply absolute paths for server code,
// specifically needed by the client code bundler.
__filename: true,
__dirname: true,
},
plugins: [
shouldEmitStats &&
new BundleAnalyzerPlugin( {
analyzerMode: 'disabled', // just write the stats.json file
generateStatsFile: true,
statsFilename: path.join( __dirname, 'stats-server.json' ),
statsOptions: {
source: false,
reasons: shouldEmitStatsWithReasons,
optimizationBailout: false,
chunkOrigins: false,
chunkGroups: true,
},
} ),
new webpack.ExternalsPlugin( 'commonjs', getExternals() ),
new webpack.DefinePlugin( {
BUILD_TIMESTAMP: JSON.stringify( new Date().toISOString() ),
COMMIT_SHA: JSON.stringify( commitSha ),
'process.env.NODE_ENV': JSON.stringify( bundleEnv ),
} ),
new webpack.IgnorePlugin( { resourceRegExp: /^\.\/locale$/, contextRegExp: /moment$/ } ),
! isDevelopment && new ExternalModulesWriter(),
].filter( Boolean ),
};
module.exports = webpackConfig;