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

Commit 0de2532

Browse files
committed
feat: add platform matching host for AngularCompilerPlugin
1 parent 5732d00 commit 0de2532

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ plugins/WatchStateLoggerPlugin.d.ts
1515
plugins/WatchStateLoggerPlugin.js
1616
plugins/WatchStateLoggerPlugin.js.map
1717

18+
host/platform.d.ts
19+
host/platform.js
20+
host/platform.js.map
21+
1822
hooks
1923
.DS_Store

host/platform.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import {
2+
parse,
3+
join,
4+
} from "path";
5+
import { statSync } from "fs";
6+
7+
import { Observable } from "rxjs";
8+
import {
9+
Path,
10+
PathFragment,
11+
} from "@angular-devkit/core";
12+
import {
13+
FileBuffer,
14+
Host,
15+
HostCapabilities,
16+
HostWatchOptions,
17+
HostWatchEvent,
18+
Stats,
19+
} from "@angular-devkit/core/src/virtual-fs/host";
20+
const { NodeJsSyncHost } = require("@angular-devkit/core/node");
21+
22+
23+
export class PlatformReplacementHost<StatsT extends object = {}> implements Host<StatsT> {
24+
constructor(protected _platforms: string[], protected _delegate = new NodeJsSyncHost()) {
25+
}
26+
27+
protected _resolve(path) {
28+
const { dir, name, ext } = parse(path);
29+
30+
for (const platform of this._platforms) {
31+
const newPath = join(dir, `${name}.${platform}${ext}`);
32+
33+
try {
34+
const stat = statSync(newPath);
35+
return stat && stat.isFile() ?
36+
newPath :
37+
path;
38+
} catch(_e) {
39+
// continue checking the other platforms
40+
}
41+
}
42+
43+
return path;
44+
}
45+
46+
get capabilities(): HostCapabilities {
47+
return this._delegate.capabilities;
48+
}
49+
50+
write(path: Path, content: FileBuffer): Observable<void> {
51+
return this._delegate.write(this._resolve(path), content);
52+
}
53+
read(path: Path): Observable<FileBuffer> {
54+
return this._delegate.read(this._resolve(path));
55+
}
56+
delete(path: Path): Observable<void> {
57+
return this._delegate.delete(this._resolve(path));
58+
}
59+
rename(from: Path, to: Path): Observable<void> {
60+
return this._delegate.rename(this._resolve(from), this._resolve(to));
61+
}
62+
63+
list(path: Path): Observable<PathFragment[]> {
64+
return this._delegate.list(this._resolve(path));
65+
}
66+
67+
exists(path: Path): Observable<boolean> {
68+
return this._delegate.exists(this._resolve(path));
69+
}
70+
isDirectory(path: Path): Observable<boolean> {
71+
return this._delegate.isDirectory(this._resolve(path));
72+
}
73+
isFile(path: Path): Observable<boolean> {
74+
return this._delegate.isFile(this._resolve(path));
75+
}
76+
77+
stat(path: Path): Observable<Stats<StatsT>> | null {
78+
return this._delegate.stat(this._resolve(path));
79+
}
80+
81+
watch(path: Path, options?: HostWatchOptions): Observable<HostWatchEvent> | null {
82+
return this._delegate.watch(this._resolve(path), options);
83+
}
84+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@
7777
"tapable": "1.0.0"
7878
},
7979
"devDependencies": {
80+
"@angular-devkit/core": "^0.6.3",
8081
"@types/node": "^8.0.0",
8182
"conventional-changelog-cli": "^1.3.22",
83+
"rxjs": "^6.2.0",
8284
"source-map-support": "^0.5.0",
8385
"typescript": "~2.7.2"
8486
}

templates/webpack.angular.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { join, relative, resolve, sep } = require("path");
33
const webpack = require("webpack");
44
const nsWebpack = require("nativescript-dev-webpack");
55
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
6+
const { PlatformReplacementHost } = require("nativescript-dev-webpack/host/platform");
67
const CleanWebpackPlugin = require("clean-webpack-plugin");
78
const CopyWebpackPlugin = require("copy-webpack-plugin");
89
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
@@ -22,6 +23,9 @@ module.exports = env => {
2223
throw new Error("You need to provide a target platform!");
2324
}
2425

26+
const extensions = ["tns", platform];
27+
const platformHost = new PlatformReplacementHost(extensions);
28+
2529
const projectRoot = __dirname;
2630

2731
// Default destination inside platforms/<platform>/...
@@ -220,6 +224,7 @@ module.exports = env => {
220224
new NativeScriptWorkerPlugin(),
221225

222226
new AngularCompilerPlugin({
227+
host: platformHost,
223228
entryModule: resolve(appPath, "app.module#AppModule"),
224229
tsConfigPath: join(__dirname, "tsconfig.esm.json"),
225230
skipCodeGeneration: !aot,

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"files": [
1111
"plugins/PlatformFSPlugin.ts",
12-
"plugins/WatchStateLoggerPlugin.ts"
12+
"plugins/WatchStateLoggerPlugin.ts",
13+
"host/platform.ts"
1314
]
1415
}

0 commit comments

Comments
 (0)