mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 05:18:39 +08:00
refactor: Application modules to classes
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
import { Screen, Device } from '../platform';
|
||||
import * as appCommonModule from '../application/application-common';
|
||||
import { PlatformContext, findMatch, stripQualifiers } from './qualifier-matcher';
|
||||
import { registerModulesFromFileSystem } from './non-bundle-workflow-compat';
|
||||
import { Trace } from '../trace';
|
||||
import { Application } from '../application';
|
||||
|
||||
export type { PlatformContext } from './qualifier-matcher';
|
||||
|
||||
@ -11,7 +11,15 @@ export type ModuleListProvider = () => string[];
|
||||
export class ModuleNameResolver {
|
||||
private _cache = {};
|
||||
|
||||
constructor(private context: PlatformContext, private moduleListProvider: ModuleListProvider = global.getRegisteredModules) {}
|
||||
constructor(
|
||||
private context: PlatformContext,
|
||||
private moduleListProvider: ModuleListProvider = global.getRegisteredModules
|
||||
) {
|
||||
Application.on('livesync', (args) => clearCache());
|
||||
Application.on('orientationChanged', (args) => {
|
||||
resolverInstance = undefined;
|
||||
});
|
||||
}
|
||||
|
||||
public resolveModuleName(path: string, ext: string): string {
|
||||
const key = path + ext;
|
||||
@ -22,7 +30,10 @@ export class ModuleNameResolver {
|
||||
}
|
||||
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(`path: '${path}' with ext: '${ext}' resolved: '${result}'`, Trace.categories.ModuleNameResolver);
|
||||
Trace.write(
|
||||
`path: '${path}' with ext: '${ext}' resolved: '${result}'`,
|
||||
Trace.categories.ModuleNameResolver
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -46,7 +57,9 @@ export class ModuleNameResolver {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@ -72,7 +85,10 @@ export function resolveModuleName(path: string, ext: string): string {
|
||||
}
|
||||
|
||||
function resolveModuleSnapshot(path, ext) {
|
||||
Trace.write(`Resolving module in SNAPSHOT context - path: '${path}' with ext: '${ext}'`, Trace.categories.ModuleNameResolver);
|
||||
Trace.write(
|
||||
`Resolving module in SNAPSHOT context - path: '${path}' with ext: '${ext}'`,
|
||||
Trace.categories.ModuleNameResolver
|
||||
);
|
||||
|
||||
// Platform module when in snapshot. So resolve modules with default android phone.
|
||||
// NB: The only module name that should ever be resolved while in snapshot is app.css, because it is
|
||||
@ -94,8 +110,3 @@ export function clearCache() {
|
||||
export function _setResolver(resolver: ModuleNameResolver) {
|
||||
resolverInstance = resolver;
|
||||
}
|
||||
|
||||
appCommonModule.on('livesync', (args) => clearCache());
|
||||
appCommonModule.on('orientationChanged', (args) => {
|
||||
resolverInstance = undefined;
|
||||
});
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Application } from '../application';
|
||||
import * as fs from '../file-system';
|
||||
import * as appCommonModule from '../application/application-common';
|
||||
import { Trace } from '../trace';
|
||||
|
||||
const cache = new Set<string>();
|
||||
@ -14,19 +14,27 @@ function register(name: string, loader: (name?: string) => void) {
|
||||
if (name.startsWith('tns_modules')) {
|
||||
const nonTnsModulesName = name.substr('tns_modules'.length + 1);
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(`[Compat] Register module: ${nonTnsModulesName}`, Trace.categories.ModuleNameResolver);
|
||||
Trace.write(
|
||||
`[Compat] Register module: ${nonTnsModulesName}`,
|
||||
Trace.categories.ModuleNameResolver
|
||||
);
|
||||
}
|
||||
global.registerModule(nonTnsModulesName, loader);
|
||||
}
|
||||
}
|
||||
|
||||
function processFile(file: fs.File) {
|
||||
const filePathRelativeToApp = file.path.substr(fs.knownFolders.currentApp().path.length + 1);
|
||||
const filePathRelativeToApp = file.path.substr(
|
||||
fs.knownFolders.currentApp().path.length + 1
|
||||
);
|
||||
const loadContent = () => file.readTextSync();
|
||||
|
||||
switch (file.extension.toLocaleLowerCase()) {
|
||||
case '.js': {
|
||||
const noExtPath = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - '.js'.length);
|
||||
const noExtPath = filePathRelativeToApp.substr(
|
||||
0,
|
||||
filePathRelativeToApp.length - '.js'.length
|
||||
);
|
||||
|
||||
register(filePathRelativeToApp, function () {
|
||||
return global.require(file.path);
|
||||
@ -49,7 +57,10 @@ function processFile(file: fs.File) {
|
||||
if (file.name === 'package.json') {
|
||||
const json = global.require(file.path);
|
||||
if (json.main) {
|
||||
const name = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - 'package.json'.length - 1);
|
||||
const name = filePathRelativeToApp.substr(
|
||||
0,
|
||||
filePathRelativeToApp.length - 'package.json'.length - 1
|
||||
);
|
||||
const requirePath = fs.path.join(file.parent.path, json.main);
|
||||
|
||||
register(name, () => global.require(requirePath));
|
||||
@ -68,7 +79,10 @@ function processFolder(path: string): boolean {
|
||||
cache.add(path);
|
||||
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write(`[Compat] Processing folder: ${path}`, Trace.categories.ModuleNameResolver);
|
||||
Trace.write(
|
||||
`[Compat] Processing folder: ${path}`,
|
||||
Trace.categories.ModuleNameResolver
|
||||
);
|
||||
}
|
||||
|
||||
let folderEmpty = true;
|
||||
@ -120,14 +134,22 @@ export function registerModulesFromFileSystem(moduleName: string) {
|
||||
}
|
||||
|
||||
// moduleName is a folder in tns_modules ex. "nativescript-ui-chart"
|
||||
const tnsModulesPath = fs.path.join(fs.knownFolders.currentApp().path, 'tns_modules', moduleName);
|
||||
const tnsModulesPath = fs.path.join(
|
||||
fs.knownFolders.currentApp().path,
|
||||
'tns_modules',
|
||||
moduleName
|
||||
);
|
||||
if (fs.Folder.exists(tnsModulesPath)) {
|
||||
processFolder(tnsModulesPath);
|
||||
}
|
||||
|
||||
// moduleName a file in tns_modules/plugin. Avoid traversing the whole tns_modules folder if parentName is empty
|
||||
if (parentName) {
|
||||
const tnsParentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, 'tns_modules', parentName);
|
||||
const tnsParentFolderPath = fs.path.join(
|
||||
fs.knownFolders.currentApp().path,
|
||||
'tns_modules',
|
||||
parentName
|
||||
);
|
||||
if (fs.Folder.exists(tnsParentFolderPath)) {
|
||||
processFolder(tnsParentFolderPath);
|
||||
}
|
||||
@ -136,7 +158,7 @@ export function registerModulesFromFileSystem(moduleName: string) {
|
||||
|
||||
function initialize() {
|
||||
if (!initialized) {
|
||||
appCommonModule.on('livesync', (args) => cache.clear());
|
||||
Application.on('livesync', (args) => cache.clear());
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user