-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathwebpack.config.js
More file actions
168 lines (159 loc) · 4.64 KB
/
Copy pathwebpack.config.js
File metadata and controls
168 lines (159 loc) · 4.64 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const path = require( 'path' );
const webpack = require( 'webpack' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const pkg = require( './package.json' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const RtlCssPlugin = require( '@wordpress/scripts/plugins/rtlcss-webpack-plugin' );
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
function isDefaultCssPlugin( plugin ) {
const pluginName = plugin?.constructor?.name;
return (
plugin instanceof MiniCSSExtractPlugin ||
pluginName === MiniCSSExtractPlugin.name ||
plugin instanceof RtlCssPlugin ||
pluginName === RtlCssPlugin.name ||
pluginName === 'RtlCSSPlugin'
);
}
// Custom RTL CSS Plugin to use redirection naming
class CustomRtlCssPlugin extends RtlCssPlugin {
processAssets = ( compilation, callback ) => {
const rtlcss = require( 'rtlcss' );
const chunks = Array.from( compilation.chunks );
chunks.forEach( ( chunk ) => {
const files = Array.from( chunk.files );
files
.filter( ( filename ) => path.extname( filename ) === '.css' )
.forEach( ( filename ) => {
const src = compilation.assets[ filename ].source();
const dst = rtlcss.process( src );
// Use custom naming: redirection.css -> redirection-rtl.css
const dstFileName = filename.replace( /\.css$/, '-rtl.css' );
compilation.assets[ dstFileName ] = new webpack.sources.RawSource( dst );
chunk.files.add( dstFileName );
} );
} );
callback();
};
}
const modified = {
...defaultConfig,
output: {
...defaultConfig.output,
filename: 'redirection.js',
},
module: {
...defaultConfig.module,
rules: defaultConfig.module.rules.map( ( rule ) => {
// Find the sass-loader and configure it to suppress deprecation warnings
if (
rule &&
typeof rule === 'object' &&
rule.test &&
rule.test.toString().includes( 'scss' ) &&
Array.isArray( rule.use )
) {
return {
...rule,
use: rule.use.map( ( loader ) => {
// Handle both string and object loaders
if ( typeof loader === 'string' && loader.includes( 'sass-loader' ) ) {
return {
loader,
options: {
sassOptions: {
quietDeps: true,
silenceDeprecations: [ 'import' ],
},
},
};
}
if (
typeof loader === 'object' &&
loader &&
loader.loader &&
loader.loader.includes( 'sass-loader' )
) {
return {
...loader,
options: {
...( loader.options || {} ),
sassOptions: {
...( loader.options?.sassOptions || {} ),
quietDeps: true,
silenceDeprecations: [ 'import' ],
},
},
};
}
return loader;
} ),
};
}
return rule;
} ),
},
ignoreWarnings: [
{
module: /\.scss$/,
message: /Sass @import rules are deprecated/,
},
],
plugins: [
// Replace the default MiniCSSExtractPlugin and RtlCssPlugin with custom ones
...defaultConfig.plugins.filter( ( plugin ) => ! isDefaultCssPlugin( plugin ) ),
new MiniCSSExtractPlugin( { filename: 'redirection.css' } ),
new CustomRtlCssPlugin(),
new webpack.DefinePlugin( {
'process.env': { NODE_ENV: JSON.stringify( process.env.NODE_ENV || 'development' ) },
REDIRECTION_VERSION: "'" + pkg.version + "'",
} ),
// Add bundle analyzer when ANALYZE env var is set
...( process.env.ANALYZE ? [ new BundleAnalyzerPlugin() ] : [] ),
],
resolve: {
...defaultConfig.resolve,
alias: {
...defaultConfig.resolve.alias,
'@wp-plugin-components': path.resolve( __dirname, 'src/wp-plugin-components' ),
'@wp-plugin-lib': path.resolve( __dirname, 'src/wp-plugin-lib/' ),
lib: path.resolve( __dirname, 'src/lib/' ),
component: path.resolve( __dirname, 'src/component/' ),
state: path.resolve( __dirname, 'src/state/' ),
page: path.resolve( __dirname, 'src/page/' ),
app: path.resolve( __dirname, 'src/app/' ),
types: path.resolve( __dirname, 'src/types/' ),
stores: path.resolve( __dirname, 'src/stores/' ),
},
},
optimization: {
...defaultConfig.optimization,
usedExports: true,
sideEffects: true,
minimize: true,
minimizer: [
new TerserPlugin( {
parallel: true,
terserOptions: {
output: {
comments: /translators:/i,
},
compress: {
passes: 2,
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
},
extractComments: {
condition: true,
banner: () => {
return 'Redirection v' + pkg.version + ' - please refer to license.txt for license information';
},
},
} ),
],
},
};
module.exports = modified;