UNPKG

3.57 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
56/**
57 * Converts an array of strings externals to an array of regular expressions.
58 * 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.
59
60 * For example, in case we want nativescript-vue to be external, we will pass `["nativescript-vue"]`.
61 * If we pass it to webpack in this way, it will treat all `require("nativescript-vue")` as externals.
62 * However, you may import some file/subdir of the module, for example `require("nativescript-vue/somedir/file1")`.
63 * To treat this as external, we convert the strings to regular expressions, which makes webpack exclude all imports of the module.
64 * @param {string[]} externals Array of strings.
65 * @returns {RegExp[]} Array of regular expressions constructed from the input strings. In case the input is nullable, an empty array is returned.
66 */
67exports.getConvertedExternals = (externals) => {
68 const modifiedExternals = (externals || []).map((e) => {
69 return new RegExp(`^${e}((/.*)|$)`);
70 });
71
72 return modifiedExternals;
73};
74
75const sanitize = name => name
76 .split("")
77 .filter(char => /[a-zA-Z0-9]/.test(char))
78 .join("");
79
80function getPackageJsonEntry(appDirectory) {
81 const packageJsonSource = getPackageJson(appDirectory);
82 const entry = packageJsonSource.main;
83
84 if (!entry) {
85 throw new Error(`${appDirectory}/package.json must contain a 'main' attribute!`);
86 }
87
88 return entry.replace(/\.js$/i, "");
89}
90
91function verifyEntryModuleDirectory(appDirectory) {
92 if (!appDirectory) {
93 throw new Error("Path to app directory is not specified. Unable to find entry module.");
94 }
95
96 if (!existsSync(appDirectory)) {
97 throw new Error(`The specified path to app directory ${appDirectory} does not exist. Unable to find entry module.`);
98 }
99}