forked from woocommerce/google-listings-and-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
170 lines (161 loc) · 5.03 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
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { hasArgInCLI } = require( '@wordpress/scripts/utils' );
const WooCommerceDependencyExtractionWebpackPlugin = require( '@woocommerce/dependency-extraction-webpack-plugin' );
const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const ReactRefreshWebpackPlugin = require( '@pmmmwh/react-refresh-webpack-plugin' );
const path = require( 'path' );
const isProduction = process.env.NODE_ENV === 'production';
const hasReactFastRefresh = hasArgInCLI( '--hot' ) && ! isProduction;
const exceptSVGAndPNGRule = ( rule ) => {
return ! rule.test.toString().match( /svg|png/i );
};
const webpackConfig = {
...defaultConfig,
module: {
...defaultConfig.module,
// Expose image assets as files.
rules: [
// Remove `@wordpress/` rules for SVGs.
...defaultConfig.module.rules.filter( exceptSVGAndPNGRule ),
{
test: /\.(svg|png|jpe?g|gif)$/i,
type: 'asset/resource',
generator: {
filename: 'images/[path][contenthash].[name][ext]',
},
},
// Fix module not found problem for `process/browser`.
// Ref: https://webpack.js.org/configuration/module/#resolvefullyspecified
{
test: /\.m?js$/,
resolve: { fullySpecified: false },
},
],
},
resolve: {
...defaultConfig.resolve,
alias: {
'.~': path.resolve( process.cwd(), 'js/src/' ),
},
fallback: {
/**
* Automatic polyfills for native node.js modules were removed from webpack v5.
* And `postcss` requires the `path` module, so here needs a polyfill.
*/
path: require.resolve( 'path-browserify' ),
},
},
plugins: [
...defaultConfig.plugins.filter( ( plugin ) => {
const filteredPlugins = [
// Filter WP/DEWP, as we will replace it with WC one.
'DependencyExtractionWebpackPlugin',
/**
* We don't use block.json to build the client files.
* And CopyPlugin will cause any changes to files in the '<rootDir>/src' folder
* to trigger an unwanted webpack rebuild.
*
* Ref:
* - https://github.com/WordPress/gutenberg/tree/%40wordpress/scripts%4022.1.0/packages/scripts#default-webpack-config
* - https://github.com/WordPress/gutenberg/blob/%40wordpress/scripts%4022.1.0/packages/scripts/config/webpack.config.js#L232-L240
*/
'MiniCssExtractPlugin',
'ReactRefreshPlugin',
];
return ! filteredPlugins.includes( plugin.constructor.name );
} ),
new WooCommerceDependencyExtractionWebpackPlugin( {
externalizedReport:
! hasReactFastRefresh && '../../.externalized.json',
} ),
new MiniCSSExtractPlugin( {
filename: '[name].css',
chunkFilename: '[name].css?ver=[chunkhash]',
} ),
],
entry: () => ( {
...defaultConfig.entry(),
index: path.resolve( process.cwd(), 'js/src', 'index.js' ),
'product-attributes': path.resolve(
process.cwd(),
'js/src/product-attributes',
'index.js'
),
blocks: path.join( __dirname, 'js/src/blocks/index.js' ),
'gtag-events': path.resolve(
process.cwd(),
'js/src/gtag-events',
'index.js'
),
} ),
output: {
...defaultConfig.output,
path: path.resolve( process.cwd(), 'js/build' ),
chunkFilename: '[name].js?ver=[chunkhash]',
},
optimization: {
...defaultConfig.optimization,
splitChunks: {
...defaultConfig.optimization.splitChunks,
cacheGroups: {
...defaultConfig.optimization.splitChunks.cacheGroups,
vendors: {
name: 'vendors',
test: /([\\/])node_modules\1/,
},
commons: {
name: 'commons',
test( { resource } ) {
return /([\\/])js\1src\1(components|data|hooks|images|utils|wcdl)\1/.test(
resource
);
},
},
},
},
},
};
if ( hasReactFastRefresh ) {
/**
* If the development environment uses HTTPS,
* it will fail when first connecting to webpack dev server,
* so turn off the `overlay` option here.
* Ref: https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/v0.5.4/docs/TROUBLESHOOTING.md#component-not-updating-with-bundle-splitting-techniques
*/
webpackConfig.plugins.push(
new ReactRefreshWebpackPlugin( { overlay: false } )
);
webpackConfig.optimization = {
...webpackConfig.optimization,
// With multiple entries, it will need a webpack runtime to be shared
// for all generated chunks when enabling Fast Refresh.
runtimeChunk: 'single',
};
}
const sassTest = /\.(sc|sa)ss$/;
const updatedSassOptions = {
sourceMap: ! isProduction,
sassOptions: {
includePaths: [ 'js/src/css/abstracts' ],
},
additionalData:
'@use "sass:color";' +
'@import "_colors"; ' +
'@import "_variables"; ' +
'@import "_mixins"; ' +
'@import "_breakpoints"; ',
};
// Update sass-loader config to prepend imports automatically
// like wc-admin, without rebuilding entire Rule config
webpackConfig.module.rules.forEach( ( { test, use }, ruleIndex ) => {
if ( test.toString() === sassTest.toString() ) {
use.forEach( ( { loader }, loaderIndex ) => {
if ( loader === require.resolve( 'sass-loader' ) ) {
webpackConfig.module.rules[ ruleIndex ].use[
loaderIndex
].options = updatedSassOptions;
}
} );
}
} );
module.exports = webpackConfig;