-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
shared.js
105 lines (99 loc) · 2.49 KB
/
shared.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
/**
* External dependencies
*/
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const { DefinePlugin } = require( 'webpack' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const postcss = require( 'postcss' );
/**
* WordPress dependencies
*/
const ReadableJsAssetsWebpackPlugin = require( '@wordpress/readable-js-assets-webpack-plugin' );
const { NODE_ENV: mode = 'development', WP_DEVTOOL: devtool = 'source-map' } =
process.env;
const baseConfig = {
target: 'browserslist',
optimization: {
// Only concatenate modules in production, when not analyzing bundles.
concatenateModules:
mode === 'production' && ! process.env.WP_BUNDLE_ANALYZER,
minimizer: [
new TerserPlugin( {
parallel: true,
terserOptions: {
output: {
comments: /translators:/i,
},
compress: {
passes: 2,
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
},
extractComments: false,
} ),
],
},
mode,
module: {
rules: [
{
test: /\.js$/,
use: require.resolve( 'source-map-loader' ),
enforce: 'pre',
},
],
},
watchOptions: {
ignored: [
'**/node_modules',
'**/packages/*/src/**/*.{js,ts,tsx,scss}',
],
aggregateTimeout: 500,
},
devtool,
};
const plugins = [
// The WP_BUNDLE_ANALYZER global variable enables a utility that represents bundle
// content as a convenient interactive zoomable treemap.
process.env.WP_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(),
new DefinePlugin( {
// Inject the `IS_GUTENBERG_PLUGIN` global, used for feature flagging.
'globalThis.IS_GUTENBERG_PLUGIN': JSON.stringify(
Boolean( process.env.npm_package_config_IS_GUTENBERG_PLUGIN )
),
// Inject the `IS_WORDPRESS_CORE` global, used for feature flagging.
'globalThis.IS_WORDPRESS_CORE': JSON.stringify(
Boolean( process.env.npm_package_config_IS_WORDPRESS_CORE )
),
// Inject the `SCRIPT_DEBUG` global, used for dev versions of JavaScript.
'globalThis.SCRIPT_DEBUG': JSON.stringify( mode === 'development' ),
} ),
mode === 'production' && new ReadableJsAssetsWebpackPlugin(),
];
const stylesTransform = ( content ) => {
return postcss( [
require( 'cssnano' )( {
preset: [
'default',
{
discardComments: {
removeAll: true,
},
normalizeWhitespace: mode === 'production',
},
],
} ),
] )
.process( content, {
from: 'src/app.css',
to: 'dest/app.css',
} )
.then( ( result ) => result.css );
};
module.exports = {
baseConfig,
plugins,
stylesTransform,
};