mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat: compat mode for non-webpack apps (playground) (#7479)
This commit is contained in:
committed by
Manol Donev
parent
20c6d71c70
commit
eed86b4a01
@@ -185,7 +185,7 @@ export class Folder extends FileSystemEntity {
|
|||||||
* Gets all the top-level entities residing within this folder synchronously.
|
* Gets all the top-level entities residing within this folder synchronously.
|
||||||
* @param onError An optional function to be called if some error occurs.
|
* @param onError An optional function to be called if some error occurs.
|
||||||
*/
|
*/
|
||||||
getEntitiesSync(onError?: (error: any) => any): Promise<Array<FileSystemEntity>>;
|
getEntitiesSync(onError?: (error: any) => any): Array<FileSystemEntity>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enumerates all the top-level FileSystem entities residing within this folder.
|
* Enumerates all the top-level FileSystem entities residing within this folder.
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { ModuleNameResolver as ModuleNameResolverDefinition, ModuleListProvider
|
|||||||
import { screen, device } from "../platform/platform";
|
import { screen, device } from "../platform/platform";
|
||||||
import * as appCommonModule from "../application/application-common";
|
import * as appCommonModule from "../application/application-common";
|
||||||
import { PlatformContext, findMatch } from "./qualifier-matcher";
|
import { PlatformContext, findMatch } from "./qualifier-matcher";
|
||||||
|
import { registerModulesFromFileSystem } from "./non-bundle-workflow-compat";
|
||||||
|
|
||||||
export class ModuleNameResolver implements ModuleNameResolverDefinition {
|
export class ModuleNameResolver implements ModuleNameResolverDefinition {
|
||||||
private _cache = {};
|
private _cache = {};
|
||||||
@@ -28,7 +29,13 @@ export class ModuleNameResolver implements ModuleNameResolverDefinition {
|
|||||||
let result: string = null;
|
let result: string = null;
|
||||||
ext = ext ? "." + ext : "";
|
ext = ext ? "." + ext : "";
|
||||||
|
|
||||||
const candidates = this.getCandidates(path, ext);
|
// Compatibility path for non-webpack workflow
|
||||||
|
// register modules from FS first
|
||||||
|
if (!global.TNS_WEBPACK) {
|
||||||
|
registerModulesFromFileSystem(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
let candidates = this.getCandidates(path, ext);
|
||||||
result = findMatch(path, ext, candidates, this.context);
|
result = findMatch(path, ext, candidates, this.context);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -0,0 +1,116 @@
|
|||||||
|
import * as fs from "../file-system/file-system";
|
||||||
|
import * as appCommonModule from "../application/application-common";
|
||||||
|
|
||||||
|
const appFolder = fs.knownFolders.currentApp();
|
||||||
|
const cache = new Set<string>();
|
||||||
|
let initialized = false;
|
||||||
|
|
||||||
|
function register(name, loader) {
|
||||||
|
global.registerModule(name, loader);
|
||||||
|
}
|
||||||
|
|
||||||
|
function processFile(file: fs.File) {
|
||||||
|
const filePathRelativeToApp = file.path.substr(appFolder.path.length + 1);
|
||||||
|
const loadContent = () => file.readTextSync();
|
||||||
|
|
||||||
|
switch (file.extension.toLocaleLowerCase()) {
|
||||||
|
case ".js":
|
||||||
|
const noExtPath = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - ".js".length);
|
||||||
|
|
||||||
|
register(filePathRelativeToApp, function () { return global.require(file.path); });
|
||||||
|
register(noExtPath, function () { return global.require(file.path); });
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ".css":
|
||||||
|
register(filePathRelativeToApp, loadContent);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ".xml":
|
||||||
|
register(filePathRelativeToApp, loadContent);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.name === "package.json") {
|
||||||
|
const json = global.require(file.path);
|
||||||
|
if (json.main) {
|
||||||
|
let name = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - "package.json".length - 1);
|
||||||
|
let requirePath = fs.path.join(file.parent.path, json.main);
|
||||||
|
|
||||||
|
if (name.startsWith("tns_modules")) {
|
||||||
|
name = name.substr("tns_modules".length + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
register(name, () => global.require(requirePath));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function processFolder(path: string) {
|
||||||
|
if (fs.Folder.exists(path)) {
|
||||||
|
const folder = fs.Folder.fromPath(path);
|
||||||
|
|
||||||
|
folder.eachEntity((file) => {
|
||||||
|
if (file instanceof fs.File) {
|
||||||
|
processFile(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers loaders for all files from the containing folder with global.registerModule().
|
||||||
|
* Compatibility method for non-webpack workflow (like in playground).
|
||||||
|
* @param moduleName
|
||||||
|
*/
|
||||||
|
export function registerModulesFromFileSystem(moduleName: string) {
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
if (cache.has(moduleName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cache.add(moduleName);
|
||||||
|
|
||||||
|
// moduleName is a folder with package.json
|
||||||
|
const path = fs.path.join(fs.knownFolders.currentApp().path, moduleName);
|
||||||
|
if (fs.Folder.exists(path)) {
|
||||||
|
processFolder(path);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// moduleName is file - load all files in it's folder
|
||||||
|
const parentName = moduleName.substr(0, moduleName.lastIndexOf(fs.path.separator));
|
||||||
|
const parentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, parentName);
|
||||||
|
if (fs.Folder.exists(parentFolderPath)) {
|
||||||
|
processFolder(parentFolderPath);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// moduleName is a folder in tns_modules ex. "nativescript-ui-chart"
|
||||||
|
const tnsModulesPath = fs.path.join(fs.knownFolders.currentApp().path, "tns_modules", moduleName);
|
||||||
|
if (fs.Folder.exists(tnsModulesPath)) {
|
||||||
|
processFolder(tnsModulesPath);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
if (fs.Folder.exists(tnsParentFolderPath)) {
|
||||||
|
processFolder(tnsParentFolderPath);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function initialize() {
|
||||||
|
if (!initialized) {
|
||||||
|
appCommonModule.on("livesync", args => cache.clear());
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user