-
Notifications
You must be signed in to change notification settings - Fork 15
/
webpack.config.js
133 lines (120 loc) · 2.99 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
127
128
129
130
131
132
133
/**
* External dependencies
*/
// TerserPlugin is bundled in Webpack 5.
// eslint-disable-next-line import/no-extraneous-dependencies
const TerserPlugin = require( 'terser-webpack-plugin' );
// path is a native Node module
// eslint-disable-next-line import/no-extraneous-dependencies
const path = require( 'path' );
const WebpackBar = require( 'webpackbar' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const config = {
srcDir: '/js/src/',
distDirModern: 'js/dist/module/',
distDirLegacy: 'js/dist/nomodule/',
};
config.modernJsEntries = {
'web-vitals-analytics': config.srcDir + 'web-vitals-analytics.js',
};
config.legacyJsEntries = {};
/**
* WordPress dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
/**
* Remove the default `DependencyExtractionWebpackPlugin` instance from
* plugins and instantiate a new one with our own options.
*/
defaultConfig.plugins = defaultConfig.plugins.filter(
( plugin ) => plugin.constructor.name !== 'DependencyExtractionWebpackPlugin'
);
defaultConfig.plugins.push( new DependencyExtractionWebpackPlugin( {
outputFilename: '[name].asset.php',
} ) );
const sharedConfig = {
optimization: {
minimizer: [
new TerserPlugin( {
terserOptions: {
output: {
comments: /translators:/i,
},
},
extractComments: false,
} ),
],
},
plugins: [ ...defaultConfig.plugins ],
};
const configureBabelLoader = ( browserlist ) => {
return {
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
babelrc: false,
exclude: [ /core-js/, /regenerator-runtime/ ],
presets: [
[
'@babel/preset-env',
{
loose: true,
modules: false,
// debug: true,
corejs: 3,
useBuiltIns: 'usage',
targets: {
browsers: browserlist,
},
},
],
],
plugins: [ '@babel/plugin-syntax-dynamic-import' ],
},
},
};
};
const modernConfig = {
output: {
path: path.join( __dirname, config.distDirModern ),
filename: `[name].[contenthash].js`,
chunkFilename: `[name].[chunkhash].js`,
// Needed for [chunkhash] length to match the hard-coded
// settings in `DependencyExtractionWebpackPlugin`.
hashDigestLength: 32,
},
module: {
rules: [
configureBabelLoader( [
// The last two versions of each browser, excluding versions
// that don't support <script type="module">.
'last 2 Chrome versions',
'not Chrome < 60',
'last 2 Safari versions',
'not Safari < 10.1',
'last 2 iOS versions',
'not iOS < 10.3',
'last 2 Firefox versions',
'not Firefox < 54',
'last 2 Edge versions',
'not Edge < 15',
] ),
],
},
};
const modernJs = {
...defaultConfig,
...sharedConfig,
...modernConfig,
entry: config.modernJsEntries,
plugins: [
...sharedConfig.plugins,
// Display nice progress bar while building or watching.
new WebpackBar( {
name: 'ModernJs',
color: '#36f271',
} ),
],
};
module.exports = [ modernJs ];