forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.node.js
192 lines (179 loc) · 5.53 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* **** WARNING: No ES6 modules here. Not transpiled! ****
*
* @format
*/
/* eslint-disable import/no-nodejs-modules */
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const webpack = require( 'webpack' );
const _ = require( 'lodash' );
const os = require( 'os' );
/**
* Internal dependencies
*/
const cacheIdentifier = require( './server/bundler/babel/babel-loader-cache-identifier' );
const config = require( 'config' );
const bundleEnv = config( 'env' );
/**
* Internal variables
*/
const isDevelopment = bundleEnv === 'development';
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 { object } list of externals
*/
function getExternals() {
const externals = {};
// Don't bundle any node_modules, both to avoid a massive bundle, and problems
// with modules that are incompatible with webpack bundling.
fs.readdirSync( 'node_modules' )
.filter( function( module ) {
return [ '.bin' ].indexOf( module ) === -1;
} )
.forEach( function( module ) {
externals[ module ] = 'commonjs ' + module;
} );
// Don't bundle webpack.config, as it depends on absolute paths (__dirname)
externals[ 'webpack.config' ] = 'commonjs webpack.config';
// Exclude hot-reloader, as webpack will try and resolve this in production builds,
// and error.
externals[ 'bundler/hot-reloader' ] = 'commonjs bundler/hot-reloader';
// Exclude the devdocs search-index, as it's huge.
externals[ 'devdocs/search-index' ] = 'commonjs devdocs/search-index';
// Exclude the devdocs components usage stats data
externals[ 'devdocs/components-usage-stats.json' ] =
'commonjs devdocs/components-usage-stats.json';
// Exclude server/bundler/assets, since the files it requires don't exist until the bundler has run
externals[ 'bundler/assets' ] = 'commonjs bundler/assets';
// Map React and redux to the minimized version in production
if ( config( 'env' ) === 'production' ) {
externals.react = 'commonjs react/umd/react.production.min.js';
externals.redux = 'commonjs redux/dist/redux.min';
}
return externals;
}
const babelLoader = {
loader: 'babel-loader',
options: {
cacheDirectory: path.join( __dirname, 'build', '.babel-server-cache' ),
cacheIdentifier,
},
};
const webpackConfig = {
devtool: 'source-map',
entry: './index.js',
target: 'node',
output: {
path: path.join( __dirname, 'build' ),
filename: 'bundle.js',
},
mode: isDevelopment ? 'development' : 'production',
optimization: { minimize: false },
module: {
rules: [
{
test: /extensions[\/\\]index/,
exclude: path.join( __dirname, 'node_modules' ),
loader: path.join( __dirname, 'server', 'bundler', 'extensions-loader' ),
},
{
include: path.join( __dirname, 'client/sections.js' ),
use: {
loader: path.join( __dirname, 'server', 'bundler', 'sections-loader' ),
options: { forceRequire: true, onlyIsomorphic: true },
},
},
{
test: /\.jsx?$/,
exclude: /(node_modules|devdocs[\/\\]search-index)/,
use: [
{
loader: 'thread-loader',
options: { workers: Math.max( 2, Math.floor( os.cpus().length / 2 ) ) },
},
babelLoader,
],
},
{
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: /\.(svg)$/,
use: [
{
loader: 'file-loader',
options: {
emitFile: false, // On the server side, don't actually copy files
name: '[name].[ext]',
outputPath: 'images/',
},
},
],
},
{
test: /\.(sc|sa|c)ss$/,
loader: 'ignore-loader',
},
],
},
resolve: {
modules: [
__dirname,
path.join( __dirname, 'server' ),
path.join( __dirname, 'client' ),
path.join( __dirname, 'client', 'extensions' ),
'node_modules',
],
extensions: [ '.json', '.js', '.jsx' ],
},
node: {
// Tell webpack we want to supply absolute paths for server code,
// specifically needed by the client code bundler.
__filename: true,
__dirname: true,
},
plugins: _.compact( [
// Require source-map-support at the top, so we get source maps for the bundle
new webpack.BannerPlugin( {
banner: 'require( "source-map-support" ).install();',
raw: true,
entryOnly: false,
} ),
new webpack.DefinePlugin( {
BUILD_TIMESTAMP: JSON.stringify( new Date().toISOString() ),
PROJECT_NAME: JSON.stringify( config( 'project' ) ),
COMMIT_SHA: JSON.stringify( commitSha ),
'process.env.NODE_ENV': JSON.stringify( bundleEnv ),
} ),
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]abtest$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]analytics$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]user$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin(
/^components[\/\\]popover$/,
'components/null-component'
), // Depends on BOM and interactions don't work without JS
new webpack.NormalModuleReplacementPlugin(
/^my-sites[\/\\]themes[\/\\]theme-upload$/,
'components/empty-component'
), // Depends on BOM
] ),
externals: getExternals(),
};
if ( ! config.isEnabled( 'desktop' ) ) {
webpackConfig.plugins.push(
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]desktop$/, 'lodash/noop' )
);
}
module.exports = webpackConfig;