Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit ba9b772

Browse files
committed
fix: avoid duplicate modules from tns-core-modules and @nativescript/core causing app crashes on Android
The AngularCompilerPlugin is resolving the files based on the `paths` property from the tsconfig and we have to unify the core modules package name in order to avoid duplicate modules.
1 parent 9358e83 commit ba9b772

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

index.js

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,28 @@ const {
1010
Object.assign(exports, require("./plugins"));
1111
Object.assign(exports, require("./host/resolver"));
1212

13+
exports.processTsPathsForScopedModules = function ({ compilerOptions }) {
14+
return replacePathInCompilerOptions({
15+
compilerOptions,
16+
targetPath: "tns-core-modules",
17+
replacementPath: "@nativescript/core"
18+
});
19+
}
20+
21+
exports.processTsPathsForScopedAngular = function ({ compilerOptions }) {
22+
return replacePathInCompilerOptions({
23+
compilerOptions,
24+
targetPath: "nativescript-angular",
25+
replacementPath: "@nativescript/angular"
26+
});
27+
}
28+
1329
exports.hasRootLevelScopedModules = function ({ projectDir }) {
14-
let hasRootLevelScopedModules;
15-
try {
16-
const scopedModulesPackageName = '@nativescript/core';
17-
require.resolve(scopedModulesPackageName, { paths: [projectDir] });
18-
hasRootLevelScopedModules = true;
19-
} catch (e) {
20-
hasRootLevelScopedModules = false;
21-
}
30+
return hasRootLevelPackage({ projectDir, packageName: "@nativescript/core" });
31+
}
2232

23-
return hasRootLevelScopedModules;
33+
exports.hasRootLevelScopedAngular = function ({ projectDir }) {
34+
return hasRootLevelPackage({ projectDir, packageName: "@nativescript/angular" });
2435
}
2536

2637
exports.getAotEntryModule = function (appDirectory) {
@@ -176,3 +187,32 @@ function verifyEntryModuleDirectory(appDirectory) {
176187
throw new Error(`The specified path to app directory ${appDirectory} does not exist. Unable to find entry module.`);
177188
}
178189
}
190+
191+
192+
function hasRootLevelPackage({ projectDir, packageName }) {
193+
let hasRootLevelPackage;
194+
try {
195+
require.resolve(packageName, { paths: [projectDir] });
196+
hasRootLevelPackage = true;
197+
} catch (e) {
198+
hasRootLevelPackage = false;
199+
}
200+
201+
return hasRootLevelPackage;
202+
}
203+
204+
function replacePathInCompilerOptions({ compilerOptions, targetPath, replacementPath }) {
205+
const paths = (compilerOptions && compilerOptions.paths) || {};
206+
for (const key in paths) {
207+
if (paths.hasOwnProperty(key)) {
208+
const pathsForPattern = paths[key];
209+
if (Array.isArray(pathsForPattern)) {
210+
for (let i = 0; i < pathsForPattern.length; ++i) {
211+
if (typeof pathsForPattern[i] === "string") {
212+
pathsForPattern[i] = pathsForPattern[i].replace(targetPath, replacementPath);
213+
}
214+
}
215+
}
216+
}
217+
}
218+
}

templates/webpack.angular.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { nsReplaceBootstrap } = require("nativescript-dev-webpack/transformers/ns
77
const { nsReplaceLazyLoader } = require("nativescript-dev-webpack/transformers/ns-replace-lazy-loader");
88
const { nsSupportHmrNg } = require("nativescript-dev-webpack/transformers/ns-support-hmr-ng");
99
const { getMainModulePath } = require("nativescript-dev-webpack/utils/ast-utils");
10-
const { getNoEmitOnErrorFromTSConfig } = require("nativescript-dev-webpack/utils/tsconfig-utils");
10+
const { getNoEmitOnErrorFromTSConfig, getCompilerOptionsFromTSConfig } = require("nativescript-dev-webpack/utils/tsconfig-utils");
1111
const CleanWebpackPlugin = require("clean-webpack-plugin");
1212
const CopyWebpackPlugin = require("copy-webpack-plugin");
1313
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
@@ -60,19 +60,28 @@ module.exports = env => {
6060
const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
6161
const externals = nsWebpack.getConvertedExternals(env.externals);
6262
const appFullPath = resolve(projectRoot, appPath);
63+
const tsConfigName = "tsconfig.tns.json";
64+
const tsConfigPath = join(__dirname, tsConfigName);
6365
const hasRootLevelScopedModules = nsWebpack.hasRootLevelScopedModules({ projectDir: projectRoot });
66+
const hasRootLevelScopedAngular = nsWebpack.hasRootLevelScopedAngular({ projectDir: projectRoot });
6467
let coreModulesPackageName = "tns-core-modules";
6568
const alias = {
6669
'~': appFullPath
6770
};
6871

72+
const compilerOptions = getCompilerOptionsFromTSConfig(tsConfigPath);
6973
if (hasRootLevelScopedModules) {
7074
coreModulesPackageName = "@nativescript/core";
7175
alias["tns-core-modules"] = coreModulesPackageName;
76+
nsWebpack.processTsPathsForScopedModules({ compilerOptions });
77+
}
78+
79+
if (hasRootLevelScopedAngular) {
7280
alias["nativescript-angular"] = "@nativescript/angular";
81+
nsWebpack.processTsPathsForScopedAngular({ compilerOptions });
7382
}
83+
7484
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
75-
const tsConfigName = "tsconfig.tns.json";
7685
const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
7786
const entryPath = `.${sep}${entryModule}`;
7887
const entries = { bundle: entryPath };
@@ -109,10 +118,11 @@ module.exports = env => {
109118
hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
110119
platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule), projectRoot)),
111120
mainPath: join(appFullPath, entryModule),
112-
tsConfigPath: join(__dirname, tsConfigName),
121+
tsConfigPath,
113122
skipCodeGeneration: !aot,
114123
sourceMap: !!isAnySourceMapEnabled,
115-
additionalLazyModuleResources: additionalLazyModuleResources
124+
additionalLazyModuleResources: additionalLazyModuleResources,
125+
compilerOptions: { paths: compilerOptions.paths }
116126
});
117127

118128
let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);

0 commit comments

Comments
 (0)