UNPKG

4.67 kBJavaScriptView Raw
1import { Application } from '../application';
2import * as fs from '../file-system';
3import { Trace } from '../trace';
4const cache = new Set();
5let initialized = false;
6function register(name, loader) {
7 if (Trace.isEnabled()) {
8 Trace.write(`[Compat] Register module: ${name}`, Trace.categories.ModuleNameResolver);
9 }
10 global.registerModule(name, loader);
11 if (name.startsWith('tns_modules')) {
12 const nonTnsModulesName = name.substr('tns_modules'.length + 1);
13 if (Trace.isEnabled()) {
14 Trace.write(`[Compat] Register module: ${nonTnsModulesName}`, Trace.categories.ModuleNameResolver);
15 }
16 global.registerModule(nonTnsModulesName, loader);
17 }
18}
19function processFile(file) {
20 const filePathRelativeToApp = file.path.substr(fs.knownFolders.currentApp().path.length + 1);
21 const loadContent = () => file.readTextSync();
22 switch (file.extension.toLocaleLowerCase()) {
23 case '.js': {
24 const noExtPath = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - '.js'.length);
25 register(filePathRelativeToApp, function () {
26 return global.require(file.path);
27 });
28 register(noExtPath, function () {
29 return global.require(file.path);
30 });
31 break;
32 }
33 case '.css':
34 register(filePathRelativeToApp, loadContent);
35 break;
36 case '.xml':
37 register(filePathRelativeToApp, loadContent);
38 break;
39 }
40 if (file.name === 'package.json') {
41 const json = global.require(file.path);
42 if (json.main) {
43 const name = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - 'package.json'.length - 1);
44 const requirePath = fs.path.join(file.parent.path, json.main);
45 register(name, () => global.require(requirePath));
46 }
47 }
48}
49/**
50 * Processes folder and returns true if folder was not empty.
51 * @param Folder path
52 */
53function processFolder(path) {
54 if (cache.has(path)) {
55 return true;
56 }
57 cache.add(path);
58 if (Trace.isEnabled()) {
59 Trace.write(`[Compat] Processing folder: ${path}`, Trace.categories.ModuleNameResolver);
60 }
61 let folderEmpty = true;
62 if (fs.Folder.exists(path)) {
63 const folder = fs.Folder.fromPath(path);
64 folder.eachEntity((file) => {
65 if (file instanceof fs.File) {
66 processFile(file);
67 folderEmpty = false;
68 }
69 return true;
70 });
71 }
72 return !folderEmpty;
73}
74/**
75 * Registers loaders for all files from the containing folder with global.registerModule().
76 * Compatibility method for non-webpack workflow (like in playground).
77 * @param moduleName
78 */
79export function registerModulesFromFileSystem(moduleName) {
80 initialize();
81 let folderProcessed = false;
82 let parentFolderProcessed = false;
83 // moduleName is a folder with package.json
84 const path = fs.path.join(fs.knownFolders.currentApp().path, moduleName);
85 if (fs.Folder.exists(path)) {
86 folderProcessed = processFolder(path);
87 }
88 // moduleName is file - load all files in its parent folder
89 const parentName = moduleName.substr(0, moduleName.lastIndexOf(fs.path.separator));
90 const parentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, parentName);
91 if (fs.Folder.exists(parentFolderPath)) {
92 parentFolderProcessed = processFolder(parentFolderPath);
93 }
94 // Return only if we processed the actual folder or its parent folder.
95 // If the parent folder is empty we should still check tns_modules
96 // as this might be just a name of a plugin (ex. "nativescript-ui-autocomplete")
97 if (folderProcessed || (parentFolderProcessed && parentName)) {
98 return;
99 }
100 // moduleName is a folder in tns_modules ex. "nativescript-ui-chart"
101 const tnsModulesPath = fs.path.join(fs.knownFolders.currentApp().path, 'tns_modules', moduleName);
102 if (fs.Folder.exists(tnsModulesPath)) {
103 processFolder(tnsModulesPath);
104 }
105 // moduleName a file in tns_modules/plugin. Avoid traversing the whole tns_modules folder if parentName is empty
106 if (parentName) {
107 const tnsParentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, 'tns_modules', parentName);
108 if (fs.Folder.exists(tnsParentFolderPath)) {
109 processFolder(tnsParentFolderPath);
110 }
111 }
112}
113function initialize() {
114 if (!initialized) {
115 Application.on('livesync', (args) => cache.clear());
116 initialized = true;
117 }
118}
119//# sourceMappingURL=non-bundle-workflow-compat.js.map
\No newline at end of file