mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 13:22:19 +08:00
refactor: Application modules to classes
This commit is contained in:
@ -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