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