forked from LearnPress/learnpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
112 lines (96 loc) · 2.33 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
let globalOptions = {
namespace: '@learnpress',
library: 'LP',
};
/**
* Default request to global transformation
*
* Transform @wordpress dependencies:
*
* @wordpress/api-fetch -> wp.apiFetch
* @wordpress/i18n -> wp.i18n
*
* @param {string} request Requested module
* @param {Object} options
*
* @return {(string|string[]|undefined)} Script global
*/
function defaultRequestToExternal( request, options ) {
globalOptions = Object.assign(
{
namespace: '@learnpress',
library: 'LP',
},
options
);
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';
}
const namespace = globalOptions.namespace + '/';
if ( request.startsWith( namespace ) ) {
return [ globalOptions.library, camelCaseDash( request.substring( namespace.length ) ) ];
}
}
/**
* Default request to WordPress script handle transformation
*
* Transform @wordpress dependencies:
*
* @wordpress/i18n -> wp-i18n
* @wordpress/escape-html -> wp-escape-html
*
* @param {string} request Requested module
* @param {Object} options
*
* @return {(string|undefined)} Script handle
*/
function defaultRequestToHandle( request, options ) {
globalOptions = Object.assign(
{
namespace: '@learnpress',
library: 'LP',
},
options
);
switch ( request ) {
case '@babel/runtime/regenerator':
return 'wp-polyfill';
case 'lodash-es':
return 'lodash';
}
const namespace = globalOptions.namespace + '/';
if ( request.startsWith( namespace ) ) {
return ( globalOptions.library ) + '-' + request.substring( 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.
*
* Temporarily duplicated from @wordpress/scripts/utils.
*
* @param {string} string Input dash-delimited string.
*
* @return {string} Camel-cased string.
*/
function camelCaseDash( string ) {
return string.replace( /-([a-z])/g, ( match, letter ) => letter.toUpperCase() );
}
module.exports = {
defaultRequestToExternal,
defaultRequestToHandle,
};