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

Commit 979f732

Browse files
authored
feat: add platform matching host for AngularCompilerPlugin (#539)
fixes #547
1 parent 22acdc4 commit 979f732

16 files changed

+159
-161
lines changed

Diff for: .gitignore

+4
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

Diff for: bundle-config-loader.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = function(source) {
22
this.cacheable();
3-
const { registerPages = true, loadCss = true } = this.query;
3+
const { angular = false, loadCss = true } = this.query;
44

5-
if (registerPages) {
5+
if (!angular) {
66
source = `
77
require("nativescript-dev-webpack/register-modules");
88
${source}
@@ -11,7 +11,7 @@ module.exports = function(source) {
1111

1212
if (loadCss) {
1313
source = `
14-
require("nativescript-dev-webpack/load-application-css");
14+
require("nativescript-dev-webpack/load-application-css")(${angular});
1515
${source}
1616
`;
1717
}

Diff for: demo/AngularApp/package.json

+19-19
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
}
1414
},
1515
"dependencies": {
16-
"@angular/common": "~6.0.0-rc.0",
17-
"@angular/compiler": "~6.0.0-rc.0",
18-
"@angular/core": "~6.0.0-rc.0",
19-
"@angular/forms": "~6.0.0-rc.0",
20-
"@angular/http": "~6.0.0-rc.0",
21-
"@angular/platform-browser": "~6.0.0-rc.0",
22-
"@angular/platform-browser-dynamic": "~6.0.0-rc.0",
23-
"@angular/router": "~6.0.0-rc.0",
24-
"nativescript-angular": "rc",
16+
"@angular/common": "~6.0.0",
17+
"@angular/compiler": "~6.0.0",
18+
"@angular/core": "~6.0.0",
19+
"@angular/forms": "~6.0.0",
20+
"@angular/http": "~6.0.0",
21+
"@angular/platform-browser": "~6.0.0",
22+
"@angular/platform-browser-dynamic": "~6.0.0",
23+
"@angular/router": "~6.0.0",
24+
"nativescript-angular": "~6.0.0",
2525
"nativescript-theme-core": "~1.0.2",
2626
"reflect-metadata": "~0.1.8",
2727
"rxjs": "~6.0.0-beta.1",
2828
"tns-core-modules": "next",
2929
"zone.js": "^0.8.4"
3030
},
3131
"devDependencies": {
32-
"@angular-devkit/core": "~0.5.5",
33-
"@angular/compiler-cli": "~6.0.0-rc.0",
34-
"@ngtools/webpack": "~6.0.0-rc.3",
32+
"@angular-devkit/core": "~0.7.0-beta.1",
33+
"@angular/compiler-cli": "~6.0.0",
34+
"@ngtools/webpack": "~6.1.0-beta.1",
3535
"@types/chai": "^4.0.2",
3636
"@types/mocha": "^2.2.41",
3737
"@types/node": "^7.0.5",
@@ -42,7 +42,7 @@
4242
"chai-as-promised": "~7.1.1",
4343
"clean-webpack-plugin": "~0.1.19",
4444
"copy-webpack-plugin": "~4.5.1",
45-
"css-loader": "~0.28.7",
45+
"css-loader": "~0.28.11",
4646
"extract-text-webpack-plugin": "~3.0.2",
4747
"lazy": "1.0.11",
4848
"mocha": "~3.5.0",
@@ -52,15 +52,15 @@
5252
"nativescript-dev-sass": "^1.3.5",
5353
"nativescript-dev-typescript": "next",
5454
"nativescript-dev-webpack": "file:../..",
55-
"nativescript-worker-loader": "~0.8.1",
55+
"nativescript-worker-loader": "~0.9.0",
5656
"raw-loader": "~0.5.1",
5757
"resolve-url-loader": "~2.3.0",
58-
"sass-loader": "~6.0.6",
58+
"sass-loader": "~7.0.1",
5959
"typescript": "~2.7.2",
60-
"uglifyjs-webpack-plugin": "~1.2.4",
61-
"webpack": "~4.5.0",
62-
"webpack-bundle-analyzer": "^2.9.1",
63-
"webpack-cli": "~2.0.14",
60+
"uglifyjs-webpack-plugin": "~1.2.5",
61+
"webpack": "~4.6.0",
62+
"webpack-bundle-analyzer": "~2.13.0",
63+
"webpack-cli": "~2.1.3",
6464
"webpack-sources": "~1.1.0"
6565
},
6666
"scripts": {

Diff for: dependencyManager.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ function getRequiredDeps(packageJson) {
7474
if (isAngular({packageJson})) {
7575
Object.assign(deps, {
7676
"@angular/compiler-cli": packageJson.dependencies["@angular/core"],
77-
"@ngtools/webpack": "~6.0.3",
78-
"@angular-devkit/core": "~0.6.3",
77+
"@ngtools/webpack": "~6.1.0-beta.1",
78+
"@angular-devkit/core": "~0.7.0-beta.1",
7979
"resolve-url-loader": "~2.3.0",
8080
});
8181
} else if (isTypeScript({packageJson})) {

Diff for: host/platform.ts

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 platformFileName = `${name}.${platform}${ext}`;
32+
const platformPath = this.toSystemPath(join(dir, platformFileName));
33+
34+
try {
35+
const stat = statSync(platformPath);
36+
if (stat && stat.isFile()) {
37+
return platformPath;
38+
}
39+
} catch(_e) {
40+
// continue checking the other platforms
41+
}
42+
}
43+
44+
return path;
45+
}
46+
47+
// Convert paths from \c\some\path to c:\some\path
48+
private toSystemPath(path: string) {
49+
if (!process.platform.startsWith("win32")) {
50+
return path;
51+
}
52+
53+
const drive = path.match(/^\\(\w)\\(.*)$/);
54+
return drive ?
55+
`${drive[1]}:\\${drive[2]}`:
56+
path;
57+
}
58+
59+
get capabilities(): HostCapabilities {
60+
return this._delegate.capabilities;
61+
}
62+
63+
write(path: Path, content: FileBuffer): Observable<void> {
64+
return this._delegate.write(this._resolve(path), content);
65+
}
66+
read(path: Path): Observable<FileBuffer> {
67+
return this._delegate.read(this._resolve(path));
68+
}
69+
delete(path: Path): Observable<void> {
70+
return this._delegate.delete(this._resolve(path));
71+
}
72+
rename(from: Path, to: Path): Observable<void> {
73+
return this._delegate.rename(this._resolve(from), this._resolve(to));
74+
}
75+
76+
list(path: Path): Observable<PathFragment[]> {
77+
return this._delegate.list(this._resolve(path));
78+
}
79+
80+
exists(path: Path): Observable<boolean> {
81+
return this._delegate.exists(this._resolve(path));
82+
}
83+
isDirectory(path: Path): Observable<boolean> {
84+
return this._delegate.isDirectory(this._resolve(path));
85+
}
86+
isFile(path: Path): Observable<boolean> {
87+
return this._delegate.isFile(this._resolve(path));
88+
}
89+
90+
stat(path: Path): Observable<Stats<StatsT>> | null {
91+
return this._delegate.stat(this._resolve(path));
92+
}
93+
94+
watch(path: Path, options?: HostWatchOptions): Observable<HostWatchEvent> | null {
95+
return this._delegate.watch(this._resolve(path), options);
96+
}
97+
}

Diff for: index.js

-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@ const { existsSync } = require("fs");
44
const { ANDROID_APP_PATH } = require("./androidProjectHelpers");
55
const {
66
getPackageJson,
7-
isAngular,
87
isAndroid,
98
isIos,
109
} = require("./projectHelpers");
1110

1211
Object.assign(exports, require('./plugins'));
1312

14-
exports.loadAdditionalPlugins = function (projectSettings) {
15-
if (isAngular(projectSettings)) {
16-
Object.assign(exports, require('./plugins/angular')(projectSettings.projectDir));
17-
}
18-
}
19-
2013
exports.getAotEntryModule = function (appDirectory) {
2114
verifyEntryModuleDirectory(appDirectory);
2215

Diff for: load-application-css.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
const application = require("application");
2-
require("ui/styling/style-scope");
3-
const appCssContext = require.context("~/", false, /^\.\/app\.(css|scss|less|sass)$/);
4-
global.registerWebpackModules(appCssContext);
5-
application.loadAppCss();
1+
module.exports = function(isAngular) {
2+
const application = require("tns-core-modules/application");
3+
require("tns-core-modules/ui/styling/style-scope");
4+
5+
if (isAngular) {
6+
global.registerModule("./app.css", () => require("~/app"));
7+
} else {
8+
const appCssContext = require.context("~/", false, /^\.\/app\.(css|scss|less|sass)$/);
9+
global.registerWebpackModules(appCssContext);
10+
}
11+
12+
application.loadAppCss();
13+
}
614

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@
7777
"tapable": "1.0.0"
7878
},
7979
"devDependencies": {
80-
"@ngtools/webpack": "~6.0.3",
80+
"@angular-devkit/core": "^0.7.0-beta.1",
8181
"@types/node": "^8.0.0",
8282
"conventional-changelog-cli": "^1.3.22",
83+
"rxjs": "^6.2.0",
8384
"source-map-support": "^0.5.0",
8485
"typescript": "~2.7.2"
8586
}

Diff for: plugins/NativeScriptAngularCompilerPlugin.ts

-104
This file was deleted.

Diff for: plugins/NativeScriptSnapshotPlugin/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ exports.NativeScriptSnapshotPlugin = (function() {
4545

4646
let snapshotEntryContent = "";
4747
if (includeApplicationCss) {
48-
snapshotEntryContent += `require("nativescript-dev-webpack/load-application-css");`;
48+
snapshotEntryContent += `require("nativescript-dev-webpack/load-application-css")(${options.angular});`;
4949
}
5050
snapshotEntryContent += [ ...requireModules, ...internalRequireModules]
5151
.map(mod => `require('${mod}')`).join(";");
@@ -155,3 +155,4 @@ exports.NativeScriptSnapshotPlugin = (function() {
155155

156156
return NativeScriptSnapshotPlugin;
157157
})();
158+

Diff for: plugins/NativeScriptSnapshotPlugin/options.json

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"androidNdkPath": {
55
"type": "string"
66
},
7+
"angular": {
8+
"type": "boolean",
9+
"default": false
10+
},
711
"chunk": {
812
"type": "string"
913
},

Diff for: plugins/angular.js

-3
This file was deleted.

0 commit comments

Comments
 (0)