forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
207 lines (195 loc) · 5.82 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
/**
* External dependencies
*/
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const { DefinePlugin } = require( 'webpack' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const postcss = require( 'postcss' );
const { get, escapeRegExp, compact } = require( 'lodash' );
const { basename, sep } = require( 'path' );
/**
* WordPress dependencies
*/
const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' );
const LibraryExportDefaultPlugin = require( '@wordpress/library-export-default-webpack-plugin' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const {
camelCaseDash,
} = require( '@wordpress/dependency-extraction-webpack-plugin/lib/util' );
/**
* Internal dependencies
*/
const { dependencies } = require( './package' );
const {
NODE_ENV: mode = 'development',
WP_DEVTOOL: devtool = mode === 'production' ? false : 'source-map',
} = process.env;
const WORDPRESS_NAMESPACE = '@wordpress/';
const BUNDLED_PACKAGES = [ '@wordpress/icons', '@wordpress/interface' ];
const gutenbergPackages = Object.keys( dependencies )
.filter(
( packageName ) =>
! BUNDLED_PACKAGES.includes( packageName ) &&
packageName.startsWith( WORDPRESS_NAMESPACE ) &&
! packageName.startsWith( WORDPRESS_NAMESPACE + 'react-native' )
)
.map( ( packageName ) => packageName.replace( WORDPRESS_NAMESPACE, '' ) );
module.exports = {
optimization: {
// Only concatenate modules in production, when not analyzing bundles.
concatenateModules:
mode === 'production' && ! process.env.WP_BUNDLE_ANALYZER,
},
mode,
entry: gutenbergPackages.reduce( ( memo, packageName ) => {
const name = camelCaseDash( packageName );
memo[ name ] = `./packages/${ packageName }`;
return memo;
}, {} ),
output: {
devtoolNamespace: 'wp',
filename: './build/[basename]/index.js',
path: __dirname,
library: [ 'wp', '[name]' ],
libraryTarget: 'this',
},
module: {
rules: compact( [
mode !== 'production' && {
test: /\.js$/,
use: require.resolve( 'source-map-loader' ),
enforce: 'pre',
},
] ),
},
plugins: [
// The WP_BUNDLE_ANALYZER global variable enables a utility that represents bundle
// content as a convenient interactive zoomable treemap.
process.env.WP_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(),
new DefinePlugin( {
// Inject the `GUTENBERG_PHASE` global, used for feature flagging.
'process.env.GUTENBERG_PHASE': JSON.stringify(
parseInt(
process.env.npm_package_config_GUTENBERG_PHASE,
10
) || 1
),
'process.env.FORCE_REDUCED_MOTION': JSON.stringify(
process.env.FORCE_REDUCED_MOTION
),
} ),
new CustomTemplatedPathPlugin( {
basename( path, data ) {
let rawRequest;
const entryModule = get( data, [ 'chunk', 'entryModule' ], {} );
switch ( entryModule.type ) {
case 'javascript/auto':
rawRequest = entryModule.rawRequest;
break;
case 'javascript/esm':
rawRequest = entryModule.rootModule.rawRequest;
break;
}
if ( rawRequest ) {
return basename( rawRequest );
}
return path;
},
} ),
new LibraryExportDefaultPlugin(
[
'api-fetch',
'deprecated',
'dom-ready',
'redux-routine',
'token-list',
'server-side-render',
'shortcode',
'warning',
].map( camelCaseDash )
),
new CopyWebpackPlugin(
gutenbergPackages.map( ( packageName ) => ( {
from: `./packages/${ packageName }/build-style/*.css`,
to: `./build/${ packageName }/`,
flatten: true,
transform: ( content ) => {
if ( mode === 'production' ) {
return postcss( [
require( 'cssnano' )( {
preset: [
'default',
{
discardComments: {
removeAll: true,
},
},
],
} ),
] )
.process( content, {
from: 'src/app.css',
to: 'dest/app.css',
} )
.then( ( result ) => result.css );
}
return content;
},
} ) )
),
new CopyWebpackPlugin( [
{
from: './packages/block-library/src/**/index.php',
test: new RegExp(
`([\\w-]+)${ escapeRegExp( sep ) }index\\.php$`
),
to: 'build/block-library/blocks/[1].php',
transform( content ) {
content = content.toString();
// Within content, search for any function definitions. For
// each, replace every other reference to it in the file.
return (
content
.match( /^function [^\(]+/gm )
.reduce( ( result, functionName ) => {
// Trim leading "function " prefix from match.
functionName = functionName.slice( 9 );
// Prepend the Gutenberg prefix, substituting any
// other core prefix (e.g. "wp_").
return result.replace(
new RegExp( functionName, 'g' ),
( match ) =>
'gutenberg_' +
match.replace( /^wp_/, '' )
);
}, content )
// The core blocks override procedure takes place in
// the init action default priority to ensure that core
// blocks would have been registered already. Since the
// blocks implementations occur at the default priority
// and due to WordPress hooks behavior not considering
// mutations to the same priority during another's
// callback, the Gutenberg build blocks are modified
// to occur at a later priority.
.replace(
/(add_action\(\s*'init',\s*'gutenberg_register_block_[^']+'(?!,))/,
'$1, 20'
)
);
},
},
{
from: './packages/block-library/src/*/block.json',
test: new RegExp(
`([\\w-]+)${ escapeRegExp( sep ) }block\\.json$`
),
to: 'build/block-library/blocks/[1]/block.json',
},
] ),
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
].filter( Boolean ),
watchOptions: {
ignored: '!packages/*/!(src)/**/*',
},
devtool,
};