-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathwebpack.config.js
126 lines (115 loc) · 3.28 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
/**
* External dependencies
*/
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const LiveReloadPlugin = require( 'webpack-livereload-plugin' );
const path = require( 'path' );
/**
* Internal dependencies
*/
const { camelCaseDash, hasBabelConfig } = require( '../utils' );
/**
* Converts @wordpress/* string request into request object.
*
* Note this isn't the same as camel case because of the
* way that numbers don't trigger the capitalized next letter.
*
* @example
* formatRequest( '@wordpress/api-fetch' );
* // { this: [ 'wp', 'apiFetch' ] }
* formatRequest( '@wordpress/i18n' );
* // { this: [ 'wp', 'i18n' ] }
*
* @param {string} request Request name from import statement.
* @return {Object} Request object formatted for further processing.
*/
const formatRequest = ( request ) => {
// '@wordpress/api-fetch' -> [ '@wordpress', 'api-fetch' ]
const [ , name ] = request.split( '/' );
// { this: [ 'wp', 'apiFetch' ] }
return {
this: [ 'wp', camelCaseDash( name ) ],
};
};
const wordpressExternals = ( context, request, callback ) => {
if ( /^@wordpress\//.test( request ) ) {
callback( null, formatRequest( request ), 'this' );
} else {
callback();
}
};
const externals = [
{
react: 'React',
'react-dom': 'ReactDOM',
moment: 'moment',
jquery: 'jQuery',
lodash: 'lodash',
'lodash-es': 'lodash',
// Distributed NPM packages may depend on Babel's runtime regenerator.
// In a WordPress context, the regenerator is assigned to the global
// scope via the `wp-polyfill` script. It is reassigned here as an
// externals to reduce the size of generated bundles.
//
// See: https://github.com/WordPress/gutenberg/issues/13890
'@babel/runtime/regenerator': 'regeneratorRuntime',
},
wordpressExternals,
];
const isProduction = process.env.NODE_ENV === 'production';
const mode = isProduction ? 'production' : 'development';
const getBabelLoaderOptions = () => hasBabelConfig() ? {} : {
babelrc: false,
configFile: false,
presets: [ require.resolve( '@wordpress/babel-preset-default' ) ],
};
const config = {
mode,
entry: {
index: path.resolve( process.cwd(), 'src', 'index.js' ),
},
output: {
filename: '[name].js',
path: path.resolve( process.cwd(), 'build' ),
},
externals,
resolve: {
alias: {
'lodash-es': 'lodash',
},
},
module: {
rules: [
{
test: /\.js$/,
use: require.resolve( 'source-map-loader' ),
enforce: 'pre',
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: require.resolve( 'babel-loader' ),
options: getBabelLoaderOptions(),
},
},
],
},
plugins: [
// WP_BUNDLE_ANALYZER global variable enables utility that represents bundle content
// as convenient interactive zoomable treemap.
process.env.WP_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(),
// WP_LIVE_RELOAD_PORT global variable changes port on which live reload works
// when running watch mode.
! isProduction && new LiveReloadPlugin( { port: process.env.WP_LIVE_RELOAD_PORT || 35729 } ),
].filter( Boolean ),
stats: {
children: false,
},
};
if ( ! isProduction ) {
// WP_DEVTOOL global variable controls how source maps are generated.
// See: https://webpack.js.org/configuration/devtool/#devtool.
config.devtool = process.env.WP_DEVTOOL || 'source-map';
}
module.exports = config;