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

Commit e1e9463

Browse files
author
Dimitar Tachev
authored
fix: support platform specific files that are not directly imported anywhere in the app (#843)
* fix: support platform specific files that are not directly imported anywhere in the app (e.g. when main.ts is platform specific). * fix: support platform specific entry modules
1 parent 7034a97 commit e1e9463

8 files changed

+51
-12
lines changed

Diff for: index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ exports.getAotEntryModule = function (appDirectory) {
2525
return aotEntry;
2626
}
2727

28-
exports.getEntryModule = function (appDirectory) {
28+
exports.getEntryModule = function (appDirectory, platform) {
2929
verifyEntryModuleDirectory(appDirectory);
3030

3131
const entry = getPackageJsonEntry(appDirectory);
3232

3333
const tsEntryPath = path.resolve(appDirectory, `${entry}.ts`);
3434
const jsEntryPath = path.resolve(appDirectory, `${entry}.js`);
35-
if (!existsSync(tsEntryPath) && !existsSync(jsEntryPath)) {
35+
let entryExists = existsSync(tsEntryPath) || existsSync(jsEntryPath);
36+
if (!entryExists && platform) {
37+
const platformTsEntryPath = path.resolve(appDirectory, `${entry}.${platform}.ts`);
38+
const platformJsEntryPath = path.resolve(appDirectory, `${entry}.${platform}.js`);
39+
entryExists = existsSync(platformTsEntryPath) || existsSync(platformJsEntryPath);
40+
}
41+
42+
if (!entryExists) {
3643
throw new Error(`The entry module ${entry} specified in ` +
3744
`${appDirectory}/package.json doesn't exist!`)
3845
}

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
},
101101
"devDependencies": {
102102
"@ngtools/webpack": "~7.2.0",
103+
"@angular/compiler": "~7.2.0",
104+
"@angular/compiler-cli": "~7.2.0",
103105
"@types/jasmine": "^3.3.7",
104106
"@types/node": "^10.12.12",
105107
"@types/proxyquire": "1.3.28",

Diff for: plugins/NativeScriptAngularCompilerPlugin.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { parse, join } from "path";
2+
import { AngularCompilerPlugin } from "@ngtools/webpack";
3+
4+
export function getAngularCompilerPlugin(platform: string): any {
5+
class NativeScriptAngularCompilerPlugin extends AngularCompilerPlugin {
6+
// This is the bridge between the @ngtols/webpack loader and the AngularCompilerPlugin plugin itself:
7+
// https://github.com/angular/angular-cli/blob/bf52b26219ffc16bed2dd55716e21773b415fd2a/packages/ngtools/webpack/src/loader.ts#L49
8+
// The problem is that the loader does not call the `hostReplacementPaths` method when asking for the compiledFile.
9+
// By overriding this method, we work around this issue and support platform specific files from the loader
10+
// that are not compiled by the AngularCompilerPlugin plugin. e.g. main.ts is the webpack entry point and
11+
// it's loaded by the @ngtools/webpack loader but its not compiled by the plugin because the TypeScript Compiler
12+
// knowns only about main.android.ts and main.ios.ts (main.ts is not imported/required anywhere in the app).
13+
getCompiledFile(file) {
14+
try {
15+
if (platform) {
16+
const parsed = parse(file);
17+
const platformFile = join(parsed.dir, `${parsed.name}.${platform}${parsed.ext}`);
18+
return super.getCompiledFile(platformFile);
19+
}
20+
}
21+
catch (e) { }
22+
23+
return super.getCompiledFile(file);
24+
}
25+
}
26+
27+
return NativeScriptAngularCompilerPlugin;
28+
}

Diff for: templates/webpack.angular.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ const CopyWebpackPlugin = require("copy-webpack-plugin");
1212
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
1313
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
1414
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
15-
const { AngularCompilerPlugin } = require("@ngtools/webpack");
16-
const hashSalt = Date.now().toString();
15+
const { getAngularCompilerPlugin } = require("nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin");
16+
const hashSalt = Date.now().toString();
1717

1818
module.exports = env => {
1919
// Add your custom Activities, Services and other Android app components here.
@@ -27,6 +27,7 @@ module.exports = env => {
2727
throw new Error("You need to provide a target platform!");
2828
}
2929

30+
const AngularCompilerPlugin = getAngularCompilerPlugin(platform);
3031
const projectRoot = __dirname;
3132

3233
// Default destination inside platforms/<platform>/...
@@ -54,7 +55,7 @@ module.exports = env => {
5455
const appFullPath = resolve(projectRoot, appPath);
5556
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
5657
const tsConfigName = "tsconfig.tns.json";
57-
const entryModule = `${nsWebpack.getEntryModule(appFullPath)}.ts`;
58+
const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
5859
const entryPath = `.${sep}${entryModule}`;
5960
const entries = { bundle: entryPath };
6061
if (platform === "ios") {
@@ -261,10 +262,10 @@ module.exports = env => {
261262
// configures the WebPack runtime to be generated inside the snapshot
262263
// module and no `runtime.js` module exist.
263264
(snapshot ? [] : ["./runtime"])
264-
.concat([
265-
"./vendor",
266-
"./bundle",
267-
])
265+
.concat([
266+
"./vendor",
267+
"./bundle",
268+
])
268269
),
269270
// For instructions on how to set up workers with webpack
270271
// check out https://github.com/nativescript/worker-loader

Diff for: templates/webpack.config.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const webpackConfigAngular = proxyquire('./webpack.angular', {
3636
'nativescript-dev-webpack/transformers/ns-replace-lazy-loader': { nsReplaceLazyLoader: () => { return FakeLazyTransformerFlag } },
3737
'nativescript-dev-webpack/transformers/ns-support-hmr-ng': { nsSupportHmrNg: () => { return FakeHmrTransformerFlag } },
3838
'nativescript-dev-webpack/utils/ast-utils': { getMainModulePath: () => { return "fakePath"; } },
39+
'nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin': { getAngularCompilerPlugin: () => { return AngularCompilerStub; } },
3940
'@ngtools/webpack': {
4041
AngularCompilerPlugin: AngularCompilerStub
4142
}

Diff for: templates/webpack.javascript.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = env => {
4949
const appFullPath = resolve(projectRoot, appPath);
5050
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
5151

52-
const entryModule = nsWebpack.getEntryModule(appFullPath);
52+
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
5353
const entryPath = `.${sep}${entryModule}.js`;
5454
const entries = { bundle: entryPath };
5555
if (platform === "ios") {

Diff for: templates/webpack.typescript.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = env => {
4949
const appFullPath = resolve(projectRoot, appPath);
5050
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
5151

52-
const entryModule = nsWebpack.getEntryModule(appFullPath);
52+
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
5353
const entryPath = `.${sep}${entryModule}.ts`;
5454
const entries = { bundle: entryPath };
5555
if (platform === "ios") {

Diff for: templates/webpack.vue.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = env => {
5656
const appFullPath = resolve(projectRoot, appPath);
5757
const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
5858

59-
const entryModule = nsWebpack.getEntryModule(appFullPath);
59+
const entryModule = nsWebpack.getEntryModule(appFullPath, platform);
6060
const entryPath = `.${sep}${entryModule}`;
6161
const entries = { bundle: entryPath };
6262
if (platform === "ios") {

0 commit comments

Comments
 (0)