This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
/
webpack.config.js
186 lines (175 loc) · 5.1 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
/**
* External dependencies
*/
const { get } = require( 'lodash' );
const path = require( 'path' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' );
const BundleAnalyzerPlugin = require( 'webpack-bundle-analyzer' )
.BundleAnalyzerPlugin;
const MomentTimezoneDataPlugin = require( 'moment-timezone-data-webpack-plugin' );
const ForkTsCheckerWebpackPlugin = require( 'fork-ts-checker-webpack-plugin' );
/**
* Internal dependencies
*/
const AsyncChunkSrcVersionParameterPlugin = require( './chunk-src-version-param' );
const UnminifyWebpackPlugin = require( './unminify' );
const { webpackConfig: styleConfig } = require( '@woocommerce/style-build' );
const WooCommerceDependencyExtractionWebpackPlugin = require( './packages/dependency-extraction-webpack-plugin/src/index' );
const NODE_ENV = process.env.NODE_ENV || 'development';
const WC_ADMIN_PHASE = process.env.WC_ADMIN_PHASE || 'development';
const wcAdminPackages = [
'components',
'csv-export',
'currency',
'customer-effort-score',
'date',
'experimental',
'explat',
'navigation',
'notices',
'number',
'data',
'tracks',
'onboarding',
];
const entryPoints = {};
wcAdminPackages.forEach( ( name ) => {
entryPoints[ name ] = `./packages/${ name }`;
} );
const wpAdminScripts = [
'marketing-coupons',
'navigation-opt-out',
'onboarding-homepage-notice',
'onboarding-product-notice',
'onboarding-product-import-notice',
'onboarding-tax-notice',
'print-shipping-label-banner',
'beta-features-tracking-modal',
'payment-method-promotions',
];
wpAdminScripts.forEach( ( name ) => {
entryPoints[ name ] = `./client/wp-admin-scripts/${ name }`;
} );
const suffix = WC_ADMIN_PHASE === 'core' ? '' : '.min';
const webpackConfig = {
mode: NODE_ENV,
entry: {
app: './client/index.js',
...entryPoints,
},
output: {
filename: ( data ) => {
return wpAdminScripts.includes( data.chunk.name )
? `wp-admin-scripts/[name]${ suffix }.js`
: `[name]/index${ suffix }.js`;
},
chunkFilename: `chunks/[name]${ suffix }.js`,
path: path.join( __dirname, 'dist' ),
library: [ 'wc', '[modulename]' ],
libraryTarget: 'window',
uniqueName: '__wcAdmin_webpackJsonp',
},
module: {
rules: [
{
test: /\.js$/,
parser: {
amd: false,
},
},
{
test: /\.(t|j)sx?$/,
exclude: [
// Exclude node_modules/ but not node_modules/debug* and node_modules/explat-client-react-helpers
// explat-client-react-helpers module contains optional chaining operators which need to be processed via babel loader for webpack 4.
// see webpack issue for details: https://github.com/webpack/webpack/issues/10227#issue-547480527
/node_modules(\/|\\)\.pnpm(\/|\\)(?!(debug|\@automattic\+explat-client-react-helpers))/,
],
use: {
loader: 'babel-loader',
options: {
presets: [
'@wordpress/babel-preset-default',
[
'@babel/preset-env',
{
corejs: '3',
useBuiltIns: 'usage',
},
],
[ '@babel/preset-typescript' ],
],
},
},
},
{ test: /\.md$/, use: 'raw-loader' },
{
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
loader: 'url-loader',
},
...styleConfig.rules,
],
},
resolve: {
fallback:{
'crypto': 'empty'
},
extensions: [ '.json', '.js', '.jsx', '.ts', '.tsx' ],
alias: {
'~': path.resolve( __dirname + '/client' ),
'gutenberg-components': path.resolve(
__dirname,
'node_modules/@wordpress/components/src'
),
},
},
plugins: [
...styleConfig.plugins,
new ForkTsCheckerWebpackPlugin(),
new CustomTemplatedPathPlugin( {
modulename( outputPath, data ) {
const entryName = get( data, [ 'chunk', 'name' ] );
if ( entryName ) {
return entryName.replace( /-([a-z])/g, ( match, letter ) =>
letter.toUpperCase()
);
}
return outputPath;
},
} ),
new CopyWebpackPlugin({
patterns: wcAdminPackages.map( ( packageName ) => ( {
from: `./packages/${ packageName }/build-style/*.css`,
to: `./${ packageName }/[name][ext]`,
noErrorOnMissing: true
} ) )
}
),
// We reuse this Webpack setup for Storybook, where we need to disable dependency extraction.
! process.env.STORYBOOK &&
new WooCommerceDependencyExtractionWebpackPlugin(),
new MomentTimezoneDataPlugin( {
startYear: 2000, // This strips out timezone data before the year 2000 to make a smaller file.
} ),
process.env.ANALYZE && new BundleAnalyzerPlugin(),
// Partially replace with __webpack_get_script_filename__ in app once using Webpack 5.x.
// The CSS chunk portion will need to remain, as it originates in MiniCssExtractPlugin.
new AsyncChunkSrcVersionParameterPlugin(),
WC_ADMIN_PHASE !== 'core' &&
new UnminifyWebpackPlugin( {
test: /\.js($|\?)/i,
mainEntry: 'app/index.min.js',
} ),
].filter( Boolean ),
optimization: {
minimize: NODE_ENV !== 'development',
splitChunks: {
name: false
}
},
};
if ( webpackConfig.mode !== 'production' && WC_ADMIN_PHASE !== 'core' ) {
webpackConfig.devtool = process.env.SOURCEMAP || 'source-map';
}
module.exports = webpackConfig;