UNPKG

3.92 kBJavaScriptView Raw
1const path = require("path");
2const { existsSync } = require("fs");
3const { ANDROID_APP_PATH } = require("./androidProjectHelpers");
4const {
5 getPackageJson,
6 isAndroid,
7 isIos,
8} = require("./projectHelpers");
9
10Object.assign(exports, require("./plugins"));
11Object.assign(exports, require("./host/resolver"));
12
13exports.getAotEntryModule = function (appDirectory) {
14 verifyEntryModuleDirectory(appDirectory);
15
16 const entry = getPackageJsonEntry(appDirectory);
17 const aotEntry = `${entry}.aot.ts`;
18
19 const aotEntryPath = path.resolve(appDirectory, aotEntry);
20 if (!existsSync(aotEntryPath)) {
21 throw new Error(`For ahead-of-time compilation you need to have an entry module ` +
22 `at ${aotEntryPath} that bootstraps the app with a static platform instead of dynamic one!`)
23 }
24
25 return aotEntry;
26}
27
28exports.getEntryModule = function (appDirectory) {
29 verifyEntryModuleDirectory(appDirectory);
30
31 const entry = getPackageJsonEntry(appDirectory);
32
33 const tsEntryPath = path.resolve(appDirectory, `${entry}.ts`);
34 const jsEntryPath = path.resolve(appDirectory, `${entry}.js`);
35 if (!existsSync(tsEntryPath) && !existsSync(jsEntryPath)) {
36 throw new Error(`The entry module ${entry} specified in ` +
37 `${appDirectory}/package.json doesn't exist!`)
38 }
39
40 return entry;
41};
42
43exports.getAppPath = (platform, projectDir) => {
44 if (isIos(platform)) {
45 const appName = path.basename(projectDir);
46 const sanitizedName = sanitize(appName);
47
48 return `platforms/ios/${sanitizedName}/app`;
49 } else if (isAndroid(platform)) {
50 return ANDROID_APP_PATH;
51 } else {
52 throw new Error(`Invalid platform: ${platform}`);
53 }
54};
55
56exports.getEntryPathRegExp = (appFullPath, entryModule) => {
57 const entryModuleFullPath = path.join(appFullPath, entryModule);
58 // Windows paths contain `\`, so we need to convert each of the `\` to `\\`, so it will be correct inside RegExp
59 const escapedPath = entryModuleFullPath.replace(/\\/g, "\\\\");
60 return new RegExp(escapedPath);
61}
62/**
63 * Converts an array of strings externals to an array of regular expressions.
64 * Input is an array of string, which we need to convert to regular expressions, so all imports for this module will be treated as externals.
65
66 * For example, in case we want nativescript-vue to be external, we will pass `["nativescript-vue"]`.
67 * If we pass it to webpack in this way, it will treat all `require("nativescript-vue")` as externals.
68 * However, you may import some file/subdir of the module, for example `require("nativescript-vue/somedir/file1")`.
69 * To treat this as external, we convert the strings to regular expressions, which makes webpack exclude all imports of the module.
70 * @param {string[]} externals Array of strings.
71 * @returns {RegExp[]} Array of regular expressions constructed from the input strings. In case the input is nullable, an empty array is returned.
72 */
73exports.getConvertedExternals = (externals) => {
74 const modifiedExternals = (externals || []).map((e) => {
75 return new RegExp(`^${e}((/.*)|$)`);
76 });
77
78 return modifiedExternals;
79};
80
81const sanitize = name => name
82 .split("")
83 .filter(char => /[a-zA-Z0-9]/.test(char))
84 .join("");
85
86function getPackageJsonEntry(appDirectory) {
87 const packageJsonSource = getPackageJson(appDirectory);
88 const entry = packageJsonSource.main;
89
90 if (!entry) {
91 throw new Error(`${appDirectory}/package.json must contain a 'main' attribute!`);
92 }
93
94 return entry.replace(/\.js$/i, "");
95}
96
97function verifyEntryModuleDirectory(appDirectory) {
98 if (!appDirectory) {
99 throw new Error("Path to app directory is not specified. Unable to find entry module.");
100 }
101
102 if (!existsSync(appDirectory)) {
103 throw new Error(`The specified path to app directory ${appDirectory} does not exist. Unable to find entry module.`);
104 }
105}