This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathNativeScriptAngularCompilerPlugin.ts
101 lines (89 loc) · 4.38 KB
/
NativeScriptAngularCompilerPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as path from "path";
import { PlatformFSPlugin, PlatformFSPluginOptions, mapFileSystem } from "./PlatformFSPlugin";
import * as hook from "nativescript-hook";
import * as ngToolsWebpack from "@ngtools/webpack";
// During development the nativescript-dev-webpack plugin may have @ngtools/webpack installed locally as dev-dependency,
// we want to make sure we are using the one installed in the actual app
const projectDir = hook(path.join(__dirname, "..")).findProjectDir();
const ngToolsWebpackDir = path.join(projectDir, "node_modules", "@ngtools", "webpack");
const appNgToolsWebpack: typeof ngToolsWebpack = require(ngToolsWebpackDir);
export const AngularCompilerPlugin: typeof ngToolsWebpack.AngularCompilerPlugin = appNgToolsWebpack.AngularCompilerPlugin;
export interface NativeScriptAngularCompilerPluginOptions extends ngToolsWebpack.AngularCompilerPluginOptions {
platformOptions?: PlatformFSPluginOptions;
}
export interface CompiledFile {
outputText: string;
sourceMap: string;
errorDependencies: string[];
}
export class NativeScriptAngularCompilerPlugin extends AngularCompilerPlugin {
readonly options: NativeScriptAngularCompilerPluginOptions;
readonly platform: string;
get __compilerHost() {
// Accessing private API of the AngularCompilerPlugin
// We need this to augment at least the "resourceNameToFileName" so we can map
// component.css to component.android.css etc. for platform specific css and html resources.
return (<any>this)._compilerHost;
}
constructor(options: NativeScriptAngularCompilerPluginOptions) {
super(options);
this.platform = (this.options.platformOptions && this.options.platformOptions.platform) || undefined;
const platform = this.platform;
if (platform) {
// https://github.com/angular/angular/blob/7bfeac746e717d02e062fe4a65c008060b8b662c/packages/compiler-cli/src/transformers/api.ts
const resourceNameToFileName = this.__compilerHost.resourceNameToFileName || function(file, relativeTo) {
const resolved = path.resolve(path.dirname(relativeTo), file);
if (this.fileExists(resolved)) {
return resolved;
} else {
return null;
}
};
this.__compilerHost.resourceNameToFileName = function(file, relativeTo) {
const parsed = path.parse(file);
const platformFile = parsed.name + "." + platform + parsed.ext;
let resolved;
try {
resolved = resourceNameToFileName.call(this, platformFile, relativeTo);
} catch(e) {
}
resolved = resolved || resourceNameToFileName.call(this, file, relativeTo);
resolved = resolved && resolved.replace(/\\/g, "/");
return resolved;
};
}
}
getCompiledFile(this: NativeScriptAngularCompilerPlugin, file: string): CompiledFile {
try {
if (this.platform) {
const parsed = path.parse(file);
const platformFile = parsed.dir + path.sep + parsed.name + "." + this.platform + parsed.ext;
const result = super.getCompiledFile(platformFile);
return result;
}
} catch(e) {
}
return super.getCompiledFile(file);
}
apply(compiler) {
super.apply(compiler);
if (this.options.platformOptions && this.options.platformOptions.platform && this.options.platformOptions.platforms) {
compiler.plugin('environment', () => {
compiler.inputFileSystem = mapFileSystem({
fs: compiler.inputFileSystem,
context: compiler.context,
platform: this.options.platformOptions.platform,
platforms: this.options.platformOptions.platforms,
ignore: this.options.platformOptions.ignore
});
compiler.watchFileSystem = mapFileSystem({
fs: compiler.watchFileSystem,
context: compiler.context,
platform: this.options.platformOptions.platform,
platforms: this.options.platformOptions.platforms,
ignore: this.options.platformOptions.ignore
});
});
}
}
}