-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathutil.js
91 lines (78 loc) · 2.44 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
const WORDPRESS_NAMESPACE = '@wordpress/';
const BUNDLED_PACKAGES = [ '@wordpress/icons', '@wordpress/interface' ];
/**
* 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';
}
if ( BUNDLED_PACKAGES.includes( request ) ) {
return undefined;
}
if ( request.startsWith( WORDPRESS_NAMESPACE ) ) {
return [
'wp',
camelCaseDash( request.substring( WORDPRESS_NAMESPACE.length ) ),
];
}
}
/**
* 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';
}
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,
defaultRequestToHandle,
};