This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
98 lines (93 loc) · 2.41 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
/**
* External dependencies
*/
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const CopyPlugin = require( 'copy-webpack-plugin' );
const { resolve, basename, dirname } = require( 'path' );
/**
* WordPress dependencies
*/
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const isProduction = process.env.NODE_ENV === 'production';
const mode = isProduction ? 'production' : 'development';
const config = {
mode,
entry: {
index: resolve( process.cwd(), 'src/', 'index.js' ),
editor: resolve( process.cwd(), 'src/', 'editor.js' ),
},
output: {
filename: '[name].js',
path: resolve( process.cwd(), 'build' ),
},
resolve: {
alias: {
'lodash-es': 'lodash',
},
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: require.resolve( 'babel-loader' ),
options: {
// Babel uses a directory within local node_modules
// by default. Use the environment variable option
// to enable more persistent caching.
cacheDirectory:
process.env.BABEL_CACHE_DIRECTORY || true,
presets: [
require.resolve(
'@wordpress/babel-preset-default'
),
],
},
},
},
],
},
plugins: [
// WP_BUNDLE_ANALYZER global variable enables utility that represents
// bundle content as a convenient interactive zoomable treemap.
process.env.WP_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(),
new CopyPlugin( {
patterns: [
{
from: './src/*/**/index.php',
to( { absoluteFilename } ) {
const dir = basename( dirname( absoluteFilename ) );
const parent = basename(
dirname( dirname( absoluteFilename ) )
);
return `${ parent }/${ dir }[ext]`;
},
},
{
from: './src/images/**/*',
to: 'images/[name][ext]',
},
{
from: './src/lib/fonts/**/*',
to: 'fonts/[name][ext]',
},
],
} ),
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
].filter( Boolean ),
stats: {
children: false,
},
};
if ( ! isProduction ) {
// WP_DEVTOOL global variable controls how source maps are generated.
// See: https://webpack.js.org/configuration/devtool/#devtool.
config.devtool = process.env.WP_DEVTOOL || 'source-map';
config.module.rules.unshift( {
test: /\.js$/,
use: require.resolve( 'source-map-loader' ),
enforce: 'pre',
} );
}
module.exports = config;