-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathpackages.js
More file actions
187 lines (179 loc) · 5 KB
/
packages.js
File metadata and controls
187 lines (179 loc) · 5 KB
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
/**
* External dependencies
*/
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const MomentTimezoneDataPlugin = require( 'moment-timezone-data-webpack-plugin' );
const { join } = require( 'path' );
/**
* WordPress dependencies
*/
const {
camelCaseDash,
} = require( '@wordpress/dependency-extraction-webpack-plugin/lib/util' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
/**
* Internal dependencies
*/
const { dependencies } = require( '../../package' );
const { baseConfig, plugins, stylesTransform } = require( './shared' );
const WORDPRESS_NAMESPACE = '@wordpress/';
// Experimental or other packages that should be private are bundled when used.
// That way, we can iterate on these package without making them part of the public API.
// See: https://github.com/WordPress/gutenberg/pull/19809
//
// !!
// This list must be kept in sync with the matching list in packages/dependency-extraction-webpack-plugin/lib/util.js
// !!
const BUNDLED_PACKAGES = [
'@wordpress/dataviews',
'@wordpress/icons',
'@wordpress/interface',
'@wordpress/sync',
'@wordpress/undo-manager',
];
// PHP files in packages that have to be copied during build.
const bundledPackagesPhpConfig = [
{
from: './packages/style-engine/',
to: 'build/style-engine/',
replaceClasses: [
'WP_Style_Engine_CSS_Declarations',
'WP_Style_Engine_CSS_Rules_Store',
'WP_Style_Engine_CSS_Rule',
'WP_Style_Engine_Processor',
'WP_Style_Engine',
],
},
].map( ( { from, to, replaceClasses } ) => ( {
from: `${ from }/*.php`,
to( { absoluteFilename } ) {
const [ , filename ] = absoluteFilename.match(
/([\w-]+)(\.php){1,1}$/
);
return join( to, `${ filename }-gutenberg.php` );
},
transform: ( content ) => {
const classSuffix = '_Gutenberg';
const functionPrefix = 'gutenberg_';
content = content.toString();
// Replace class names.
content = content.replace(
new RegExp( replaceClasses.join( '|' ), 'g' ),
( match ) => `${ match }${ classSuffix }`
);
// Replace function names.
content = Array.from(
content.matchAll( /^\s*function ([^\(]+)/gm )
).reduce( ( result, [ , functionName ] ) => {
// Prepend the Gutenberg prefix, substituting any
// other core prefix (e.g. "wp_").
return result.replace(
new RegExp( functionName, 'g' ),
( match ) => functionPrefix + match.replace( /^wp_/, '' )
);
}, content );
return content;
},
} ) );
const gutenbergPackages = Object.keys( dependencies )
.filter(
( packageName ) =>
! BUNDLED_PACKAGES.includes( packageName ) &&
packageName.startsWith( WORDPRESS_NAMESPACE ) &&
! packageName.startsWith( WORDPRESS_NAMESPACE + 'react-native' ) &&
! packageName.startsWith( WORDPRESS_NAMESPACE + 'interactivity' )
)
.map( ( packageName ) => packageName.replace( WORDPRESS_NAMESPACE, '' ) );
const exportDefaultPackages = [
'api-fetch',
'deprecated',
'dom-ready',
'redux-routine',
'token-list',
'server-side-render',
'shortcode',
'warning',
];
const vendors = {
react: [
'react/umd/react.development.js',
'react/umd/react.production.min.js',
],
'react-dom': [
'react-dom/umd/react-dom.development.js',
'react-dom/umd/react-dom.production.min.js',
],
'inert-polyfill': [
'wicg-inert/dist/inert.js',
'wicg-inert/dist/inert.min.js',
],
};
const vendorsCopyConfig = Object.entries( vendors ).flatMap(
( [ key, [ devFilename, prodFilename ] ] ) => {
return [
{
from: `node_modules/${ devFilename }`,
to: `build/vendors/${ key }.js`,
},
{
from: `node_modules/${ prodFilename }`,
to: `build/vendors/${ key }.min.js`,
},
];
}
);
module.exports = {
...baseConfig,
name: 'packages',
entry: Object.fromEntries(
gutenbergPackages.map( ( packageName ) => [
packageName,
{
import: `./packages/${ packageName }`,
library: {
name: [ 'wp', camelCaseDash( packageName ) ],
type: 'window',
export: exportDefaultPackages.includes( packageName )
? 'default'
: undefined,
},
},
] )
),
output: {
devtoolNamespace: 'wp',
filename: './build/[name]/index.min.js',
path: join( __dirname, '..', '..' ),
devtoolModuleFilenameTemplate: ( info ) => {
if ( info.resourcePath.includes( '/@wordpress/' ) ) {
const resourcePath =
info.resourcePath.split( '/@wordpress/' )[ 1 ];
return `../../packages/${ resourcePath }`;
}
return `webpack://${ info.namespace }/${ info.resourcePath }`;
},
},
performance: {
hints: false, // disable warnings about package sizes
},
plugins: [
...plugins,
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
new CopyWebpackPlugin( {
patterns: gutenbergPackages
.map( ( packageName ) => ( {
from: '*.css',
context: `./packages/${ packageName }/build-style`,
to: `./build/${ packageName }`,
transform: stylesTransform,
noErrorOnMissing: true,
} ) )
.concat( bundledPackagesPhpConfig )
.concat( vendorsCopyConfig ),
} ),
new MomentTimezoneDataPlugin( {
startYear: 2000,
endYear: 2040,
} ),
].filter( Boolean ),
};