forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.node.js
149 lines (137 loc) · 5.87 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
/***** WARNING: No ES6 modules here. Not transpiled! *****/
/**
* External dependencies
*/
const fs = require( 'fs' );
const HappyPack = require( 'happypack' );
const HardSourceWebpackPlugin = require( 'hard-source-webpack-plugin' );
const os = require( 'os' );
const path = require( 'path' );
const webpack = require( 'webpack' );
const _ = require( 'lodash' );
/**
* Internal dependencies
*/
const cacheIdentifier = require( './server/bundler/babel/babel-loader-cache-identifier' );
const config = require( 'config' );
const isWindows = os.type() === 'Windows_NT';
/**
* 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-with-addons' ] = 'commonjs react/dist/react-with-addons.min';
externals.react = 'commonjs react/dist/react.min';
externals.redux = 'commonjs redux/dist/redux.min';
}
return externals;
}
const babelLoader = {
loader: 'babel-loader',
options: {
plugins: [ [
path.join( __dirname, 'server', 'bundler', 'babel', 'babel-plugin-transform-wpcalypso-async' ),
{ async: false }
] ],
cacheDirectory: path.join( __dirname, 'build', '.babel-server-cache' ),
cacheIdentifier: cacheIdentifier,
}
}
// happypack is not compatible with windows: https://github.com/amireh/happypack/blob/caaed26eec1795d464ac4b66abd29e60343e6252/README.md#does-it-work-under-windows
const jsLoader = isWindows ? babelLoader : 'happypack/loader';
const webpackConfig = {
devtool: 'source-map',
entry: './index.js',
target: 'node',
output: {
path: path.join( __dirname, 'build' ),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /extensions[\/\\]index/,
exclude: path.join( __dirname, 'node_modules' ),
loader: path.join( __dirname, 'server', 'bundler', 'extensions-loader' )
},
{
test: /sections.js$/,
exclude: path.join( __dirname, 'node_modules' ),
loader: path.join( __dirname, 'server', 'isomorphic-routing', 'loader' )
},
{
test: /\.jsx?$/,
exclude: /(node_modules|devdocs[\/\\]search-index)/,
loader: [ jsLoader ]
},
]
},
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( {
'PROJECT_NAME': JSON.stringify( config( 'project' ) )
} ),
! isWindows && new HappyPack( { loaders: [ babelLoader ] } ),
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]analytics$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]sites-list$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]olark$/, 'lodash/noop' ), // Depends on DOM
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]user$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^lib[\/\\]post-normalizer[\/\\]rule-create-better-excerpt$/, 'lodash/noop' ), // Depends on BOM
new webpack.NormalModuleReplacementPlugin( /^components[\/\\]seo[\/\\]reader-preview$/, 'components/empty-component' ), // Conflicts with component-closest module
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
new webpack.NormalModuleReplacementPlugin( /^client[\/\\]layout[\/\\]guided-tours[\/\\]config$/, 'components/empty-component' ), // should never be required server side
new webpack.NormalModuleReplacementPlugin( /^components[\/\\]site-selector$/, 'components/null-component' ), // Depends on BOM
] ),
externals: getExternals()
};
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', 'server-records.json' );
webpackConfig.plugins.unshift( new HardSourceWebpackPlugin( { cacheDirectory: path.join( __dirname, '.webpack-cache', 'server' ) } ) );
}
module.exports = webpackConfig;