diff --git a/tns-core-modules/file-system/file-system.d.ts b/tns-core-modules/file-system/file-system.d.ts index d3c6e1023..64bd85ff0 100644 --- a/tns-core-modules/file-system/file-system.d.ts +++ b/tns-core-modules/file-system/file-system.d.ts @@ -185,7 +185,7 @@ export class Folder extends FileSystemEntity { * Gets all the top-level entities residing within this folder synchronously. * @param onError An optional function to be called if some error occurs. */ - getEntitiesSync(onError?: (error: any) => any): Promise>; + getEntitiesSync(onError?: (error: any) => any): Array; /** * Enumerates all the top-level FileSystem entities residing within this folder. diff --git a/tns-core-modules/module-name-resolver/module-name-resolver.ts b/tns-core-modules/module-name-resolver/module-name-resolver.ts index 1c618518a..f7945175d 100644 --- a/tns-core-modules/module-name-resolver/module-name-resolver.ts +++ b/tns-core-modules/module-name-resolver/module-name-resolver.ts @@ -2,6 +2,7 @@ import { ModuleNameResolver as ModuleNameResolverDefinition, ModuleListProvider import { screen, device } from "../platform/platform"; import * as appCommonModule from "../application/application-common"; import { PlatformContext, findMatch } from "./qualifier-matcher"; +import { registerModulesFromFileSystem } from "./non-bundle-workflow-compat"; export class ModuleNameResolver implements ModuleNameResolverDefinition { private _cache = {}; @@ -28,7 +29,13 @@ export class ModuleNameResolver implements ModuleNameResolverDefinition { let result: string = null; 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); return result; diff --git a/tns-core-modules/module-name-resolver/non-bundle-workflow-compat.ts b/tns-core-modules/module-name-resolver/non-bundle-workflow-compat.ts new file mode 100644 index 000000000..e79c4188a --- /dev/null +++ b/tns-core-modules/module-name-resolver/non-bundle-workflow-compat.ts @@ -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(); +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; + } +} \ No newline at end of file