-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathwebpack.config.js
More file actions
152 lines (145 loc) · 4.79 KB
/
Copy pathwebpack.config.js
File metadata and controls
152 lines (145 loc) · 4.79 KB
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
const path = require( 'path' );
const DependencyExtractionWebpackPlugin = require( '@woocommerce/dependency-extraction-webpack-plugin' );
const LiveReloadWebpackPlugin = require( '@kooneko/livereload-webpack-plugin' );
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
// Map runtime-corejs3 polyfill module imports back to native globals.
// Modern browser targets (last 2 evergreen versions per @wordpress/browserslist-config)
// have native support for all of these, and wp-polyfill backstops anything older that WP itself supports.
const corejsToGlobal = {
'@babel/runtime-corejs3/core-js/url': 'URL',
'@babel/runtime-corejs3/core-js-stable/url': 'URL',
'@babel/runtime-corejs3/core-js/url-search-params': 'URLSearchParams',
'@babel/runtime-corejs3/core-js-stable/url-search-params':
'URLSearchParams',
'@babel/runtime-corejs3/core-js/promise': 'Promise',
'@babel/runtime-corejs3/core-js-stable/promise': 'Promise',
'@babel/runtime-corejs3/core-js/symbol': 'Symbol',
'@babel/runtime-corejs3/core-js-stable/symbol': 'Symbol',
'@babel/runtime-corejs3/core-js-stable/map': 'Map',
'@babel/runtime-corejs3/core-js-stable/set': 'Set',
};
const defaultConfigOutput = defaultConfig.output;
const isProduction = process.env.NODE_ENV === 'production';
// Exclude jsonpFunction as it is not supported by webpack 5+.
// https://github.com/webpack/webpack.js.org/issues/3942
delete defaultConfigOutput.jsonpFunction;
module.exports = {
...defaultConfig,
output: {
...defaultConfigOutput,
chunkLoadingGlobal: defaultConfig.output.jsonpFunction,
devtoolModuleFilenameTemplate: 'webpack://[resource-path]',
},
devtool:
process.env.NODE_ENV === 'production'
? 'hidden-source-map'
: defaultConfig.devtool,
optimization: {
...defaultConfig.optimization,
minimizer: [
...defaultConfig.optimization.minimizer.map( ( plugin ) => {
if ( plugin.constructor.name === 'TerserPlugin' ) {
// wp-scripts does not allow to override the Terser minimizer sourceMap option, without this
// `devtool: 'hidden-source-map'` is not generated for js files.
plugin.options.sourceMap = true;
}
return plugin;
} ),
],
splitChunks: false,
},
plugins: [
...defaultConfig.plugins.filter(
( plugin ) =>
plugin.constructor.name !==
'DependencyExtractionWebpackPlugin' &&
plugin.constructor.name !== 'LiveReloadPlugin'
),
new DependencyExtractionWebpackPlugin( {
injectPolyfill: true,
} ),
process.env.BUNDLE_ANALYZE === 'true' &&
new BundleAnalyzerPlugin( {
analyzerMode: 'static',
reportFilename: '../bundle-report.html',
openAnalyzer: false,
} ),
! isProduction &&
new LiveReloadWebpackPlugin( {
port: process.env.WP_LIVE_RELOAD_PORT || 35729,
} ),
],
module: {
...defaultConfig.module,
rules: [
...defaultConfig.module.rules.map( ( rule ) => {
// If the rule doesn't apply to SCSS files, return the rule as is.
if ( ! rule.test.test( 'test.scss' ) ) {
return rule;
}
return {
...rule,
use: [
...rule.use.map( ( useEntry ) => {
if (
useEntry.loader !==
require.resolve( 'sass-loader' )
) {
return useEntry;
}
return {
...useEntry,
options: {
...( useEntry?.options || {} ),
sassOptions: {
...( useEntry?.options?.sassOptions ||
{} ),
quietDeps: true,
},
},
};
} ),
],
};
} ),
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
resolve: {
fullySpecified: false,
},
},
],
},
resolve: {
...defaultConfig.resolve,
extensions: [ '.json', '.js', '.jsx', '.mjs' ],
modules: [ path.join( __dirname, 'client' ), 'node_modules' ],
alias: {
...defaultConfig.resolve.alias,
wcstripe: path.resolve( __dirname, 'client' ),
// Swap Babel runtime helpers for the non-corejs equivalents (identical helpers, no polyfills).
'@babel/runtime-corejs3/helpers': '@babel/runtime/helpers',
},
},
externals: {
...( defaultConfig.externals || {} ),
...corejsToGlobal,
},
entry: {
'upe-classic': './client/classic/upe/index.js',
'upe-blocks': './client/blocks/upe/index.js',
'upe-settings': './client/settings/index.js',
'payment-gateways': './client/entrypoints/payment-gateways/index.js',
'express-checkout': './client/entrypoints/express-checkout/index.js',
'express-checkout-settings':
'./client/entrypoints/express-checkout-settings/index.js',
'amazon-pay-settings':
'./client/entrypoints/amazon-pay-settings/index.js',
'link-settings': './client/entrypoints/link-settings/index.js',
'plugins-page': './client/entrypoints/plugins-page/index.js',
'command-palette': './client/entrypoints/command-palette/index.js',
},
};