mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
chore: cleanup
This commit is contained in:
@ -23,8 +23,8 @@ export function test_global_registerModule() {
|
||||
TKUnit.assert(typeof global.registerModule === 'function', 'global.registerModule not a function');
|
||||
}
|
||||
|
||||
export function test_global_registerWebpackModules() {
|
||||
TKUnit.assert(typeof global.registerWebpackModules === 'function', 'global.registerWebpackModules not a function');
|
||||
export function test_global_registerBundlerModules() {
|
||||
TKUnit.assert(typeof global.registerBundlerModules === 'function', 'global.registerBundlerModules not a function');
|
||||
}
|
||||
|
||||
export function test_global_loadModule() {
|
||||
|
@ -410,7 +410,6 @@ export class Observable {
|
||||
this._globalNotify(eventClass, 'First', dataWithObject);
|
||||
|
||||
const observers = this._observers[data.eventName];
|
||||
console.log('notify called for event:', data.eventName, 'with observers:', observers);
|
||||
if (observers) {
|
||||
Observable._handleEvent(observers, dataWithObject);
|
||||
}
|
||||
|
10
packages/core/global-types.d.ts
vendored
10
packages/core/global-types.d.ts
vendored
@ -53,11 +53,11 @@ declare module globalThis {
|
||||
|
||||
function registerModule(name: string, loader: (name: string) => any): void;
|
||||
/**
|
||||
* Register all modules from a webpack context.
|
||||
* The context is one created using the following webpack utility:
|
||||
* Register all modules from a bundler context.
|
||||
* For example, the context could be one created using the following webpack utility:
|
||||
* 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:
|
||||
* ```
|
||||
* { ".ts": ".js" }
|
||||
@ -65,7 +65,7 @@ declare module globalThis {
|
||||
* Will resolve lookups for .js to the .ts file.
|
||||
* 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:
|
||||
@ -91,7 +91,7 @@ declare module globalThis {
|
||||
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
|
||||
*/
|
||||
function moduleExists(name: string): boolean;
|
||||
|
@ -92,34 +92,7 @@ const defaultExtensionMap: ExtensionMap = {
|
||||
'.xml': '.xml',
|
||||
};
|
||||
|
||||
// ESM-compatible module resolver
|
||||
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.moduleResolvers = [global.require];
|
||||
|
||||
global.registerModule = function (name: string, loader: ModuleLoader): void {
|
||||
modules.set(name, { loader, moduleId: name });
|
||||
@ -129,8 +102,19 @@ global._unregisterModule = function _unregisterModule(name: string): void {
|
||||
modules.delete(name);
|
||||
};
|
||||
|
||||
global.registerWebpackModules = function registerWebpackModules(context: Context, extensionMap: ExtensionMap = {}) {
|
||||
context.keys().forEach((moduleId) => {
|
||||
global.registerBundlerModules = function registerBundlerModules(context: Context, extensionMap: ExtensionMap = {}) {
|
||||
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 base = moduleId.substring(0, extDotIndex);
|
||||
const originalExt = moduleId.substring(extDotIndex);
|
||||
@ -142,15 +126,6 @@ global.registerWebpackModules = function registerWebpackModules(context: Context
|
||||
const isSourceFile = originalExt !== registerExt;
|
||||
const registerName = base + registerExt;
|
||||
|
||||
const registerWithName = (nickName: string) => {
|
||||
modules.set(nickName, {
|
||||
moduleId,
|
||||
loader: () => {
|
||||
return context(moduleId);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
if (registerName.startsWith('./') && registerName.endsWith('.js')) {
|
||||
const jsNickNames = [
|
||||
// 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) => {
|
||||
if (isSourceFile || !global.moduleExists(jsNickName)) {
|
||||
registerWithName(jsNickName);
|
||||
registerWithName(jsNickName, moduleId);
|
||||
}
|
||||
});
|
||||
} else if (registerName.startsWith('./')) {
|
||||
@ -174,15 +149,17 @@ global.registerWebpackModules = function registerWebpackModules(context: Context
|
||||
|
||||
moduleNickNames.forEach((moduleNickName) => {
|
||||
if (!global.moduleExists(moduleNickName)) {
|
||||
registerWithName(moduleNickName);
|
||||
registerWithName(moduleNickName, moduleId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (isSourceFile || !global.moduleExists(registerName)) {
|
||||
registerWithName(registerName);
|
||||
registerWithName(registerName, moduleId);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
context.keys().forEach(registerModuleById);
|
||||
};
|
||||
|
||||
global.moduleExists = function moduleExists(name: string): boolean {
|
||||
@ -282,12 +259,11 @@ async function dynamicResolveModule(name: string) {
|
||||
return result.default || result;
|
||||
}
|
||||
|
||||
console.log(`globals/index, __COMMONJS__:`, __COMMONJS__);
|
||||
global.loadModule = function loadModule(name: string): any {
|
||||
console.log(`@@@ loadModule: ${name}, __COMMONJS__:`, __COMMONJS__);
|
||||
const moduleInfo = modules.get(name);
|
||||
if (moduleInfo) {
|
||||
const result = moduleInfo.loader(name);
|
||||
console.log(`@@@ loadModule resolved to`, result);
|
||||
if (result?.enableAutoAccept) {
|
||||
result.enableAutoAccept();
|
||||
}
|
||||
@ -295,13 +271,14 @@ global.loadModule = function loadModule(name: string): any {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (const resolver of global.moduleResolvers) {
|
||||
const result = resolver(name);
|
||||
if (result) {
|
||||
console.log(`@@@ loadModule SET SET resolver: ${name} resolved to`, result);
|
||||
modules.set(name, { moduleId: name, loader: () => result });
|
||||
if (__COMMONJS__) {
|
||||
for (const resolver of global.moduleResolvers) {
|
||||
const result = resolver(name);
|
||||
if (result) {
|
||||
modules.set(name, { moduleId: name, loader: () => result });
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -343,6 +320,7 @@ if (!global.NativeScriptHasPolyfilled) {
|
||||
console.log('Installing polyfills...');
|
||||
|
||||
// DOM api polyfills
|
||||
const glb = global as any;
|
||||
if (__COMMONJS__) {
|
||||
global.registerModule('timer', () => timer);
|
||||
installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']);
|
||||
@ -371,72 +349,45 @@ if (!global.NativeScriptHasPolyfilled) {
|
||||
global.registerModule('subtle', () => subtleCryptoImpl);
|
||||
installPolyfills('subtle-crypto', ['Subtle']);
|
||||
} else {
|
||||
// @ts-expect-error
|
||||
global.setTimeout = timer.setTimeout;
|
||||
global.clearTimeout = timer.clearTimeout;
|
||||
// @ts-expect-error
|
||||
global.setInterval = timer.setInterval;
|
||||
global.clearInterval = timer.clearInterval;
|
||||
// global.registerModule('timer', () => timer);
|
||||
// installPolyfills('timer', ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']);
|
||||
// timers
|
||||
glb.setTimeout = timer.setTimeout;
|
||||
glb.clearTimeout = timer.clearTimeout;
|
||||
glb.setInterval = timer.setInterval;
|
||||
glb.clearInterval = timer.clearInterval;
|
||||
|
||||
// global.registerModule('animation', () => animationFrame);
|
||||
// installPolyfills('animation', ['requestAnimationFrame', 'cancelAnimationFrame']);
|
||||
global.requestAnimationFrame = animationFrame.requestAnimationFrame;
|
||||
global.cancelAnimationFrame = animationFrame.cancelAnimationFrame;
|
||||
// animation frame
|
||||
glb.requestAnimationFrame = animationFrame.requestAnimationFrame;
|
||||
glb.cancelAnimationFrame = animationFrame.cancelAnimationFrame;
|
||||
|
||||
// global.registerModule('media-query-list', () => mediaQueryList);
|
||||
// installPolyfills('media-query-list', ['matchMedia', 'MediaQueryList']);
|
||||
// @ts-expect-error
|
||||
global.matchMedia = mediaQueryList.matchMedia;
|
||||
// @ts-expect-error
|
||||
global.MediaQueryList = mediaQueryList.MediaQueryList;
|
||||
// media query list
|
||||
glb.matchMedia = mediaQueryList.matchMedia;
|
||||
glb.MediaQueryList = mediaQueryList.MediaQueryList;
|
||||
|
||||
// global.registerModule('text', () => text);
|
||||
// installPolyfills('text', ['TextDecoder', 'TextEncoder']);
|
||||
// @ts-expect-error
|
||||
global.TextDecoder = text.TextDecoder;
|
||||
// @ts-expect-error
|
||||
global.TextEncoder = text.TextEncoder;
|
||||
// text
|
||||
glb.TextDecoder = text.TextDecoder;
|
||||
glb.TextEncoder = text.TextEncoder;
|
||||
|
||||
// global.registerModule('xhr', () => xhrImpl);
|
||||
// installPolyfills('xhr', ['XMLHttpRequest', 'FormData', 'Blob', 'File', 'FileReader']);
|
||||
// @ts-expect-error
|
||||
global.XMLHttpRequest = xhrImpl.XMLHttpRequest;
|
||||
// @ts-expect-error
|
||||
global.FormData = xhrImpl.FormData;
|
||||
// @ts-expect-error
|
||||
global.Blob = xhrImpl.Blob;
|
||||
// @ts-expect-error
|
||||
global.File = xhrImpl.File;
|
||||
// xhr
|
||||
glb.XMLHttpRequest = xhrImpl.XMLHttpRequest;
|
||||
glb.FormData = xhrImpl.FormData;
|
||||
glb.Blob = xhrImpl.Blob;
|
||||
glb.File = xhrImpl.File;
|
||||
|
||||
// global.registerModule('fetch', () => fetchPolyfill);
|
||||
// installPolyfills('fetch', ['fetch', 'Headers', 'Request', 'Response']);
|
||||
// @ts-expect-error
|
||||
global.fetch = fetchPolyfill.fetch;
|
||||
// @ts-expect-error
|
||||
global.Headers = fetchPolyfill.Headers;
|
||||
// @ts-expect-error
|
||||
global.Request = fetchPolyfill.Request;
|
||||
// @ts-expect-error
|
||||
global.Response = fetchPolyfill.Response;
|
||||
// fetch
|
||||
glb.fetch = fetchPolyfill.fetch;
|
||||
glb.Headers = fetchPolyfill.Headers;
|
||||
glb.Request = fetchPolyfill.Request;
|
||||
glb.Response = fetchPolyfill.Response;
|
||||
|
||||
// global.registerModule('wgc', () => wgc);
|
||||
// installPolyfills('wgc', ['atob', 'btoa']);
|
||||
global.atob = wgc.atob;
|
||||
global.btoa = wgc.btoa;
|
||||
// wgc
|
||||
glb.atob = wgc.atob;
|
||||
glb.btoa = wgc.btoa;
|
||||
|
||||
// global.registerModule('crypto', () => cryptoImpl);
|
||||
// installPolyfills('crypto', ['Crypto']);
|
||||
|
||||
// global.registerModule('subtle', () => subtleCryptoImpl);
|
||||
// installPolyfills('subtle-crypto', ['Subtle']);
|
||||
// @ts-expect-error
|
||||
global.SubtleCrypto = subtleCryptoImpl.SubtleCrypto;
|
||||
// wgc
|
||||
glb.SubtleCrypto = subtleCryptoImpl.SubtleCrypto;
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
global.crypto = new cryptoImpl.Crypto();
|
||||
glb.crypto = new cryptoImpl.Crypto();
|
||||
|
||||
// global.registerModule('abortcontroller', () => require('../abortcontroller'));
|
||||
// installPolyfills('abortcontroller', ['AbortController', 'AbortSignal']);
|
||||
|
15
packages/core/module-name-resolver/index.d.ts
vendored
15
packages/core/module-name-resolver/index.d.ts
vendored
@ -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;
|
@ -20,6 +20,7 @@ export class ModuleNameResolver implements ModuleNameResolverType {
|
||||
let result: string = this._cache[key];
|
||||
if (result === undefined) {
|
||||
result = this.resolveModuleNameImpl(path, ext);
|
||||
console.log('resolveModuleName result:', result);
|
||||
this._cache[key] = result;
|
||||
}
|
||||
|
||||
@ -43,13 +44,11 @@ export class ModuleNameResolver implements ModuleNameResolverType {
|
||||
|
||||
const candidates = this.getCandidates(path, ext);
|
||||
result = findMatch(path, ext, candidates, this.context);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private getCandidates(path: string, ext: string): Array<string> {
|
||||
const candidates = this.moduleListProvider().filter((moduleName) => moduleName.startsWith(path) && (!ext || moduleName.endsWith(ext)));
|
||||
|
||||
return candidates;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
@ -27,7 +27,7 @@ const minWidthHeightQualifier: QualifierSpec = {
|
||||
return path.match(new RegExp(`.${MIN_WH}\\d+`, 'g'));
|
||||
},
|
||||
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)) {
|
||||
return -1;
|
||||
}
|
||||
@ -49,7 +49,7 @@ const minWidthQualifier: QualifierSpec = {
|
||||
return path.match(new RegExp(`.${MIN_W}\\d+`, 'g'));
|
||||
},
|
||||
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)) {
|
||||
return -1;
|
||||
}
|
||||
@ -71,7 +71,7 @@ const minHeightQualifier: QualifierSpec = {
|
||||
return path.match(new RegExp(`.${MIN_H}\\d+`, 'g'));
|
||||
},
|
||||
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)) {
|
||||
return -1;
|
||||
}
|
||||
@ -93,7 +93,7 @@ const platformQualifier: QualifierSpec = {
|
||||
return path.match(new RegExp('\\.android|\\.ios', 'g'));
|
||||
},
|
||||
getMatchValue(value: string, context: PlatformContext): number {
|
||||
const val = value.substr(1);
|
||||
const val = value.substring(1);
|
||||
|
||||
return val === context.os.toLowerCase() ? 1 : -1;
|
||||
},
|
||||
@ -107,7 +107,7 @@ const orientationQualifier: QualifierSpec = {
|
||||
return path.match(new RegExp('\\.land|\\.port', 'g'));
|
||||
},
|
||||
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;
|
||||
|
||||
return val === 'land' ? isLandscape : -isLandscape;
|
||||
@ -176,6 +176,8 @@ export function findMatch(path: string, ext: string, candidates: Array<string>,
|
||||
result = candidates[i];
|
||||
}
|
||||
}
|
||||
console.log(' > findMatch called with path:', path, 'and ext:', ext, 'candidates:', candidates, 'context:', context, '--- MATCH? result:', result);
|
||||
console.log('. ');
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -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) {
|
||||
let subObj = instance;
|
||||
const properties = attr.split('.');
|
||||
@ -189,9 +185,6 @@ export function getComponentModule(elementName: string, namespace: string, attri
|
||||
|
||||
const { instance, instanceModule } = createComponentInstance(elementName, namespace, null);
|
||||
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) {
|
||||
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) {
|
||||
// Note: instanceModule can be null if we are loading custom component with no code-behind.
|
||||
if (isBinding(propertyValue) && instance.bind) {
|
||||
console.log('1 setPropertyValue, Binding detected for property:', propertyName, 'with value:', propertyValue);
|
||||
const bindOptions = getBindingOptions(propertyName, getBindingExpressionFromAttribute(propertyValue));
|
||||
instance.bind(
|
||||
{
|
||||
@ -221,8 +213,6 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
|
||||
bindOptions[bindingConstants.source],
|
||||
);
|
||||
} 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.
|
||||
const handler = exports && exports[propertyValue];
|
||||
|
||||
@ -231,10 +221,8 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
|
||||
instance.on(propertyName, handler);
|
||||
}
|
||||
} 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];
|
||||
} else {
|
||||
console.log('4 setPropertyValue, Binding detected for property:', propertyName, 'with value:', propertyValue);
|
||||
instance[propertyName] = propertyValue;
|
||||
}
|
||||
}
|
||||
@ -249,7 +237,6 @@ function isBinding(value: any): boolean {
|
||||
const str = value.trim();
|
||||
isBinding = str.indexOf('{{') === 0 && str.lastIndexOf('}}') === str.length - 2;
|
||||
}
|
||||
console.log('isBinding called with value:', value, ' isBinding:', isBinding);
|
||||
|
||||
return isBinding;
|
||||
}
|
||||
|
@ -73,9 +73,11 @@ export class Builder {
|
||||
return view;
|
||||
} else if (entry.moduleName) {
|
||||
const moduleName = sanitizeModuleName(entry.moduleName);
|
||||
console.log('.');
|
||||
console.log('moduleName:', moduleName);
|
||||
const resolvedCodeModuleName = resolveModuleName(moduleName, ''); //`${moduleName}.xml`;
|
||||
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) {
|
||||
// Exports has a createPage() method
|
||||
const view = moduleExports.createPage();
|
||||
@ -141,7 +143,7 @@ export class Builder {
|
||||
*/
|
||||
static parseMultipleTemplates(value: string, context: any): Array<KeyedTemplate> {
|
||||
const dummyComponent = `<ListView><ListView.itemTemplates>${value}</ListView.itemTemplates></ListView>`;
|
||||
|
||||
console.log('parseMultipleTemplates called with value:', value, 'context:', context);
|
||||
return parseInternal(dummyComponent, context).component['itemTemplates'];
|
||||
}
|
||||
}
|
||||
@ -184,7 +186,6 @@ export function createViewFromEntry(entry: ViewEntry): View {
|
||||
|
||||
function loadInternal(moduleName: string, moduleExports: any): ComponentModule {
|
||||
let componentModule: ComponentModule;
|
||||
console.log('loadInternal called for moduleName:', moduleName, 'with moduleExports:', moduleExports);
|
||||
const resolvedXmlModule = resolveModuleName(moduleName, 'xml');
|
||||
|
||||
if (resolvedXmlModule) {
|
||||
@ -285,7 +286,6 @@ export function getExports(instance: ViewBase): any {
|
||||
}
|
||||
|
||||
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__) {
|
||||
let start: xml2ui.XmlStringParser;
|
||||
let ui: xml2ui.ComponentParser;
|
||||
@ -668,7 +668,6 @@ export namespace xml2ui {
|
||||
|
||||
@profile
|
||||
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) {
|
||||
// Custom components
|
||||
return loadCustomComponent(args.namespace, args.elementName, args.attributes, this.context, this.currentRootView, !this.currentRootView, this.moduleName);
|
||||
|
@ -59,7 +59,7 @@ export function sanitizeModuleName(moduleName: string, removeExtension = true):
|
||||
}
|
||||
|
||||
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');
|
||||
moduleName = moduleName.replace(extensionRegEx, '$1');
|
||||
}
|
||||
|
@ -23,7 +23,7 @@
|
||||
"assets": [
|
||||
{
|
||||
"input": "{projectRoot}/src/stubs",
|
||||
"glob": "*.js",
|
||||
"glob": "*.{js,mjs}",
|
||||
"output": "stubs"
|
||||
},
|
||||
{
|
||||
|
@ -12,7 +12,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
|
||||
const entryPath = getEntryPath();
|
||||
const virtualEntryPath = path.resolve(
|
||||
__dirname,
|
||||
'../stubs/virtual-entry-typescript.js'
|
||||
`../stubs/virtual-entry-typescript.${env.commonjs ? 'js' : 'mjs'}`,
|
||||
);
|
||||
|
||||
// exclude files starting with _ from require.context
|
||||
@ -23,7 +23,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
|
||||
chainedSetAddAfter(
|
||||
config.entry('bundle'),
|
||||
'@nativescript/core/globals/index',
|
||||
virtualEntryPath
|
||||
virtualEntryPath,
|
||||
);
|
||||
|
||||
config.when(env.hmr, (config) => {
|
||||
|
@ -1,5 +1,5 @@
|
||||
// VIRTUAL ENTRY START
|
||||
require('@nativescript/core/bundle-entry-points')
|
||||
const context = require.context("~/", /* deep: */ true, /* filter: */ /.(xml|js|s?css)$/);
|
||||
global.registerWebpackModules(context);
|
||||
global.registerBundlerModules(context);
|
||||
// VIRTUAL ENTRY END
|
@ -1,5 +1,5 @@
|
||||
// VIRTUAL ENTRY START
|
||||
require('@nativescript/core/bundle-entry-points')
|
||||
const context = require.context("~/", /* deep: */ true, /* filter: */ /\.(xml|js|(?<!\.d\.)ts|s?css)$/);
|
||||
global.registerWebpackModules(context);
|
||||
global.registerBundlerModules(context);
|
||||
// VIRTUAL ENTRY END
|
78
packages/webpack5/src/stubs/virtual-entry-typescript.mjs
Normal file
78
packages/webpack5/src/stubs/virtual-entry-typescript.mjs
Normal 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 fast‑glob’s 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
|
Reference in New Issue
Block a user