forked from woocommerce/woocommerce-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
265 lines (255 loc) · 6.93 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
/**
* External dependencies
*/
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const { get } = require( 'lodash' );
const path = require( 'path' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const { DefinePlugin } = require( 'webpack' );
const WebpackRTLPlugin = require( 'webpack-rtl-plugin' );
const FixStyleOnlyEntriesPlugin = require( 'webpack-fix-style-only-entries' );
const BundleAnalyzerPlugin = require( 'webpack-bundle-analyzer' )
.BundleAnalyzerPlugin;
const MomentTimezoneDataPlugin = require( 'moment-timezone-data-webpack-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const UnminifyWebpackPlugin = require( './unminify' );
/**
* WordPress dependencies
*/
const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' );
const NODE_ENV = process.env.NODE_ENV || 'development';
// generate-feature-config.php defaults to 'plugin', so lets match that here.
let WC_ADMIN_PHASE = process.env.WC_ADMIN_PHASE || 'plugin';
if ( [ 'development', 'plugin', 'core' ].indexOf( WC_ADMIN_PHASE ) === -1 ) {
WC_ADMIN_PHASE = 'plugin';
}
const WC_ADMIN_CONFIG = require( path.join(
__dirname,
'config',
WC_ADMIN_PHASE + '.json'
) );
const WC_ADMIN_ADDITIONAL_FEATURES =
( process.env.WC_ADMIN_ADDITIONAL_FEATURES &&
JSON.parse( process.env.WC_ADMIN_ADDITIONAL_FEATURES ) ) ||
{};
const externals = {
'@wordpress/api-fetch': { this: [ 'wp', 'apiFetch' ] },
'@wordpress/blocks': { this: [ 'wp', 'blocks' ] },
'@wordpress/data': { this: [ 'wp', 'data' ] },
'@wordpress/editor': { this: [ 'wp', 'editor' ] },
'@wordpress/element': { this: [ 'wp', 'element' ] },
'@wordpress/hooks': { this: [ 'wp', 'hooks' ] },
'@wordpress/url': { this: [ 'wp', 'url' ] },
'@wordpress/html-entities': { this: [ 'wp', 'htmlEntities' ] },
'@wordpress/i18n': { this: [ 'wp', 'i18n' ] },
'@wordpress/data-controls': { this: [ 'wp', 'dataControls' ] },
tinymce: 'tinymce',
moment: 'moment',
react: 'React',
lodash: 'lodash',
'react-dom': 'ReactDOM',
};
const wcAdminPackages = [
'components',
'csv-export',
'currency',
'date',
'navigation',
'number',
'data',
];
const entryPoints = {};
wcAdminPackages.forEach( ( name ) => {
externals[ `@woocommerce/${ name }` ] = {
this: [
'wc',
name.replace( /-([a-z])/g, ( match, letter ) =>
letter.toUpperCase()
),
],
};
entryPoints[ name ] = `./packages/${ name }`;
} );
const wpAdminScripts = [
'onboarding-homepage-notice',
'onboarding-product-notice',
'onboarding-product-import-notice',
'onboarding-tax-notice',
'print-shipping-label-banner',
];
wpAdminScripts.forEach( ( name ) => {
entryPoints[ name ] = `./client/wp-admin-scripts/${ name }`;
} );
const webpackConfig = {
mode: NODE_ENV,
entry: {
app: './client/index.js',
ie: './client/ie.scss',
...entryPoints,
},
output: {
filename: ( data ) => {
return wpAdminScripts.includes( data.chunk.name )
? `wp-admin-scripts/[name].min.js`
: `[name]/index.min.js`;
},
chunkFilename: `chunks/[name].[chunkhash].min.js`,
path: path.join( __dirname, 'dist' ),
library: [ 'wc', '[modulename]' ],
libraryTarget: 'this',
},
externals,
module: {
rules: [
{
parser: {
amd: false,
},
},
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.js?$/,
use: {
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{ loose: true, modules: 'commonjs' },
],
],
plugins: [ 'transform-es2015-template-literals' ],
},
},
include: new RegExp(
'/node_modules/(' +
'|acorn-jsx' +
'|d3-array' +
'|debug' +
'|marked' +
'|regexpu-core' +
'|unicode-match-property-ecmascript' +
'|unicode-match-property-value-ecmascript)/'
),
},
{ test: /\.md$/, use: 'raw-loader' },
{
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
loader: 'url-loader',
},
{
test: /\.s?css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
// postcss loader so we can use autoprefixer and theme Gutenberg components
loader: 'postcss-loader',
options: {
config: {
path: 'postcss.config.js',
},
},
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: [
'client/stylesheets/abstracts',
],
},
prependData:
'@import "node_modules/@wordpress/base-styles/_colors.scss"; ' +
'@import "node_modules/@wordpress/base-styles/_variables.scss"; ' +
'@import "node_modules/@wordpress/base-styles/_mixins.scss"; ' +
'@import "node_modules/@wordpress/base-styles/_breakpoints.scss"; ' +
'@import "node_modules/@wordpress/base-styles/_animations.scss"; ' +
'@import "node_modules/@wordpress/base-styles/_z-index.scss"; ' +
'@import "_colors"; ' +
'@import "_variables"; ' +
'@import "_breakpoints"; ' +
'@import "_mixins"; ',
},
},
],
},
],
},
resolve: {
extensions: [ '.json', '.js', '.jsx' ],
modules: [ path.join( __dirname, 'client' ), 'node_modules' ],
alias: {
'gutenberg-components': path.resolve(
__dirname,
'node_modules/@wordpress/components/src'
),
// @todo - remove once https://github.com/WordPress/gutenberg/pull/16196 is released.
'react-spring': 'react-spring/web.cjs',
'@woocommerce/wc-admin-settings': path.resolve(
__dirname,
'client/settings/index.js'
),
},
},
plugins: [
new FixStyleOnlyEntriesPlugin(),
// Inject the current feature flags.
new DefinePlugin( {
'window.wcAdminFeatures': {
...WC_ADMIN_CONFIG.features,
...WC_ADMIN_ADDITIONAL_FEATURES,
},
} ),
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 WebpackRTLPlugin( {
filename: './[name]/style-rtl.css',
minify: {
safe: true,
},
} ),
new MiniCssExtractPlugin( {
filename: './[name]/style.css',
} ),
new CopyWebpackPlugin(
wcAdminPackages.map( ( packageName ) => ( {
from: `./packages/${ packageName }/build-style/*.css`,
to: `./${ packageName }/`,
flatten: true,
transform: ( content ) => content,
} ) )
),
new MomentTimezoneDataPlugin( {
startYear: 2000, // This strips out timezone data before the year 2000 to make a smaller file.
} ),
process.env.ANALYZE && new BundleAnalyzerPlugin(),
new UnminifyWebpackPlugin( {
test: /\.js($|\?)/i,
mainEntry: 'app/index.min.js',
} ),
].filter( Boolean ),
optimization: {
minimize: NODE_ENV !== 'development',
minimizer: [
new TerserPlugin(),
],
},
};
if ( webpackConfig.mode !== 'production' ) {
webpackConfig.devtool = process.env.SOURCEMAP || 'source-map';
}
module.exports = webpackConfig;