chore: cleanup

This commit is contained in:
Nathan Walker
2025-07-22 19:06:58 -07:00
parent 0ba0ab0d59
commit b853447da2
16 changed files with 162 additions and 179 deletions

View File

@ -23,8 +23,8 @@ export function test_global_registerModule() {
TKUnit.assert(typeof global.registerModule === 'function', 'global.registerModule not a function'); TKUnit.assert(typeof global.registerModule === 'function', 'global.registerModule not a function');
} }
export function test_global_registerWebpackModules() { export function test_global_registerBundlerModules() {
TKUnit.assert(typeof global.registerWebpackModules === 'function', 'global.registerWebpackModules not a function'); TKUnit.assert(typeof global.registerBundlerModules === 'function', 'global.registerBundlerModules not a function');
} }
export function test_global_loadModule() { export function test_global_loadModule() {

View File

@ -410,7 +410,6 @@ export class Observable {
this._globalNotify(eventClass, 'First', dataWithObject); this._globalNotify(eventClass, 'First', dataWithObject);
const observers = this._observers[data.eventName]; const observers = this._observers[data.eventName];
console.log('notify called for event:', data.eventName, 'with observers:', observers);
if (observers) { if (observers) {
Observable._handleEvent(observers, dataWithObject); Observable._handleEvent(observers, dataWithObject);
} }

View File

@ -53,11 +53,11 @@ declare module globalThis {
function registerModule(name: string, loader: (name: string) => any): void; function registerModule(name: string, loader: (name: string) => any): void;
/** /**
* Register all modules from a webpack context. * Register all modules from a bundler context.
* The context is one created using the following webpack utility: * For example, the context could be one created using the following webpack utility:
* https://webpack.js.org/guides/dependency-management/#requirecontext * https://webpack.js.org/guides/dependency-management/#requirecontext
* *
* The extension map is optional, modules in the webpack context will have their original file extension (e.g. may be ".ts" or ".scss" etc.), * The extension map is optional, modules in the bundler context will have their original file extension (e.g. may be ".ts" or ".scss" etc.),
* while the built-in module builders in {N} will look for ".js", ".css" or ".xml" files. Adding a map such as: * while the built-in module builders in {N} will look for ".js", ".css" or ".xml" files. Adding a map such as:
* ``` * ```
* { ".ts": ".js" } * { ".ts": ".js" }
@ -65,7 +65,7 @@ declare module globalThis {
* Will resolve lookups for .js to the .ts file. * Will resolve lookups for .js to the .ts file.
* By default scss and ts files are mapped. * By default scss and ts files are mapped.
*/ */
function registerWebpackModules(context: { keys(): string[]; (key: string): any }, extensionMap?: { [originalFileExtension: string]: string }); function registerBundlerModules(context: { keys(): string[]; (key: string): any }, extensionMap?: { [originalFileExtension: string]: string });
/** /**
* The NativeScript XML builder, style-scope, application modules use various resources such as: * The NativeScript XML builder, style-scope, application modules use various resources such as:
@ -91,7 +91,7 @@ declare module globalThis {
function loadModule(name: string, loadForUI?: boolean): any; function loadModule(name: string, loadForUI?: boolean): any;
/** /**
* Checks if the module has been registered with `registerModule` or in `registerWebpackModules` * Checks if the module has been registered with `registerModule` or in `registerBundlerModules`
* @param name Name of the module * @param name Name of the module
*/ */
function moduleExists(name: string): boolean; function moduleExists(name: string): boolean;

View File

@ -92,34 +92,7 @@ const defaultExtensionMap: ExtensionMap = {
'.xml': '.xml', '.xml': '.xml',
}; };
// ESM-compatible module resolver global.moduleResolvers = [global.require];
function esmModuleResolver(name: string): any {
// First try to resolve from registered modules
if (modules.has(name)) {
return modules.get(name).loader(name);
}
// For ESM builds, we can't use require() synchronously for dynamic imports
// Instead, we need to rely on pre-registered modules or throw an error
// indicating the module needs to be pre-registered
if (!__COMMONJS__) {
console.warn(`Module '${name}' not found in registered modules. In ESM builds, modules must be pre-registered as follows:`);
console.warn(`import * as myModule from '${name}';`);
console.warn(`global.registerModule('${name}', () => myModule);`);
return null;
}
// Fallback to require for CommonJS builds
try {
return global.require ? global.require(name) : null;
} catch (e) {
console.warn(`Failed to require module '${name}':`, e);
return null;
}
}
// Cast to <any> because moduleResolvers is read-only in definitions
global.moduleResolvers = [esmModuleResolver];
global.registerModule = function (name: string, loader: ModuleLoader): void { global.registerModule = function (name: string, loader: ModuleLoader): void {
modules.set(name, { loader, moduleId: name }); modules.set(name, { loader, moduleId: name });
@ -129,8 +102,19 @@ global._unregisterModule = function _unregisterModule(name: string): void {
modules.delete(name); modules.delete(name);
}; };
global.registerWebpackModules = function registerWebpackModules(context: Context, extensionMap: ExtensionMap = {}) { global.registerBundlerModules = function registerBundlerModules(context: Context, extensionMap: ExtensionMap = {}) {
context.keys().forEach((moduleId) => { const registerWithName = (nickName: string, moduleId: string) => {
console.log(`Registering module '${nickName}' with id '${moduleId}'`);
modules.set(nickName, {
moduleId,
loader: () => {
return context(moduleId);
},
});
};
const registerModuleById = (moduleId: string) => {
console.log(`registerModuleById: ${moduleId}`);
const extDotIndex = moduleId.lastIndexOf('.'); const extDotIndex = moduleId.lastIndexOf('.');
const base = moduleId.substring(0, extDotIndex); const base = moduleId.substring(0, extDotIndex);
const originalExt = moduleId.substring(extDotIndex); const originalExt = moduleId.substring(extDotIndex);
@ -142,15 +126,6 @@ global.registerWebpackModules = function registerWebpackModules(context: Context
const isSourceFile = originalExt !== registerExt; const isSourceFile = originalExt !== registerExt;
const registerName = base + registerExt; const registerName = base + registerExt;
const registerWithName = (nickName: string) => {
modules.set(nickName, {
moduleId,
loader: () => {
return context(moduleId);
},
});
};
if (registerName.startsWith('./') && registerName.endsWith('.js')) { if (registerName.startsWith('./') && registerName.endsWith('.js')) {
const jsNickNames = [ const jsNickNames = [
// This is extremely short version like "main-page" that was promoted to be used with global.registerModule("module-name", loaderFunc); // This is extremely short version like "main-page" that was promoted to be used with global.registerModule("module-name", loaderFunc);
@ -163,7 +138,7 @@ global.registerWebpackModules = function registerWebpackModules(context: Context
jsNickNames.forEach((jsNickName) => { jsNickNames.forEach((jsNickName) => {
if (isSourceFile || !global.moduleExists(jsNickName)) { if (isSourceFile || !global.moduleExists(jsNickName)) {
registerWithName(jsNickName); registerWithName(jsNickName, moduleId);
} }
}); });
} else if (registerName.startsWith('./')) { } else if (registerName.startsWith('./')) {
@ -174,15 +149,17 @@ global.registerWebpackModules = function registerWebpackModules(context: Context
moduleNickNames.forEach((moduleNickName) => { moduleNickNames.forEach((moduleNickName) => {
if (!global.moduleExists(moduleNickName)) { if (!global.moduleExists(moduleNickName)) {
registerWithName(moduleNickName); registerWithName(moduleNickName, moduleId);
} }
}); });
} }
if (isSourceFile || !global.moduleExists(registerName)) { if (isSourceFile || !global.moduleExists(registerName)) {
registerWithName(registerName); registerWithName(registerName, moduleId);
} }
}); };
context.keys().forEach(registerModuleById);
}; };
global.moduleExists = function moduleExists(name: string): boolean { global.moduleExists = function moduleExists(name: string): boolean {
@ -282,12 +259,11 @@ async function dynamicResolveModule(name: string) {
return result.default || result; return result.default || result;
} }
console.log(`globals/index, __COMMONJS__:`, __COMMONJS__);
global.loadModule = function loadModule(name: string): any { global.loadModule = function loadModule(name: string): any {
console.log(`@@@ loadModule: ${name}, __COMMONJS__:`, __COMMONJS__);
const moduleInfo = modules.get(name); const moduleInfo = modules.get(name);
if (moduleInfo) { if (moduleInfo) {
const result = moduleInfo.loader(name); const result = moduleInfo.loader(name);
console.log(`@@@ loadModule resolved to`, result);
if (result?.enableAutoAccept) { if (result?.enableAutoAccept) {
result.enableAutoAccept(); result.enableAutoAccept();
} }
@ -295,15 +271,16 @@ global.loadModule = function loadModule(name: string): any {
return result; return result;
} }
if (__COMMONJS__) {
for (const resolver of global.moduleResolvers) { for (const resolver of global.moduleResolvers) {
const result = resolver(name); const result = resolver(name);
if (result) { if (result) {
console.log(`@@@ loadModule SET SET resolver: ${name} resolved to`, result);
modules.set(name, { moduleId: name, loader: () => result }); modules.set(name, { moduleId: name, loader: () => result });
return result; return result;
} }
} }
}
// If no resolver found the module, return null // If no resolver found the module, return null
console.warn(`Module '${name}' could not be loaded by any resolver.`); console.warn(`Module '${name}' could not be loaded by any resolver.`);
@ -343,6 +320,7 @@ if (!global.NativeScriptHasPolyfilled) {
console.log('Installing polyfills...'); console.log('Installing polyfills...');
// DOM api polyfills // DOM api polyfills
const glb = global as any;
if (__COMMONJS__) { if (__COMMONJS__) {
global.registerModule('timer', () => timer); global.registerModule('timer', () => timer);
installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']); installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']);
@ -371,72 +349,45 @@ if (!global.NativeScriptHasPolyfilled) {
global.registerModule('subtle', () => subtleCryptoImpl); global.registerModule('subtle', () => subtleCryptoImpl);
installPolyfills('subtle-crypto', ['Subtle']); installPolyfills('subtle-crypto', ['Subtle']);
} else { } else {
// @ts-expect-error // timers
global.setTimeout = timer.setTimeout; glb.setTimeout = timer.setTimeout;
global.clearTimeout = timer.clearTimeout; glb.clearTimeout = timer.clearTimeout;
// @ts-expect-error glb.setInterval = timer.setInterval;
global.setInterval = timer.setInterval; glb.clearInterval = timer.clearInterval;
global.clearInterval = timer.clearInterval;
// global.registerModule('timer', () => timer);
// installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']);
// global.registerModule('animation', () => animationFrame); // animation frame
// installPolyfills('animation', ['requestAnimationFrame', 'cancelAnimationFrame']); glb.requestAnimationFrame = animationFrame.requestAnimationFrame;
global.requestAnimationFrame = animationFrame.requestAnimationFrame; glb.cancelAnimationFrame = animationFrame.cancelAnimationFrame;
global.cancelAnimationFrame = animationFrame.cancelAnimationFrame;
// global.registerModule('media-query-list', () => mediaQueryList); // media query list
// installPolyfills('media-query-list', ['matchMedia', 'MediaQueryList']); glb.matchMedia = mediaQueryList.matchMedia;
// @ts-expect-error glb.MediaQueryList = mediaQueryList.MediaQueryList;
global.matchMedia = mediaQueryList.matchMedia;
// @ts-expect-error
global.MediaQueryList = mediaQueryList.MediaQueryList;
// global.registerModule('text', () => text); // text
// installPolyfills('text', ['TextDecoder', 'TextEncoder']); glb.TextDecoder = text.TextDecoder;
// @ts-expect-error glb.TextEncoder = text.TextEncoder;
global.TextDecoder = text.TextDecoder;
// @ts-expect-error
global.TextEncoder = text.TextEncoder;
// global.registerModule('xhr', () => xhrImpl); // xhr
// installPolyfills('xhr', ['XMLHttpRequest', 'FormData', 'Blob', 'File', 'FileReader']); glb.XMLHttpRequest = xhrImpl.XMLHttpRequest;
// @ts-expect-error glb.FormData = xhrImpl.FormData;
global.XMLHttpRequest = xhrImpl.XMLHttpRequest; glb.Blob = xhrImpl.Blob;
// @ts-expect-error glb.File = xhrImpl.File;
global.FormData = xhrImpl.FormData;
// @ts-expect-error
global.Blob = xhrImpl.Blob;
// @ts-expect-error
global.File = xhrImpl.File;
// global.registerModule('fetch', () => fetchPolyfill); // fetch
// installPolyfills('fetch', ['fetch', 'Headers', 'Request', 'Response']); glb.fetch = fetchPolyfill.fetch;
// @ts-expect-error glb.Headers = fetchPolyfill.Headers;
global.fetch = fetchPolyfill.fetch; glb.Request = fetchPolyfill.Request;
// @ts-expect-error glb.Response = fetchPolyfill.Response;
global.Headers = fetchPolyfill.Headers;
// @ts-expect-error
global.Request = fetchPolyfill.Request;
// @ts-expect-error
global.Response = fetchPolyfill.Response;
// global.registerModule('wgc', () => wgc); // wgc
// installPolyfills('wgc', ['atob', 'btoa']); glb.atob = wgc.atob;
global.atob = wgc.atob; glb.btoa = wgc.btoa;
global.btoa = wgc.btoa;
// global.registerModule('crypto', () => cryptoImpl); // wgc
// installPolyfills('crypto', ['Crypto']); glb.SubtleCrypto = subtleCryptoImpl.SubtleCrypto;
// global.registerModule('subtle', () => subtleCryptoImpl);
// installPolyfills('subtle-crypto', ['Subtle']);
// @ts-expect-error
global.SubtleCrypto = subtleCryptoImpl.SubtleCrypto;
} }
// @ts-expect-error glb.crypto = new cryptoImpl.Crypto();
global.crypto = new cryptoImpl.Crypto();
// global.registerModule('abortcontroller', () => require('../abortcontroller')); // global.registerModule('abortcontroller', () => require('../abortcontroller'));
// installPolyfills('abortcontroller', ['AbortController', 'AbortSignal']); // installPolyfills('abortcontroller', ['AbortController', 'AbortSignal']);

View File

@ -1,15 +0,0 @@
/**
* Provides ModuleNameResolver class used for loading files based on device capabilities.
*/ /** */
import type { PlatformContext } from './qualifier-matcher';
import type { ModuleListProvider } from './helpers';
export { PlatformContext } from './qualifier-matcher';
export class ModuleNameResolver {
constructor(context: PlatformContext, moduleListProvider?: ModuleListProvider);
resolveModuleName(path: string, ext: string): string;
clearCache(): void;
}
export function resolveModuleName(path: string, ext: string): string;

View File

@ -20,6 +20,7 @@ export class ModuleNameResolver implements ModuleNameResolverType {
let result: string = this._cache[key]; let result: string = this._cache[key];
if (result === undefined) { if (result === undefined) {
result = this.resolveModuleNameImpl(path, ext); result = this.resolveModuleNameImpl(path, ext);
console.log('resolveModuleName result:', result);
this._cache[key] = result; this._cache[key] = result;
} }
@ -43,13 +44,11 @@ export class ModuleNameResolver implements ModuleNameResolverType {
const candidates = this.getCandidates(path, ext); const candidates = this.getCandidates(path, ext);
result = findMatch(path, ext, candidates, this.context); result = findMatch(path, ext, candidates, this.context);
return result; return result;
} }
private getCandidates(path: string, ext: string): Array<string> { private getCandidates(path: string, ext: string): Array<string> {
const candidates = this.moduleListProvider().filter((moduleName) => moduleName.startsWith(path) && (!ext || moduleName.endsWith(ext))); const candidates = this.moduleListProvider().filter((moduleName) => moduleName.startsWith(path) && (!ext || moduleName.endsWith(ext)));
return candidates; return candidates;
} }
} }

View File

@ -1,17 +0,0 @@
/**
* Provides ModuleNameResolver class used for loading files based on device capabilities.
*/
/**
* Used with qualifier matchers and module resolution
*/
export interface PlatformContext {
width: number;
height: number;
os: string;
deviceType: string;
}
export function findMatch(path: string, ext: string, candidates: Array<string>, context: PlatformContext): string;
export function stripQualifiers(path: string): string;

View File

@ -27,7 +27,7 @@ const minWidthHeightQualifier: QualifierSpec = {
return path.match(new RegExp(`.${MIN_WH}\\d+`, 'g')); return path.match(new RegExp(`.${MIN_WH}\\d+`, 'g'));
}, },
getMatchValue(value: string, context: PlatformContext): number { getMatchValue(value: string, context: PlatformContext): number {
const numVal = parseInt(value.substr(MIN_WH.length + 1)); const numVal = parseInt(value.substring(MIN_WH.length + 1));
if (isNaN(numVal)) { if (isNaN(numVal)) {
return -1; return -1;
} }
@ -49,7 +49,7 @@ const minWidthQualifier: QualifierSpec = {
return path.match(new RegExp(`.${MIN_W}\\d+`, 'g')); return path.match(new RegExp(`.${MIN_W}\\d+`, 'g'));
}, },
getMatchValue(value: string, context: PlatformContext): number { getMatchValue(value: string, context: PlatformContext): number {
const numVal = parseInt(value.substr(MIN_W.length + 1)); const numVal = parseInt(value.substring(MIN_W.length + 1));
if (isNaN(numVal)) { if (isNaN(numVal)) {
return -1; return -1;
} }
@ -71,7 +71,7 @@ const minHeightQualifier: QualifierSpec = {
return path.match(new RegExp(`.${MIN_H}\\d+`, 'g')); return path.match(new RegExp(`.${MIN_H}\\d+`, 'g'));
}, },
getMatchValue(value: string, context: PlatformContext): number { getMatchValue(value: string, context: PlatformContext): number {
const numVal = parseInt(value.substr(MIN_H.length + 1)); const numVal = parseInt(value.substring(MIN_H.length + 1));
if (isNaN(numVal)) { if (isNaN(numVal)) {
return -1; return -1;
} }
@ -93,7 +93,7 @@ const platformQualifier: QualifierSpec = {
return path.match(new RegExp('\\.android|\\.ios', 'g')); return path.match(new RegExp('\\.android|\\.ios', 'g'));
}, },
getMatchValue(value: string, context: PlatformContext): number { getMatchValue(value: string, context: PlatformContext): number {
const val = value.substr(1); const val = value.substring(1);
return val === context.os.toLowerCase() ? 1 : -1; return val === context.os.toLowerCase() ? 1 : -1;
}, },
@ -107,7 +107,7 @@ const orientationQualifier: QualifierSpec = {
return path.match(new RegExp('\\.land|\\.port', 'g')); return path.match(new RegExp('\\.land|\\.port', 'g'));
}, },
getMatchValue(value: string, context: PlatformContext): number { getMatchValue(value: string, context: PlatformContext): number {
const val = value.substr(1); const val = value.substring(1);
const isLandscape: number = context.width > context.height ? 1 : -1; const isLandscape: number = context.width > context.height ? 1 : -1;
return val === 'land' ? isLandscape : -isLandscape; return val === 'land' ? isLandscape : -isLandscape;
@ -176,6 +176,8 @@ export function findMatch(path: string, ext: string, candidates: Array<string>,
result = candidates[i]; result = candidates[i];
} }
} }
console.log(' > findMatch called with path:', path, 'and ext:', ext, 'candidates:', candidates, 'context:', context, '--- MATCH? result:', result);
console.log('. ');
return result; return result;
} }

View File

@ -155,10 +155,6 @@ const applyComponentAttributes = profile('applyComponentAttributes', (instance:
} }
} }
console.log('applyComponentAttributes called for attr:', attr, 'with value:', attrValue);
if (attr === 'navigatingTo') {
console.log('@@@ navigatingTo moduleExports:', moduleExports);
}
if (attr.indexOf('.') !== -1) { if (attr.indexOf('.') !== -1) {
let subObj = instance; let subObj = instance;
const properties = attr.split('.'); const properties = attr.split('.');
@ -189,9 +185,6 @@ export function getComponentModule(elementName: string, namespace: string, attri
const { instance, instanceModule } = createComponentInstance(elementName, namespace, null); const { instance, instanceModule } = createComponentInstance(elementName, namespace, null);
moduleExports = getComponentModuleExports(instance, <any>moduleExports, attributes); moduleExports = getComponentModuleExports(instance, <any>moduleExports, attributes);
console.log('getComponentModule called for element:', elementName, 'with namespace:', namespace, 'and attributes:', attributes);
console.log('moduleExports ---');
console.log(moduleExports);
if (isRootComponent) { if (isRootComponent) {
applyComponentCss(instance, moduleNamePath, attributes); applyComponentCss(instance, moduleNamePath, attributes);
} }
@ -209,7 +202,6 @@ export function getComponentModule(elementName: string, namespace: string, attri
export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: any) { export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: any) {
// Note: instanceModule can be null if we are loading custom component with no code-behind. // Note: instanceModule can be null if we are loading custom component with no code-behind.
if (isBinding(propertyValue) && instance.bind) { if (isBinding(propertyValue) && instance.bind) {
console.log('1 setPropertyValue, Binding detected for property:', propertyName, 'with value:', propertyValue);
const bindOptions = getBindingOptions(propertyName, getBindingExpressionFromAttribute(propertyValue)); const bindOptions = getBindingOptions(propertyName, getBindingExpressionFromAttribute(propertyValue));
instance.bind( instance.bind(
{ {
@ -221,8 +213,6 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
bindOptions[bindingConstants.source], bindOptions[bindingConstants.source],
); );
} else if (isEventOrGesture(propertyName, instance)) { } else if (isEventOrGesture(propertyName, instance)) {
console.log('2 setPropertyValue, Binding detected for property:', propertyName, 'with value:', propertyValue);
console.log('setPropertyValue, exports:', exports);
// Get the event handler from page module exports. // Get the event handler from page module exports.
const handler = exports && exports[propertyValue]; const handler = exports && exports[propertyValue];
@ -231,10 +221,8 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
instance.on(propertyName, handler); instance.on(propertyName, handler);
} }
} else if (isKnownFunction(propertyName, instance) && exports && typeof exports[propertyValue] === 'function') { } else if (isKnownFunction(propertyName, instance) && exports && typeof exports[propertyValue] === 'function') {
console.log('3 setPropertyValue, Binding detected for property:', propertyName, 'with value:', propertyValue);
instance[propertyName] = exports[propertyValue]; instance[propertyName] = exports[propertyValue];
} else { } else {
console.log('4 setPropertyValue, Binding detected for property:', propertyName, 'with value:', propertyValue);
instance[propertyName] = propertyValue; instance[propertyName] = propertyValue;
} }
} }
@ -249,7 +237,6 @@ function isBinding(value: any): boolean {
const str = value.trim(); const str = value.trim();
isBinding = str.indexOf('{{') === 0 && str.lastIndexOf('}}') === str.length - 2; isBinding = str.indexOf('{{') === 0 && str.lastIndexOf('}}') === str.length - 2;
} }
console.log('isBinding called with value:', value, ' isBinding:', isBinding);
return isBinding; return isBinding;
} }

View File

@ -73,9 +73,11 @@ export class Builder {
return view; return view;
} else if (entry.moduleName) { } else if (entry.moduleName) {
const moduleName = sanitizeModuleName(entry.moduleName); const moduleName = sanitizeModuleName(entry.moduleName);
console.log('.');
console.log('moduleName:', moduleName);
const resolvedCodeModuleName = resolveModuleName(moduleName, ''); //`${moduleName}.xml`; const resolvedCodeModuleName = resolveModuleName(moduleName, ''); //`${moduleName}.xml`;
const moduleExports = resolvedCodeModuleName ? global.loadModule(resolvedCodeModuleName) : null; const moduleExports = resolvedCodeModuleName ? global.loadModule(resolvedCodeModuleName) : null;
console.log('Resolved code module name:', resolvedCodeModuleName, ' exports:', moduleExports); console.log('createViewFromEntry called with entry:', entry, 'moduleName:', entry.moduleName, 'resolvedCodeModuleName:', resolvedCodeModuleName, 'moduleExports:', moduleExports);
if (moduleExports && moduleExports.createPage) { if (moduleExports && moduleExports.createPage) {
// Exports has a createPage() method // Exports has a createPage() method
const view = moduleExports.createPage(); const view = moduleExports.createPage();
@ -141,7 +143,7 @@ export class Builder {
*/ */
static parseMultipleTemplates(value: string, context: any): Array<KeyedTemplate> { static parseMultipleTemplates(value: string, context: any): Array<KeyedTemplate> {
const dummyComponent = `<ListView><ListView.itemTemplates>${value}</ListView.itemTemplates></ListView>`; const dummyComponent = `<ListView><ListView.itemTemplates>${value}</ListView.itemTemplates></ListView>`;
console.log('parseMultipleTemplates called with value:', value, 'context:', context);
return parseInternal(dummyComponent, context).component['itemTemplates']; return parseInternal(dummyComponent, context).component['itemTemplates'];
} }
} }
@ -184,7 +186,6 @@ export function createViewFromEntry(entry: ViewEntry): View {
function loadInternal(moduleName: string, moduleExports: any): ComponentModule { function loadInternal(moduleName: string, moduleExports: any): ComponentModule {
let componentModule: ComponentModule; let componentModule: ComponentModule;
console.log('loadInternal called for moduleName:', moduleName, 'with moduleExports:', moduleExports);
const resolvedXmlModule = resolveModuleName(moduleName, 'xml'); const resolvedXmlModule = resolveModuleName(moduleName, 'xml');
if (resolvedXmlModule) { if (resolvedXmlModule) {
@ -285,7 +286,6 @@ export function getExports(instance: ViewBase): any {
} }
function parseInternal(value: string, context: any, xmlModule?: string, moduleName?: string): ComponentModule { function parseInternal(value: string, context: any, xmlModule?: string, moduleName?: string): ComponentModule {
console.log('parseInternal called with value:', value, 'context:', context, 'xmlModule:', xmlModule, 'moduleName:', moduleName);
if (__UI_USE_XML_PARSER__) { if (__UI_USE_XML_PARSER__) {
let start: xml2ui.XmlStringParser; let start: xml2ui.XmlStringParser;
let ui: xml2ui.ComponentParser; let ui: xml2ui.ComponentParser;
@ -668,7 +668,6 @@ export namespace xml2ui {
@profile @profile
private buildComponent(args: xml.ParserEvent): ComponentModule { private buildComponent(args: xml.ParserEvent): ComponentModule {
console.log('ComponentParser.buildComponent called for element:', args.elementName, 'with namespace:', args.namespace, 'and attributes:', args.attributes, 'context:', this.context);
if (args.prefix && args.namespace) { if (args.prefix && args.namespace) {
// Custom components // Custom components
return loadCustomComponent(args.namespace, args.elementName, args.attributes, this.context, this.currentRootView, !this.currentRootView, this.moduleName); return loadCustomComponent(args.namespace, args.elementName, args.attributes, this.context, this.currentRootView, !this.currentRootView, this.moduleName);

View File

@ -59,7 +59,7 @@ export function sanitizeModuleName(moduleName: string, removeExtension = true):
} }
if (removeExtension) { if (removeExtension) {
const extToRemove = ['js', 'ts', 'xml', 'html', 'css', 'scss']; const extToRemove = ['js', 'mjs', 'ts', 'xml', 'html', 'css', 'scss'];
const extensionRegEx = new RegExp(`(.*)\\.(?:${extToRemove.join('|')})`, 'i'); const extensionRegEx = new RegExp(`(.*)\\.(?:${extToRemove.join('|')})`, 'i');
moduleName = moduleName.replace(extensionRegEx, '$1'); moduleName = moduleName.replace(extensionRegEx, '$1');
} }

View File

@ -23,7 +23,7 @@
"assets": [ "assets": [
{ {
"input": "{projectRoot}/src/stubs", "input": "{projectRoot}/src/stubs",
"glob": "*.js", "glob": "*.{js,mjs}",
"output": "stubs" "output": "stubs"
}, },
{ {

View File

@ -12,7 +12,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
const entryPath = getEntryPath(); const entryPath = getEntryPath();
const virtualEntryPath = path.resolve( const virtualEntryPath = path.resolve(
__dirname, __dirname,
'../stubs/virtual-entry-typescript.js' `../stubs/virtual-entry-typescript.${env.commonjs ? 'js' : 'mjs'}`,
); );
// exclude files starting with _ from require.context // exclude files starting with _ from require.context
@ -23,7 +23,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
chainedSetAddAfter( chainedSetAddAfter(
config.entry('bundle'), config.entry('bundle'),
'@nativescript/core/globals/index', '@nativescript/core/globals/index',
virtualEntryPath virtualEntryPath,
); );
config.when(env.hmr, (config) => { config.when(env.hmr, (config) => {

View File

@ -1,5 +1,5 @@
// VIRTUAL ENTRY START // VIRTUAL ENTRY START
require('@nativescript/core/bundle-entry-points') require('@nativescript/core/bundle-entry-points')
const context = require.context("~/", /* deep: */ true, /* filter: */ /.(xml|js|s?css)$/); const context = require.context("~/", /* deep: */ true, /* filter: */ /.(xml|js|s?css)$/);
global.registerWebpackModules(context); global.registerBundlerModules(context);
// VIRTUAL ENTRY END // VIRTUAL ENTRY END

View File

@ -1,5 +1,5 @@
// VIRTUAL ENTRY START // VIRTUAL ENTRY START
require('@nativescript/core/bundle-entry-points') require('@nativescript/core/bundle-entry-points')
const context = require.context("~/", /* deep: */ true, /* filter: */ /\.(xml|js|(?<!\.d\.)ts|s?css)$/); const context = require.context("~/", /* deep: */ true, /* filter: */ /\.(xml|js|(?<!\.d\.)ts|s?css)$/);
global.registerWebpackModules(context); global.registerBundlerModules(context);
// VIRTUAL ENTRY END // VIRTUAL ENTRY END

View File

@ -0,0 +1,78 @@
// VIRTUAL ENTRY START
require('@nativescript/core/bundle-entry-points')
import { readdirSync } from 'fs';
import { join, extname, relative } from 'path';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';
console.log('__COMMONJS__:', __COMMONJS__);
// const context = require.context("~/", /* deep: */ true, /* filter: */ /\.(xml|js|(?<!\.d\.)ts|s?css)$/);
// const modules = import.meta.glob(
// // adjust the pattern to your layout:
// "/src/**/*.@(xml|js|ts|scss|css)"
// // { eager: true } // uncomment to import immediately
// );
console.log('typeof import.meta.glob:', typeof import.meta.glob);
if (typeof import.meta.glob !== 'undefined') {
// Vite environment
const modules = import.meta.glob(
// adjust the pattern to your layout:
'~/**/*.@(xml|js|ts|scss|css)',
{ eager: true }, // uncomment to import immediately
);
global.registerBundlerModules(modules);
} else {
const require = createRequire(import.meta.url);
const root = fileURLToPath(new URL('./src', import.meta.url));
/**
* Recursively walk `dir`, collecting all files whose extension is in `exts`,
* ignoring any `*.d.ts` files.
*/
function collectFilesSync(
dir,
exts = ['.xml', '.js', '.ts', '.css', '.scss'],
) {
let results = [];
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const full = join(dir, entry.name);
if (entry.isDirectory()) {
results = results.concat(collectFilesSync(full, exts));
} else {
// skip declaration files
if (entry.name.endsWith('.d.ts')) continue;
// filter by extension
if (exts.includes(extname(entry.name))) {
results.push(full);
}
}
}
return results;
}
/**
* Synchronously load every matching module under `./src`,
* keyed by its path relative to `root`.
*/
function loadContextSync() {
const files = collectFilesSync(root);
const context = {};
for (const fullPath of files) {
// make the key look like fastglobs relative paths
const relPath = relative(root, fullPath).replace(/\\/g, '/');
// require() each module
context[relPath] = require(fullPath);
}
return context;
}
const context = loadContextSync();
global.registerBundlerModules(context);
}
// VIRTUAL ENTRY END