forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.node.js
120 lines (111 loc) · 5.09 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
/***** WARNING: ES5 code only here. Not transpiled! *****/
/**
* External dependencies
*/
var webpack = require( 'webpack' ),
path = require( 'path' ),
HardSourceWebpackPlugin = require( 'hard-source-webpack-plugin' ),
fs = require( 'fs' );
/**
* Internal dependencies
*/
var config = require( 'config' );
/**
* This lists modules that must use commonJS `require()`s
* All modules listed here need to be ES5.
*
* @returns { object } list of externals
*/
function getExternals() {
var 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.
// TODO: use WebpackDefinePlugin for CALYPSO_ENV, so we can make conditional requires work
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';
return externals;
}
var webpackConfig = {
devtool: 'source-map',
entry: 'index.js',
target: 'node',
output: {
path: path.join( __dirname, 'build' ),
filename: 'bundle-' + ( process.env.CALYPSO_ENV || 'development' ) + '.js',
},
module: {
loaders: [
{
test: /sections.js$/,
exclude: 'node_modules',
loader: path.join( __dirname, 'server', 'isomorphic-routing', 'loader' )
},
{
test: /\.jsx?$/,
exclude: /(node_modules|devdocs\/search-index)/,
loader: 'babel',
query: {
plugins: [ [
path.join( __dirname, 'server', 'bundler', 'babel', 'babel-plugin-transform-wpcalypso-async' ),
{ async: false }
] ]
}
},
{
test: /\.json$/,
exclude: /(devdocs\/components-usage-stats.json)/,
loader: 'json-loader'
}
]
},
resolve: {
extensions: [ '', '.json', '.js', '.jsx' ],
root: [ path.join( __dirname, 'server' ), path.join( __dirname, 'client' ), __dirname ],
modulesDirectories: [ 'node_modules' ]
},
node: {
// Tell webpack we want to supply absolute paths for server code,
// specifically needed by the client code bundler.
__filename: true,
__dirname: true
},
plugins: [
// Require source-map-support at the top, so we get source maps for the bundle
new webpack.BannerPlugin( 'require( "source-map-support" ).install();', { raw: true, entryOnly: false } ),
new webpack.NormalModuleReplacementPlugin( /^lib\/analytics$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^lib\/olark$/, 'lodash/noop' ), // Too many dependencies, e.g. sites-list
new webpack.NormalModuleReplacementPlugin( /^lib\/post-normalizer\/rule-create-better-excerpt$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^components\/seo\/preview-upgrade-nudge$/, 'components/empty-component' ), // Depends on page.js and should never be required server side
new webpack.NormalModuleReplacementPlugin( /^components\/popover$/, 'components/empty-component' ), // Depends on BOM and interactions don't work without JS
new webpack.NormalModuleReplacementPlugin( /^my-sites\/themes\/thanks-modal$/, 'components/empty-component' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^my-sites\/themes\/themes-site-selector-modal$/, 'components/empty-component' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^my-sites\/themes\/theme-upload$/, 'components/empty-component' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^my-sites\/themes\/single-site$/, 'components/empty-component' ), // Depends on DOM
new webpack.NormalModuleReplacementPlugin( /^my-sites\/themes\/multi-site$/, 'components/empty-component' ), // Depends on DOM
new webpack.NormalModuleReplacementPlugin( /^state\/ui\/editor\/selectors$/, 'lodash/noop' ), // will never be called server-side
new webpack.NormalModuleReplacementPlugin( /^state\/posts\/selectors$/, 'lodash/noop' ), // will never be called server-side
new webpack.NormalModuleReplacementPlugin( /^client\/layout\/guided-tours\/config$/, 'components/empty-component' ) // should never be required server side
],
externals: getExternals()
};
if ( config.isEnabled( 'webpack/persistent-caching' ) ) {
webpackConfig.recordsPath = path.join( __dirname, '.webpack-cache', 'server-records.json' ),
webpackConfig.plugins.unshift( new HardSourceWebpackPlugin( { cacheDirectory: path.join( __dirname, '.webpack-cache', 'server' ) } ) );
}
module.exports = webpackConfig;