2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-06-10 23:06:17 +00:00
|
|
|
const MiniCssExtractPlugin = require( '@automattic/mini-css-extract-plugin-with-rtl' );
|
2018-11-06 21:53:22 +00:00
|
|
|
const { get } = require( 'lodash' );
|
|
|
|
const path = require( 'path' );
|
2018-11-15 18:16:23 +00:00
|
|
|
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
|
2019-03-14 10:18:54 +00:00
|
|
|
const WebpackRTLPlugin = require( 'webpack-rtl-plugin' );
|
2019-10-08 22:02:26 +00:00
|
|
|
const FixStyleOnlyEntriesPlugin = require( 'webpack-fix-style-only-entries' );
|
2020-04-29 18:01:27 +00:00
|
|
|
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' );
|
2020-10-06 12:58:15 +00:00
|
|
|
const AsyncChunkSrcVersionParameterPlugin = require( './chunk-src-version-param' );
|
2021-03-01 03:01:22 +00:00
|
|
|
const ForkTsCheckerWebpackPlugin = require( 'fork-ts-checker-webpack-plugin' );
|
2021-02-28 23:48:52 +00:00
|
|
|
const WooCommerceDependencyExtractionWebpackPlugin = require( './packages/dependency-extraction-webpack-plugin/src/index' );
|
2018-11-06 21:53:22 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-13 02:05:22 +00:00
|
|
|
* External dependencies
|
2018-11-06 21:53:22 +00:00
|
|
|
*/
|
|
|
|
const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' );
|
|
|
|
|
2018-07-23 20:14:40 +00:00
|
|
|
const NODE_ENV = process.env.NODE_ENV || 'development';
|
2020-06-22 12:38:17 +00:00
|
|
|
const WC_ADMIN_PHASE = process.env.WC_ADMIN_PHASE || 'development';
|
2018-04-17 21:38:56 +00:00
|
|
|
|
2018-11-15 18:16:23 +00:00
|
|
|
const wcAdminPackages = [
|
|
|
|
'components',
|
|
|
|
'csv-export',
|
|
|
|
'currency',
|
2020-10-30 06:52:52 +00:00
|
|
|
'customer-effort-score',
|
2018-11-15 18:16:23 +00:00
|
|
|
'date',
|
2021-04-16 03:34:15 +00:00
|
|
|
'experimental',
|
|
|
|
'explat',
|
2018-11-15 18:16:23 +00:00
|
|
|
'navigation',
|
2020-11-09 07:17:08 +00:00
|
|
|
'notices',
|
2019-01-29 16:48:46 +00:00
|
|
|
'number',
|
2020-03-25 03:20:17 +00:00
|
|
|
'data',
|
2020-08-20 04:59:52 +00:00
|
|
|
'tracks',
|
2021-05-28 23:26:38 +00:00
|
|
|
'onboarding',
|
2018-11-15 18:16:23 +00:00
|
|
|
];
|
2018-10-30 18:57:48 +00:00
|
|
|
|
2018-11-15 18:16:23 +00:00
|
|
|
const entryPoints = {};
|
2020-02-14 02:23:21 +00:00
|
|
|
wcAdminPackages.forEach( ( name ) => {
|
2018-11-15 18:16:23 +00:00
|
|
|
entryPoints[ name ] = `./packages/${ name }`;
|
2018-10-30 18:57:48 +00:00
|
|
|
} );
|
|
|
|
|
2019-10-21 18:13:25 +00:00
|
|
|
const wpAdminScripts = [
|
2020-06-16 02:30:41 +00:00
|
|
|
'marketing-coupons',
|
2020-12-16 14:08:14 +00:00
|
|
|
'navigation-opt-out',
|
2019-10-21 18:13:25 +00:00
|
|
|
'onboarding-homepage-notice',
|
2019-11-12 01:17:36 +00:00
|
|
|
'onboarding-product-notice',
|
2019-12-10 19:28:19 +00:00
|
|
|
'onboarding-product-import-notice',
|
2019-11-07 00:17:46 +00:00
|
|
|
'onboarding-tax-notice',
|
2020-03-27 20:42:58 +00:00
|
|
|
'print-shipping-label-banner',
|
2021-01-13 19:43:45 +00:00
|
|
|
'beta-features-tracking-modal',
|
2019-10-21 18:13:25 +00:00
|
|
|
];
|
2020-02-14 02:23:21 +00:00
|
|
|
wpAdminScripts.forEach( ( name ) => {
|
2019-10-21 18:13:25 +00:00
|
|
|
entryPoints[ name ] = `./client/wp-admin-scripts/${ name }`;
|
|
|
|
} );
|
|
|
|
|
2020-06-15 02:00:25 +00:00
|
|
|
const postcssPlugins = require( '@wordpress/postcss-plugins-preset' );
|
|
|
|
|
2020-09-15 15:36:58 +00:00
|
|
|
const suffix = WC_ADMIN_PHASE === 'core' ? '' : '.min';
|
|
|
|
|
2018-04-17 21:38:56 +00:00
|
|
|
const webpackConfig = {
|
|
|
|
mode: NODE_ENV,
|
2018-05-04 14:44:19 +00:00
|
|
|
entry: {
|
2018-10-30 18:57:48 +00:00
|
|
|
app: './client/index.js',
|
2018-11-15 18:16:23 +00:00
|
|
|
...entryPoints,
|
2018-05-04 14:44:19 +00:00
|
|
|
},
|
2018-04-17 21:38:56 +00:00
|
|
|
output: {
|
2019-10-21 18:13:25 +00:00
|
|
|
filename: ( data ) => {
|
2020-02-14 02:23:21 +00:00
|
|
|
return wpAdminScripts.includes( data.chunk.name )
|
2020-09-15 15:36:58 +00:00
|
|
|
? `wp-admin-scripts/[name]${ suffix }.js`
|
|
|
|
: `[name]/index${ suffix }.js`;
|
2019-10-21 18:13:25 +00:00
|
|
|
},
|
2020-10-06 12:58:15 +00:00
|
|
|
chunkFilename: `chunks/[name]${ suffix }.js`,
|
2020-04-29 18:01:27 +00:00
|
|
|
path: path.join( __dirname, 'dist' ),
|
2018-11-06 21:53:22 +00:00
|
|
|
library: [ 'wc', '[modulename]' ],
|
2018-04-17 21:38:56 +00:00
|
|
|
libraryTarget: 'this',
|
2020-07-09 02:10:34 +00:00
|
|
|
jsonpFunction: '__wcAdmin_webpackJsonp',
|
2018-04-17 21:38:56 +00:00
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
2018-12-06 22:08:40 +00:00
|
|
|
{
|
|
|
|
parser: {
|
|
|
|
amd: false,
|
|
|
|
},
|
|
|
|
},
|
2018-12-26 02:46:32 +00:00
|
|
|
{
|
2021-03-01 03:01:22 +00:00
|
|
|
test: /\.(t|j)sx?$/,
|
2021-03-24 21:29:37 +00:00
|
|
|
exclude: [
|
|
|
|
/node_modules(\/|\\)(?!(debug))/,
|
|
|
|
/build/,
|
|
|
|
/build-module/,
|
|
|
|
],
|
2018-12-26 02:46:32 +00:00
|
|
|
use: {
|
|
|
|
loader: 'babel-loader',
|
|
|
|
options: {
|
2021-02-24 01:24:22 +00:00
|
|
|
presets: [
|
|
|
|
'@wordpress/babel-preset-default',
|
|
|
|
[
|
|
|
|
'@babel/preset-env',
|
|
|
|
{
|
|
|
|
corejs: '3',
|
|
|
|
useBuiltIns: 'usage',
|
|
|
|
},
|
|
|
|
],
|
2021-03-01 03:01:22 +00:00
|
|
|
[ '@babel/preset-typescript' ],
|
2021-02-24 01:24:22 +00:00
|
|
|
],
|
2018-12-26 02:46:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-24 15:36:35 +00:00
|
|
|
{ test: /\.md$/, use: 'raw-loader' },
|
2019-05-28 14:05:55 +00:00
|
|
|
{
|
|
|
|
test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
|
|
|
|
loader: 'url-loader',
|
|
|
|
},
|
2018-04-17 21:38:56 +00:00
|
|
|
{
|
2018-11-15 18:16:23 +00:00
|
|
|
test: /\.s?css$/,
|
2021-05-26 19:35:46 +00:00
|
|
|
exclude: [ /storybook\/wordpress/, /build-style\/*\/*.css/ ],
|
2019-01-15 10:55:54 +00:00
|
|
|
use: [
|
|
|
|
MiniCssExtractPlugin.loader,
|
|
|
|
'css-loader',
|
|
|
|
{
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
2020-06-15 02:00:25 +00:00
|
|
|
ident: 'postcss',
|
|
|
|
plugins: postcssPlugins,
|
2018-07-30 14:05:22 +00:00
|
|
|
},
|
2019-01-15 10:55:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'sass-loader',
|
2020-03-24 01:15:45 +00:00
|
|
|
options: {
|
|
|
|
sassOptions: {
|
|
|
|
includePaths: [
|
2021-06-02 03:54:00 +00:00
|
|
|
path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'client/stylesheets/abstracts'
|
|
|
|
),
|
2020-03-24 01:15:45 +00:00
|
|
|
],
|
|
|
|
},
|
2021-06-02 03:54:00 +00:00
|
|
|
webpackImporter: true,
|
|
|
|
additionalData:
|
2019-01-15 10:55:54 +00:00
|
|
|
'@import "_colors"; ' +
|
|
|
|
'@import "_variables"; ' +
|
|
|
|
'@import "_breakpoints"; ' +
|
|
|
|
'@import "_mixins"; ',
|
2018-05-14 13:41:30 +00:00
|
|
|
},
|
2019-01-15 10:55:54 +00:00
|
|
|
},
|
|
|
|
],
|
2018-04-17 21:38:56 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2018-05-10 16:35:46 +00:00
|
|
|
resolve: {
|
2021-03-01 03:01:22 +00:00
|
|
|
extensions: [ '.json', '.js', '.jsx', '.ts', '.tsx' ],
|
2018-07-20 03:40:15 +00:00
|
|
|
alias: {
|
2021-03-10 15:11:37 +00:00
|
|
|
'~': path.resolve( __dirname + '/client' ),
|
2020-02-14 02:23:21 +00:00
|
|
|
'gutenberg-components': path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'node_modules/@wordpress/components/src'
|
|
|
|
),
|
2019-09-23 21:47:08 +00:00
|
|
|
'@woocommerce/wc-admin-settings': path.resolve(
|
|
|
|
__dirname,
|
2021-02-12 19:32:42 +00:00
|
|
|
'client/wc-admin-settings/index.js'
|
2019-09-23 21:47:08 +00:00
|
|
|
),
|
2018-07-20 03:40:15 +00:00
|
|
|
},
|
2018-05-10 16:35:46 +00:00
|
|
|
},
|
2018-10-30 18:57:48 +00:00
|
|
|
plugins: [
|
2021-03-01 03:01:22 +00:00
|
|
|
new ForkTsCheckerWebpackPlugin(),
|
2019-10-08 22:02:26 +00:00
|
|
|
new FixStyleOnlyEntriesPlugin(),
|
2018-11-06 21:53:22 +00:00
|
|
|
new CustomTemplatedPathPlugin( {
|
|
|
|
modulename( outputPath, data ) {
|
|
|
|
const entryName = get( data, [ 'chunk', 'name' ] );
|
|
|
|
if ( entryName ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
return entryName.replace( /-([a-z])/g, ( match, letter ) =>
|
|
|
|
letter.toUpperCase()
|
|
|
|
);
|
2018-11-06 21:53:22 +00:00
|
|
|
}
|
|
|
|
return outputPath;
|
|
|
|
},
|
|
|
|
} ),
|
2019-03-14 10:18:54 +00:00
|
|
|
new WebpackRTLPlugin( {
|
2019-03-20 02:55:43 +00:00
|
|
|
minify: {
|
|
|
|
safe: true,
|
|
|
|
},
|
2019-03-14 10:18:54 +00:00
|
|
|
} ),
|
2019-01-15 10:55:54 +00:00
|
|
|
new MiniCssExtractPlugin( {
|
2020-04-29 18:01:27 +00:00
|
|
|
filename: './[name]/style.css',
|
2020-06-10 22:41:27 +00:00
|
|
|
chunkFilename: './chunks/[id].style.css',
|
2020-06-10 23:06:17 +00:00
|
|
|
rtlEnabled: true,
|
2018-10-30 18:57:48 +00:00
|
|
|
} ),
|
2018-11-15 18:16:23 +00:00
|
|
|
new CopyWebpackPlugin(
|
2020-02-14 02:23:21 +00:00
|
|
|
wcAdminPackages.map( ( packageName ) => ( {
|
2018-11-15 18:16:23 +00:00
|
|
|
from: `./packages/${ packageName }/build-style/*.css`,
|
2020-04-29 18:01:27 +00:00
|
|
|
to: `./${ packageName }/`,
|
2018-11-15 18:16:23 +00:00
|
|
|
flatten: true,
|
2020-02-14 02:23:21 +00:00
|
|
|
transform: ( content ) => content,
|
2018-11-15 18:16:23 +00:00
|
|
|
} ) )
|
|
|
|
),
|
2021-06-02 03:54:00 +00:00
|
|
|
|
|
|
|
// We reuse this Webpack setup for Storybook, where we need to disable dependency extraction.
|
|
|
|
! process.env.STORYBOOK &&
|
|
|
|
new WooCommerceDependencyExtractionWebpackPlugin(),
|
2020-04-29 18:01:27 +00:00
|
|
|
new MomentTimezoneDataPlugin( {
|
|
|
|
startYear: 2000, // This strips out timezone data before the year 2000 to make a smaller file.
|
|
|
|
} ),
|
|
|
|
process.env.ANALYZE && new BundleAnalyzerPlugin(),
|
2020-10-06 12:58:15 +00:00
|
|
|
// 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(),
|
2020-07-28 02:32:58 +00:00
|
|
|
WC_ADMIN_PHASE !== 'core' &&
|
|
|
|
new UnminifyWebpackPlugin( {
|
|
|
|
test: /\.js($|\?)/i,
|
|
|
|
mainEntry: 'app/index.min.js',
|
|
|
|
} ),
|
2020-04-29 18:01:27 +00:00
|
|
|
].filter( Boolean ),
|
|
|
|
optimization: {
|
|
|
|
minimize: NODE_ENV !== 'development',
|
2020-06-10 01:55:06 +00:00
|
|
|
minimizer: [ new TerserPlugin() ],
|
2020-10-06 12:58:15 +00:00
|
|
|
splitChunks: {
|
|
|
|
name: false,
|
|
|
|
},
|
2020-04-29 18:01:27 +00:00
|
|
|
},
|
2020-12-02 01:17:02 +00:00
|
|
|
node: {
|
|
|
|
crypto: 'empty',
|
|
|
|
},
|
2018-04-17 21:38:56 +00:00
|
|
|
};
|
|
|
|
|
2020-07-28 02:32:58 +00:00
|
|
|
if ( webpackConfig.mode !== 'production' && WC_ADMIN_PHASE !== 'core' ) {
|
2018-04-17 23:51:48 +00:00
|
|
|
webpackConfig.devtool = process.env.SOURCEMAP || 'source-map';
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:38:56 +00:00
|
|
|
module.exports = webpackConfig;
|