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

Commit fea5b69

Browse files
committed
fix: support platform specific files that are not directly imported anywhere in the app (e.g. when main.ts is platform specific).
1 parent d6f6672 commit fea5b69

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

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, sep } 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 workaround 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 = parsed.dir + sep + 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: plugins/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ module.exports = Object.assign({},
33
require("./NativeScriptSnapshotPlugin"),
44
require("./PlatformSuffixPlugin"),
55
require("./PlatformFSPlugin"),
6-
require("./WatchStateLoggerPlugin")
6+
require("./WatchStateLoggerPlugin"),
7+
require("./NativeScriptAngularCompilerPlugin")
78
);

Diff for: templates/webpack.angular.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ 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 hashSalt = Date.now().toString();
1716

1817
module.exports = env => {
1918
// Add your custom Activities, Services and other Android app components here.
@@ -27,6 +26,7 @@ module.exports = env => {
2726
throw new Error("You need to provide a target platform!");
2827
}
2928

29+
const AngularCompilerPlugin = nsWebpack.getAngularCompilerPlugin(platform);
3030
const projectRoot = __dirname;
3131

3232
// Default destination inside platforms/<platform>/...
@@ -261,10 +261,10 @@ module.exports = env => {
261261
// configures the WebPack runtime to be generated inside the snapshot
262262
// module and no `runtime.js` module exist.
263263
(snapshot ? [] : ["./runtime"])
264-
.concat([
265-
"./vendor",
266-
"./bundle",
267-
])
264+
.concat([
265+
"./vendor",
266+
"./bundle",
267+
])
268268
),
269269
// For instructions on how to set up workers with webpack
270270
// check out https://github.com/nativescript/worker-loader

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const nativeScriptDevWebpack = {
2222
getEntryModule: () => 'EntryModule',
2323
getResolver: () => null,
2424
getEntryPathRegExp: () => null,
25-
getConvertedExternals: nsWebpackIndex.getConvertedExternals
25+
getConvertedExternals: nsWebpackIndex.getConvertedExternals,
26+
getAngularCompilerPlugin: () => AngularCompilerStub
2627
};
2728

2829
const emptyObject = {};

0 commit comments

Comments
 (0)