forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
365 lines (341 loc) · 9.01 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* External dependencies
*/
const path = require( 'path' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const OptimizeCSSAssetsPlugin = require( 'optimize-css-assets-webpack-plugin' );
const RtlCssPlugin = require( 'rtlcss-webpack-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const WebpackBar = require( 'webpackbar' );
/**
* WordPress dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const { defaultRequestToExternal, defaultRequestToHandle, camelCaseDash } = require( '@wordpress/dependency-extraction-webpack-plugin/lib/util' );
const sharedConfig = {
...defaultConfig,
output: {
path: path.resolve( process.cwd(), 'assets', 'js' ),
filename: '[name].js',
chunkFilename: '[name].js',
},
// TODO: Remove the `module` override once @wordpress/scripts upgrades to PostCSS 8.
module: {
...defaultConfig.module,
rules: defaultConfig.module.rules.map(
( rule ) => {
const postCssLoader = Array.isArray( rule.use ) && rule.use.find(
( loader ) => loader.loader && loader.loader.includes( 'postcss-loader' ),
);
if ( postCssLoader ) {
postCssLoader.loader = 'postcss-loader';
}
return rule;
},
),
},
plugins: [
...defaultConfig.plugins
.map(
( plugin ) => {
if ( plugin.constructor.name === 'MiniCssExtractPlugin' ) {
plugin.options.filename = '../css/[name].css';
}
return plugin;
},
)
.filter( ( plugin ) => plugin.constructor.name !== 'CleanWebpackPlugin' ),
new RtlCssPlugin( {
filename: '../css/[name]-rtl.css',
} ),
],
optimization: {
minimizer: [
new TerserPlugin( {
parallel: true,
sourceMap: false,
cache: true,
terserOptions: {
output: {
comments: /translators:/i,
},
},
extractComments: false,
} ),
new OptimizeCSSAssetsPlugin( { } ),
],
},
};
const ampValidation = {
...sharedConfig,
entry: {
'amp-validated-url-post-edit-screen': './assets/src/amp-validation/amp-validated-url-post-edit-screen.js',
'amp-validated-urls-index': './assets/src/amp-validation/amp-validated-urls-index.js',
'amp-validation-detail-toggle': './assets/src/amp-validation/amp-validation-detail-toggle.js',
'amp-validation-single-error-url-details': './assets/src/amp-validation/amp-validation-single-error-url-details.js',
},
plugins: [
...sharedConfig.plugins,
new WebpackBar( {
name: 'AMP Validation',
color: '#1c5fec',
} ),
],
};
const blockEditor = {
...sharedConfig,
externals: {
// Make localized data importable.
'amp-block-editor-data': 'ampBlockEditor',
},
entry: {
'amp-block-editor': './assets/src/block-editor/index.js',
'amp-block-validation': './assets/src/block-validation/index.js',
},
plugins: [
...sharedConfig.plugins,
new WebpackBar( {
name: 'Block Editor',
color: '#1773a8',
} ),
],
};
const classicEditor = {
...sharedConfig,
entry: {
'amp-post-meta-box': './assets/src/classic-editor/amp-post-meta-box.js',
},
plugins: [
...sharedConfig.plugins,
new WebpackBar( {
name: 'Classic Editor',
color: '#dc3232',
} ),
],
};
const admin = {
...sharedConfig,
entry: {
'amp-validation-tooltips': './assets/src/admin/amp-validation-tooltips.js',
'amp-paired-browsing-app': './assets/src/admin/paired-browsing/app.js',
'amp-paired-browsing-client': './assets/src/admin/paired-browsing/client.js',
},
plugins: [
...sharedConfig.plugins,
new WebpackBar( {
name: 'Admin',
color: '#67b255',
} ),
],
};
const customizer = {
...sharedConfig,
entry: {
'amp-customize-controls': './assets/src/customizer/amp-customize-controls.js',
'amp-customize-controls-legacy': './assets/src/customizer/amp-customize-controls-legacy.js',
'amp-customize-preview-legacy': './assets/src/customizer/amp-customize-preview-legacy.js',
'amp-customizer-design-preview-legacy': './assets/src/customizer/amp-customizer-design-preview-legacy.js',
},
plugins: [
...sharedConfig.plugins,
new WebpackBar( {
name: 'Customizer',
color: '#f27136',
} ),
],
};
const WORDPRESS_NAMESPACE = '@wordpress/';
const BABEL_NAMESPACE = '@babel/';
const gutenbergPackages = [ '@babel/polyfill', '@wordpress/dom-ready', '@wordpress/i18n', '@wordpress/url' ].map(
( packageName ) => {
if ( 0 !== packageName.indexOf( WORDPRESS_NAMESPACE ) && 0 !== packageName.indexOf( BABEL_NAMESPACE ) ) {
return null;
}
const camelCaseName = '@wordpress/i18n' === packageName
? 'i18n'
: camelCaseDash( packageName.replace( WORDPRESS_NAMESPACE, '' ).replace( BABEL_NAMESPACE, '' ) );
const handle = packageName.replace( WORDPRESS_NAMESPACE, 'wp-' ).replace( BABEL_NAMESPACE, 'wp-' );
return {
camelCaseName,
entryPath: 'polyfill' === camelCaseName ? path.resolve( __dirname, 'assets/src/polyfills/wp-polyfill' ) : packageName,
handle,
packageName,
};
},
).filter( ( packageData ) => packageData );
const wpPolyfills = {
...sharedConfig,
externals: {},
entry: gutenbergPackages.reduce(
( memo, { camelCaseName, entryPath } ) =>
( { ...memo, [ camelCaseName ]: entryPath } ),
{} ),
output: {
devtoolNamespace: 'wp',
filename: ( pathData ) => `${ gutenbergPackages.find(
( gutenbergPackage ) => pathData.chunk.name === gutenbergPackage.camelCaseName,
).handle }.js`,
path: path.resolve( __dirname, 'assets/js' ),
library: [ 'wp', '[name]' ],
libraryTarget: 'this',
},
plugins: [
new DependencyExtractionWebpackPlugin( {
useDefaults: false,
requestToHandle: ( request ) => {
if ( gutenbergPackages.find( ( { packageName } ) => packageName === request ) ) {
return undefined;
}
return defaultRequestToHandle( request );
},
requestToExternal: ( request ) => {
if ( gutenbergPackages.find( ( { packageName } ) => packageName === request ) ) {
return undefined;
}
return defaultRequestToExternal( request );
},
} ),
new CopyWebpackPlugin( {
patterns: [
{
from: 'node_modules/lodash/lodash.js',
to: './vendor/lodash.js',
},
],
} ),
new WebpackBar( {
name: 'WordPress Polyfills',
color: '#21a0d0',
} ),
],
};
const setup = {
...sharedConfig,
entry: {
'amp-onboarding-wizard': [
'./assets/src/onboarding-wizard',
],
},
externals: {
'amp-settings': 'ampSettings',
},
plugins: [
...sharedConfig.plugins.filter(
( plugin ) => plugin.constructor.name !== 'DependencyExtractionWebpackPlugin',
),
new DependencyExtractionWebpackPlugin( {
useDefaults: false,
// Most dependencies will be bundled for the AMP setup screen for compatibility across WP versions.
requestToHandle: ( handle ) => {
switch ( handle ) {
case 'lodash':
case '@wordpress/api-fetch':
case '@wordpress/dom-ready':
case '@wordpress/html-entities':
case '@wordpress/url':
case '@wordpress/i18n':
return defaultRequestToHandle( handle );
default:
return undefined;
}
},
requestToExternal: ( external ) => {
switch ( external ) {
case 'lodash':
case '@wordpress/api-fetch':
case '@wordpress/dom-ready':
case '@wordpress/html-entities':
case '@wordpress/url':
case '@wordpress/i18n':
return defaultRequestToExternal( external );
default:
return undefined;
}
},
} ),
new WebpackBar( {
name: 'Onboarding wizard',
color: '#1773a8',
} ),
],
};
const settingsPage = {
...sharedConfig,
entry: {
'wp-api-fetch': [
'./assets/src/polyfills/api-fetch.js',
],
'wp-components': [
'@wordpress/components/build-style/style.css',
],
'amp-settings': [
'./assets/src/settings-page',
],
},
externals: {
'amp-settings': 'ampSettings',
},
resolve: {
alias: {
'@wordpress/api-fetch__non-shim': require.resolve( '@wordpress/api-fetch' ),
},
},
plugins: [
...sharedConfig.plugins.filter(
( plugin ) => plugin.constructor.name !== 'DependencyExtractionWebpackPlugin',
),
new DependencyExtractionWebpackPlugin( {
useDefaults: false,
// Most dependencies will be bundled for the AMP setup screen for compatibility across WP versions.
requestToHandle: ( handle ) => {
switch ( handle ) {
case 'lodash':
case '@wordpress/api-fetch':
case '@wordpress/i18n':
return defaultRequestToHandle( handle );
default:
return undefined;
}
},
requestToExternal: ( external ) => {
switch ( external ) {
case 'lodash':
case '@wordpress/api-fetch':
case '@wordpress/i18n':
return defaultRequestToExternal( external );
default:
return undefined;
}
},
} ),
new WebpackBar( {
name: 'Settings page',
color: '#67b255',
} ),
],
};
const mobileRedirection = {
...sharedConfig,
entry: {
'mobile-redirection': './assets/src/mobile-redirection.js',
},
plugins: [
...sharedConfig.plugins,
new WebpackBar( {
name: 'Mobile Redirection',
color: '#f27136',
} ),
],
};
module.exports = [
ampValidation,
blockEditor,
classicEditor,
admin,
customizer,
wpPolyfills,
setup,
settingsPage,
mobileRedirection,
];