Repository navigation
Expand file tree
/
Copy pathwebpack-dependency-mapping.js
More file actions
228 lines (203 loc) · 6.96 KB
/
Copy pathwebpack-dependency-mapping.js
File metadata and controls
228 lines (203 loc) · 6.96 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* This file contains mappings for external and handle names for packages.
*
* @see https://github.com/woocommerce/woocommerce/blob/trunk/packages/js/dependency-extraction-webpack-plugin/src/index.js#L41
*
* @type {string}
*/
const { kebabCase } = require( 'lodash' );
const WOOCOMMERCE_NAMESPACE = '@woocommerce/';
/**
* Third-party UI packages that are shared across multiple entry points.
*
* Each is built once as a standalone bundle exposed on a `window.dokan.*` global, so consuming
* bundles reference the global instead of inlining their own copy of the package.
*
* @since DOKAN_SINCE
*
* @type {Object.<string, {external: string[], handle: string}>}
*/
const SHARED_UI_EXTERNALS = {
'@wedevs/plugin-ui': {
external: [ 'dokan', 'pluginUI' ],
handle: 'dokan-plugin-ui',
},
'@getdokan/dokan-ui': {
external: [ 'dokan', 'dokanUI' ],
handle: 'dokan-ui',
},
};
const packages = [
// wc-admin packages
'@woocommerce/admin-layout',
'@woocommerce/block-templates',
'@woocommerce/components',
'@woocommerce/csv-export',
'@woocommerce/currency',
'@woocommerce/customer-effort-score',
'@woocommerce/data',
'@woocommerce/date',
'@woocommerce/dependency-extraction-webpack-plugin',
'@woocommerce/eslint-plugin',
'@woocommerce/experimental',
'@woocommerce/explat',
'@woocommerce/extend-cart-checkout-block',
'@woocommerce/navigation',
'@woocommerce/notices',
'@woocommerce/number',
'@woocommerce/product-editor',
'@woocommerce/settings-editor',
'@woocommerce/tracks',
'@woocommerce/remote-logging',
// wc-blocks packages
'@woocommerce/blocks-checkout',
'@woocommerce/blocks-components',
'@woocommerce/block-data',
'@woocommerce/blocks-registry',
'@woocommerce/price-format',
'@woocommerce/settings',
];
/**
* 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.
*
* @since DOKAN_SINCE
*
* @see https://github.com/woocommerce/woocommerce/blob/34b5e8b5d63a7d9df6622dc3af2aeaf428160357/packages/js/dependency-extraction-webpack-plugin/src/index.js#L16
*
* @param {string} string Input dash-delimited string.
*
* @return {string} Camel-cased string.
*/
function camelCaseDash( string ) {
return string.replace( /-([a-z])/g, ( _, letter ) => letter.toUpperCase() );
}
/**
* Given a request string, returns the external name
* for the `WooCommerce` packages.
*
* @see https://github.com/woocommerce/woocommerce/blob/34b5e8b5d63a7d9df6622dc3af2aeaf428160357/packages/js/dependency-extraction-webpack-plugin/src/index.js#L20
*
* @param {string} request Request string.
*
* @return {string[]} External name for the package.
*/
const wooRequestToExternal = ( request ) => {
if ( packages.includes( request ) ) {
const handle = request.substring( WOOCOMMERCE_NAMESPACE.length );
const irregularExternalMap = {
'block-data': [ 'wc', 'wcBlocksData' ],
'blocks-registry': [ 'wc', 'wcBlocksRegistry' ],
settings: [ 'wc', 'wcSettings' ],
};
if ( irregularExternalMap[ handle ] ) {
return irregularExternalMap[ handle ];
}
return [ 'wc', camelCaseDash( handle ) ];
}
};
/**
* Given a request string, returns the handle name
* for the `WooCommerce` packages.
*
* @see https://github.com/woocommerce/woocommerce/blob/34b5e8b5d63a7d9df6622dc3af2aeaf428160357/packages/js/dependency-extraction-webpack-plugin/src/index.js#L41
*
* @param {string} request Request string.
*
* @return {string} Handle name for the package.
*/
const wooRequestToHandle = ( request ) => {
if ( packages.includes( request ) ) {
const handle = request.substring( WOOCOMMERCE_NAMESPACE.length );
const irregularHandleMap = {
data: 'wc-store-data',
'block-data': 'wc-blocks-data-store',
'csv-export': 'wc-csv',
};
if ( irregularHandleMap[ handle ] ) {
return irregularHandleMap[ handle ];
}
return 'wc-' + handle;
}
};
/**
* Given a request string, returns the external name for the packages.
*
* @param request string Request string.
*
* @return {string[]} External name for the package.
*
*/
const requestToExternal = ( request ) => {
// @wordpress/ui is not registered as a WP script handle yet.
// Bundle it instead of externalizing to the missing `wp.ui` global.
if ( request === '@wordpress/ui' ) {
return false;
}
// Shared UI packages: built once as their own bundles (see webpack.config.js) so they are
// not inlined into every entry that imports them. Only the bare specifier is mapped —
// deep imports such as `@getdokan/dokan-ui/dist/components/Button` stay bundled, and the
// shim entries in src/externals/ rely on that to avoid re-exporting themselves.
if ( SHARED_UI_EXTERNALS[ request ] ) {
return SHARED_UI_EXTERNALS[ request ].external;
}
const dokanStores = request.match( /^@dokan\/stores\/(.+)$/ );
const wc = request.match( /^@woocommerce\/(.+)$/ );
const dokan = request.match( /^@dokan\/([^/]+)$/ );
if ( dokanStores ) {
const storeName = camelCaseDash( dokanStores[ 1 ] );
return [ 'dokan', storeName + 'Store' ];
}
if ( wc ) {
return wooRequestToExternal( request );
}
if ( dokan ) {
const packageName = dokan[ 1 ];
const externalNameMap = {
hooks: 'reactHooks',
'product-editor': 'productEditor',
};
const externalName = externalNameMap[ packageName ] || packageName;
return [ 'dokan', externalName ];
}
// Add more custom mappings as needed.
};
/**
* Given a request string, returns the handle name.
*
* @param request string Request string.
*
* @return {string} Handle name for the package.
*/
const requestToHandle = ( request ) => {
if ( SHARED_UI_EXTERNALS[ request ] ) {
return SHARED_UI_EXTERNALS[ request ].handle;
}
const dokan = request.match( /^@dokan\/stores\/(.+)$/ );
const wc = request.match( /^@woocommerce\/(.+)$/ );
const dokanOthers = request.match( /^@dokan\/([^/]+)$/ );
if ( dokan ) {
// Convert the store name to camelCase and append 'Store'.
const storeName = kebabCase( dokan[ 1 ] );
return `dokan-stores-${ storeName }`;
}
if ( dokanOthers ) {
const packageName = dokanOthers[ 1 ];
const handleNameMap = {
components: 'react-components',
'product-editor': 'product-editor-utils',
};
const handleName = handleNameMap[ packageName ] || packageName;
return `dokan-${ handleName }`;
}
if ( wc ) {
return wooRequestToHandle( request );
}
// Add more custom mappings as needed.
};
module.exports = {
requestToExternal,
requestToHandle,
};