-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
util.js
156 lines (134 loc) · 4.4 KB
/
util.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
const WORDPRESS_NAMESPACE = '@wordpress/';
// !!
// This list must be kept in sync with the same list in tools/webpack/packages.js
// !!
const BUNDLED_PACKAGES = [
'@wordpress/dataviews',
'@wordpress/icons',
'@wordpress/interface',
'@wordpress/sync',
'@wordpress/undo-manager',
];
/**
* Default request to global transformation
*
* Transform @wordpress dependencies:
* - request `@wordpress/api-fetch` becomes `[ 'wp', 'apiFetch' ]`
* - request `@wordpress/i18n` becomes `[ 'wp', 'i18n' ]`
*
* @param {string} request Module request (the module name in `import from`) to be transformed
* @return {string|string[]|undefined} The resulting external definition. Return `undefined`
* to ignore the request. Return `string|string[]` to map the request to an external.
*/
function defaultRequestToExternal( request ) {
switch ( request ) {
case 'moment':
return request;
case '@babel/runtime/regenerator':
return 'regeneratorRuntime';
case 'lodash':
case 'lodash-es':
return 'lodash';
case 'jquery':
return 'jQuery';
case 'react':
return 'React';
case 'react-dom':
return 'ReactDOM';
case 'react/jsx-runtime':
case 'react/jsx-dev-runtime':
return 'ReactJSXRuntime';
}
if ( request.includes( 'react-refresh/runtime' ) ) {
return 'ReactRefreshRuntime';
}
if ( BUNDLED_PACKAGES.includes( request ) ) {
return undefined;
}
if ( request.startsWith( WORDPRESS_NAMESPACE ) ) {
return [
'wp',
camelCaseDash( request.substring( WORDPRESS_NAMESPACE.length ) ),
];
}
}
/**
* Default request to external module transformation
*
* Currently only @wordpress/interactivity and `@wordpress/interactivity-router`
* are supported.
*
* Do not use the boolean shorthand here, it's only handled for the
* `requestToExternalModule` option.
*
* @param {string} request Module request (the module name in `import from`) to be transformed
* @return {string|Error|undefined} The resulting external definition.
* - Return `undefined` to ignore the request (do not externalize).
* - Return `string` to map the request to an external.
* - Return `Error` to emit an error.
*/
function defaultRequestToExternalModule( request ) {
if ( request === '@wordpress/interactivity' ) {
// This is a special case. Interactivity does not support dynamic imports at
// this time. We add the external "module" type to indicate that webpack
// should externalize this as a module (instead of our default `import()`
// external type) which forces @wordpress/interactivity imports to be
// hoisted to static imports.
return `module ${ request }`;
}
if ( request === '@wordpress/interactivity-router' ) {
// Assumes this is usually going to be used as a dynamic import.
return `import ${ request }`;
}
const isWordPressScript = Boolean( defaultRequestToExternal( request ) );
if ( isWordPressScript ) {
throw new Error(
`Attempted to use WordPress script in a module: ${ request }, which is not supported yet.`
);
}
}
/**
* Default request to WordPress script handle transformation
*
* Transform @wordpress dependencies:
* - request `@wordpress/i18n` becomes `wp-i18n`
* - request `@wordpress/escape-html` becomes `wp-escape-html`
*
* @param {string} request Module request (the module name in `import from`) to be transformed
* @return {string|undefined} WordPress script handle to map the request to. Return `undefined`
* to use the same name as the module.
*/
function defaultRequestToHandle( request ) {
switch ( request ) {
case '@babel/runtime/regenerator':
return 'wp-polyfill';
case 'lodash-es':
return 'lodash';
case 'react/jsx-runtime':
return 'react-jsx-runtime';
}
if ( request.includes( 'react-refresh/runtime' ) ) {
return 'wp-react-refresh-runtime';
}
if ( request.startsWith( WORDPRESS_NAMESPACE ) ) {
return 'wp-' + request.substring( WORDPRESS_NAMESPACE.length );
}
}
/**
* Given a string, returns a new string with dash separators converted to
* camelCase equivalent. This is not as aggressive as `_.camelCase` in
* converting to uppercase, where Lodash will also capitalize letters
* following numbers.
*
* @param {string} string Input dash-delimited string.
* @return {string} Camel-cased string.
*/
function camelCaseDash( string ) {
return string.replace( /-([a-z])/g, ( _, letter ) => letter.toUpperCase() );
}
module.exports = {
camelCaseDash,
defaultRequestToExternal,
defaultRequestToExternalModule,
defaultRequestToHandle,
};