Skip to content

Commit 1e54baf

Browse files
authored
feat(webpack): support es module bundling (#10788)
1 parent d6d3800 commit 1e54baf

File tree

20 files changed

+811
-419
lines changed

20 files changed

+811
-419
lines changed

apps/automated/src/globals/globals-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export function test_global_registerModule() {
2323
TKUnit.assert(typeof global.registerModule === 'function', 'global.registerModule not a function');
2424
}
2525

26-
export function test_global_registerWebpackModules() {
27-
TKUnit.assert(typeof global.registerWebpackModules === 'function', 'global.registerWebpackModules not a function');
26+
export function test_global_registerBundlerModules() {
27+
TKUnit.assert(typeof global.registerBundlerModules === 'function', 'global.registerBundlerModules not a function');
2828
}
2929

3030
export function test_global_loadModule() {

packages/core/global-types.d.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ declare module globalThis {
5353

5454
function registerModule(name: string, loader: (name: string) => any): void;
5555
/**
56-
* Register all modules from a webpack context.
57-
* The context is one created using the following webpack utility:
56+
* Register all modules from a bundler context.
57+
* For example, the context could be one created using the following webpack utility:
5858
* https://webpack.js.org/guides/dependency-management/#requirecontext
5959
*
60-
* The extension map is optional, modules in the webpack context will have their original file extension (e.g. may be ".ts" or ".scss" etc.),
60+
* The extension map is optional, modules in the bundler context will have their original file extension (e.g. may be ".ts" or ".scss" etc.),
6161
* while the built-in module builders in {N} will look for ".js", ".css" or ".xml" files. Adding a map such as:
6262
* ```
6363
* { ".ts": ".js" }
6464
* ```
6565
* Will resolve lookups for .js to the .ts file.
6666
* By default scss and ts files are mapped.
6767
*/
68-
function registerWebpackModules(context: { keys(): string[]; (key: string): any }, extensionMap?: { [originalFileExtension: string]: string });
68+
function registerBundlerModules(context: { keys(): string[]; (key: string): any }, extensionMap?: { [originalFileExtension: string]: string });
6969

7070
/**
7171
* The NativeScript XML builder, style-scope, application modules use various resources such as:
@@ -91,7 +91,7 @@ declare module globalThis {
9191
function loadModule(name: string, loadForUI?: boolean): any;
9292

9393
/**
94-
* Checks if the module has been registered with `registerModule` or in `registerWebpackModules`
94+
* Checks if the module has been registered with `registerModule` or in `registerBundlerModules`
9595
* @param name Name of the module
9696
*/
9797
function moduleExists(name: string): boolean;
@@ -100,8 +100,6 @@ declare module globalThis {
100100

101101
function _unregisterModule(name: string): void;
102102

103-
function _isModuleLoadedForUI(moduleName: string): boolean;
104-
105103
var onGlobalLayoutListener: any;
106104
function zonedCallback<T = Function>(callback: T): T;
107105
var Reflect: any;

packages/core/globals/index.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,20 @@ export function initGlobal() {
170170
modules.delete(name);
171171
};
172172

173-
global._isModuleLoadedForUI = function _isModuleLoadedForUI(moduleName: string): boolean {
174-
return modulesLoadedForUI.has(moduleName);
175-
};
173+
global.registerBundlerModules = function registerBundlerModules(context: Context, extensionMap: ExtensionMap = {}) {
174+
const registerWithName = (nickName: string, moduleId: string) => {
175+
modules.set(nickName, {
176+
moduleId,
177+
loader: () => {
178+
return context(moduleId);
179+
},
180+
});
181+
};
176182

177-
global.registerWebpackModules = function registerWebpackModules(context: Context, extensionMap: ExtensionMap = {}) {
178-
context.keys().forEach((moduleId) => {
183+
const registerModuleById = (moduleId: string) => {
179184
const extDotIndex = moduleId.lastIndexOf('.');
180-
const base = moduleId.substr(0, extDotIndex);
181-
const originalExt = moduleId.substr(extDotIndex);
185+
const base = moduleId.substring(0, extDotIndex);
186+
const originalExt = moduleId.substring(extDotIndex);
182187
const registerExt = extensionMap[originalExt] || defaultExtensionMap[originalExt] || originalExt;
183188

184189
// We prefer source files for webpack scenarios before compilation leftovers,
@@ -187,47 +192,40 @@ export function initGlobal() {
187192
const isSourceFile = originalExt !== registerExt;
188193
const registerName = base + registerExt;
189194

190-
const registerWithName = (nickName: string) => {
191-
modules.set(nickName, {
192-
moduleId,
193-
loader: () => {
194-
return context(moduleId);
195-
},
196-
});
197-
};
198-
199195
if (registerName.startsWith('./') && registerName.endsWith('.js')) {
200196
const jsNickNames = [
201197
// This is extremely short version like "main-page" that was promoted to be used with global.registerModule("module-name", loaderFunc);
202-
registerName.substr(2, registerName.length - 5),
198+
registerName.substring(2, registerName.length - 3),
203199
// This is for supporting module names like "./main/main-page"
204-
registerName.substr(0, registerName.length - 3),
200+
registerName.substring(0, registerName.length - 3),
205201
// This is for supporting module names like "main/main-page.js"
206-
registerName.substr(2),
202+
registerName.substring(2),
207203
];
208204

209205
jsNickNames.forEach((jsNickName) => {
210206
if (isSourceFile || !global.moduleExists(jsNickName)) {
211-
registerWithName(jsNickName);
207+
registerWithName(jsNickName, moduleId);
212208
}
213209
});
214210
} else if (registerName.startsWith('./')) {
215211
const moduleNickNames = [
216212
// This is for supporting module names like "main/main-page.xml"
217-
registerName.substr(2),
213+
registerName.substring(2),
218214
];
219215

220216
moduleNickNames.forEach((moduleNickName) => {
221217
if (!global.moduleExists(moduleNickName)) {
222-
registerWithName(moduleNickName);
218+
registerWithName(moduleNickName, moduleId);
223219
}
224220
});
225221
}
226222

227223
if (isSourceFile || !global.moduleExists(registerName)) {
228-
registerWithName(registerName);
224+
registerWithName(registerName, moduleId);
229225
}
230-
});
226+
};
227+
228+
context.keys().forEach(registerModuleById);
231229
};
232230

233231
global.moduleExists = function moduleExists(name: string): boolean {

0 commit comments

Comments
 (0)