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

Commit 1c87e08

Browse files
committed
fix: import the workaroundResolve funciton based on the Angular version (it was moved from compiler_host to utils in Angular 8)
1 parent 5f7f70a commit 1c87e08

10 files changed

+66
-30
lines changed

Diff for: dependencyManager.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ function getRequiredDeps(packageJson) {
8484
}
8585

8686
const deps = {
87-
"@angular/compiler-cli": "~7.2.0",
87+
"@angular/compiler-cli": "8.0.0-rc.4",
8888
};
8989

9090
if (!dependsOn(packageJson, "@angular-devkit/build-angular")) {
91-
deps["@ngtools/webpack"] = "~7.2.0";
91+
deps["@ngtools/webpack"] = "8.0.0-rc.4";
9292
}
9393

9494
return deps;

Diff for: package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"generate-android-snapshot": "./bin/generate-android-snapshot"
7575
},
7676
"dependencies": {
77-
"@angular-devkit/core": "~7.2.0",
77+
"@angular-devkit/core": "8.0.0-rc.4",
7878
"clean-webpack-plugin": "~1.0.0",
7979
"copy-webpack-plugin": "~4.6.0",
8080
"css-loader": "~2.1.1",
@@ -89,7 +89,7 @@
8989
"resolve-url-loader": "~3.0.0",
9090
"sass-loader": "~7.1.0",
9191
"schema-utils": "0.4.5",
92-
"semver": "5.4.1",
92+
"semver": "^6.0.0",
9393
"shelljs": "0.6.0",
9494
"tapable": "1.0.0",
9595
"terser": "3.17.0",
@@ -101,16 +101,17 @@
101101
"webpack-sources": "~1.3.0"
102102
},
103103
"devDependencies": {
104-
"@ngtools/webpack": "~7.2.0",
105-
"@angular/compiler": "~7.2.0",
106-
"@angular/compiler-cli": "~7.2.0",
104+
"@angular/compiler": "8.0.0-rc.4",
105+
"@angular/compiler-cli": "8.0.0-rc.4",
106+
"@ngtools/webpack": "8.0.0-rc.4",
107107
"@types/jasmine": "^3.3.7",
108108
"@types/node": "^10.12.12",
109109
"@types/proxyquire": "1.3.28",
110+
"@types/semver": "^6.0.0",
110111
"conventional-changelog-cli": "^1.3.22",
111112
"jasmine": "^3.2.0",
112113
"jasmine-spec-reporter": "^4.2.1",
113114
"proxyquire": "2.1.0",
114-
"typescript": "~3.1.1"
115+
"typescript": "~3.4.0"
115116
}
116117
}

Diff for: projectHelpers.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ const isAngular = ({ projectDir, packageJson } = {}) => {
2929
.some(dependency => /^@angular\b/.test(dependency));
3030
};
3131

32+
const getAngularVersion = ({ projectDir, packageJson } = {}) => {
33+
packageJson = packageJson || getPackageJson(projectDir);
34+
35+
return packageJson.dependencies && packageJson.dependencies["@angular/core"];
36+
}
37+
3238
const isVue = ({ projectDir, packageJson } = {}) => {
3339
packageJson = packageJson || getPackageJson(projectDir);
3440

@@ -38,7 +44,14 @@ const isVue = ({ projectDir, packageJson } = {}) => {
3844

3945
const getPackageJson = projectDir => {
4046
const packageJsonPath = getPackageJsonPath(projectDir);
41-
return JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
47+
let result;
48+
try {
49+
result = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
50+
} catch (e) {
51+
result = {};
52+
}
53+
54+
return result;
4255
};
4356

4457
const writePackageJson = (content, projectDir) => {
@@ -106,6 +119,7 @@ module.exports = {
106119
isAndroid,
107120
isIos,
108121
isAngular,
122+
getAngularVersion,
109123
isVue,
110124
isTypeScript,
111125
writePackageJson,

Diff for: templates/webpack.angular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = env => {
9090

9191
const ngCompilerPlugin = new AngularCompilerPlugin({
9292
hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
93-
platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule))),
93+
platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule), projectRoot)),
9494
mainPath: join(appFullPath, entryModule),
9595
tsConfigPath: join(__dirname, tsConfigName),
9696
skipCodeGeneration: !aot,

Diff for: transformers/ns-replace-bootstrap.spec.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ describe('@ngtools/webpack transformers', () => {
2121
`;
2222

2323
const { program, compilerHost } = createTypescriptContext(input);
24+
const projectDir = "/project/src/";
25+
const appModule = `${projectDir}app/app.module`;
2426
const ngCompiler = <AngularCompilerPlugin>{
2527
typeChecker: program.getTypeChecker(),
2628
entryModule: {
27-
path: '/project/src/app/app.module',
29+
path: appModule,
2830
className: 'AppModule',
2931
},
3032
};
31-
const transformer = nsReplaceBootstrap(() => ngCompiler);
33+
const transformer = nsReplaceBootstrap(() => ngCompiler, appModule, projectDir);
3234
const result = transformTypescript(undefined, [transformer], program, compilerHost);
3335

3436
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
@@ -50,17 +52,19 @@ describe('@ngtools/webpack transformers', () => {
5052
`;
5153

5254
const { program, compilerHost } = createTypescriptContext(input);
55+
const projectDir = "/project/src/";
56+
const appModule = `${projectDir}app/app.module`;
5357
const ngCompiler: any = {
5458
_compilerOptions: {
5559
enableIvy: true
5660
},
5761
typeChecker: program.getTypeChecker(),
5862
entryModule: {
59-
path: '/project/src/app/app.module',
63+
path: appModule,
6064
className: 'AppModule',
6165
},
6266
};
63-
const transformer = nsReplaceBootstrap(() => ngCompiler);
67+
const transformer = nsReplaceBootstrap(() => ngCompiler, appModule, projectDir);
6468
const result = transformTypescript(undefined, [transformer], program, compilerHost);
6569

6670
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
@@ -82,14 +86,16 @@ describe('@ngtools/webpack transformers', () => {
8286
`;
8387

8488
const { program, compilerHost } = createTypescriptContext(input);
89+
const projectDir = "/project/src/";
90+
const appModule = `${projectDir}app/app.module`;
8591
const ngCompiler = <AngularCompilerPlugin>{
8692
typeChecker: program.getTypeChecker(),
8793
entryModule: {
88-
path: '/project/src/app/app.module',
94+
path: appModule,
8995
className: 'AppModule',
9096
},
9197
};
92-
const transformer = nsReplaceBootstrap(() => ngCompiler);
98+
const transformer = nsReplaceBootstrap(() => ngCompiler, appModule, projectDir);
9399
const result = transformTypescript(undefined, [transformer], program, compilerHost);
94100

95101
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
@@ -113,14 +119,16 @@ describe('@ngtools/webpack transformers', () => {
113119
`;
114120

115121
const { program, compilerHost } = createTypescriptContext(input);
122+
const projectDir = "/project/src/";
123+
const appModule = `${projectDir}app/app.module`;
116124
const ngCompiler = <AngularCompilerPlugin>{
117125
typeChecker: program.getTypeChecker(),
118126
entryModule: {
119-
path: '/project/src/app/app.module',
127+
path: appModule,
120128
className: 'AppModule',
121129
},
122130
};
123-
const transformer = nsReplaceBootstrap(() => ngCompiler);
131+
const transformer = nsReplaceBootstrap(() => ngCompiler, appModule, projectDir);
124132
const result = transformTypescript(undefined, [transformer], program, compilerHost);
125133

126134
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);

Diff for: transformers/ns-replace-bootstrap.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import { AngularCompilerPlugin } from '@ngtools/webpack';
1616
import { getResolvedEntryModule } from "../utils/transformers-utils";
1717

18-
export function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin): ts.TransformerFactory<ts.SourceFile> {
18+
export function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin, entryPath: string, projectDir: string): ts.TransformerFactory<ts.SourceFile> {
1919
const shouldTransform = (fileName) => !fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
2020
const getTypeChecker = () => getNgCompiler().typeChecker;
2121

@@ -24,7 +24,7 @@ export function nsReplaceBootstrap(getNgCompiler: () => AngularCompilerPlugin):
2424
const ngCompiler = getNgCompiler();
2525
// TODO: use something public when available
2626
const enableIvy = (<any>ngCompiler)._compilerOptions && (<any>ngCompiler)._compilerOptions.enableIvy;
27-
const entryModule = getResolvedEntryModule(ngCompiler);
27+
const entryModule = getResolvedEntryModule(ngCompiler, projectDir);
2828

2929
if (!shouldTransform(sourceFile.fileName) || !entryModule) {
3030
return ops;

Diff for: transformers/ns-replace-lazy-loader.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,16 @@ describe("@ngtools/webpack transformers", () => {
211211
const input = tags.stripIndent`${testCase.rawAppModule}`;
212212
const output = tags.stripIndent`${testCase.transformedAppModule}`;
213213
const { program, compilerHost } = createTypescriptContext(input);
214+
const projectDir = "/project/src/";
215+
const testFile = `${projectDir}test-file`;
214216
const ngCompiler = <AngularCompilerPlugin>{
215217
typeChecker: program.getTypeChecker(),
216218
entryModule: {
217-
path: "/project/src/test-file",
219+
path: testFile,
218220
className: "AppModule",
219221
},
220222
};
221-
const transformer = nsReplaceLazyLoader(() => ngCompiler);
223+
const transformer = nsReplaceLazyLoader(() => ngCompiler, testFile, projectDir);
222224
const result = transformTypescript(undefined, [transformer], program, compilerHost);
223225

224226
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);

Diff for: transformers/ns-replace-lazy-loader.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import { AngularCompilerPlugin } from "@ngtools/webpack";
1717
import { findIdentifierNode, getObjectPropertyMatches, getDecoratorMetadata } from "../utils/ast-utils";
1818
import { getResolvedEntryModule } from "../utils/transformers-utils";
1919

20-
export function nsReplaceLazyLoader(getNgCompiler: () => AngularCompilerPlugin): ts.TransformerFactory<ts.SourceFile> {
20+
export function nsReplaceLazyLoader(getNgCompiler: () => AngularCompilerPlugin, entryPath: string, projectDir: string): ts.TransformerFactory<ts.SourceFile> {
2121
const getTypeChecker = () => getNgCompiler().typeChecker;
2222

23-
const standardTransform: StandardTransform = function(sourceFile: ts.SourceFile) {
23+
const standardTransform: StandardTransform = function (sourceFile: ts.SourceFile) {
2424
let ops: TransformOperation[] = [];
25-
const entryModule = getResolvedEntryModule(getNgCompiler());
25+
const entryModule = getResolvedEntryModule(getNgCompiler(), projectDir);
2626
const sourceFilePath = join(dirname(sourceFile.fileName), basename(sourceFile.fileName, extname(sourceFile.fileName)));
2727
if (!entryModule || normalize(sourceFilePath) !== normalize(entryModule.path)) {
2828
return ops;

Diff for: transformers/ns-support-hmr-ng.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ describe("@ngtools/webpack transformers", () => {
351351
});
352352

353353
it(`${testCase.name} (in combination with AOT transformer)`, async () => {
354-
const testFile = "/project/src/test-file.ts";
354+
const projectDir = "/project/src/";
355+
const testFile = `${projectDir}test-file.ts`;
355356
const input = tags.stripIndent`${testCase.rawFile}`;
356357
const output = tags.stripIndent`${testCase.transformedFileWithAot}`;
357358
const { program, compilerHost } = createTypescriptContext(input);
@@ -363,7 +364,7 @@ describe("@ngtools/webpack transformers", () => {
363364
},
364365
};
365366

366-
const aotTransformer = nsReplaceBootstrap(() => ngCompiler);
367+
const aotTransformer = nsReplaceBootstrap(() => ngCompiler, testFile, projectDir);
367368
const hmrTransformer = nsSupportHmrNg(() => ngCompiler, testFile);
368369
const result = transformTypescript(undefined, [aotTransformer, hmrTransformer], program, compilerHost);
369370

Diff for: utils/transformers-utils.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import { workaroundResolve } from "@ngtools/webpack/src/compiler_host";
21
import { AngularCompilerPlugin } from "@ngtools/webpack";
2+
import * as semver from "semver";
3+
import { getAngularVersion } from "../projectHelpers";
4+
5+
export function getResolvedEntryModule(ngCompiler: AngularCompilerPlugin, projectDir: string) {
6+
const ngCoreVersion = semver.coerce(getAngularVersion({ projectDir }));
7+
let workaroundResolveModule;
8+
// https://github.com/angular/angular-cli/commit/d2e22e97818c6582ce4a9942c59fcac4a8aaf60e#diff-0f65e27eb122d9efa58bf08adada7f82L364
9+
if (!ngCoreVersion || semver.gte(ngCoreVersion, "8.0.0")) {
10+
workaroundResolveModule = require("@ngtools/webpack/src/utils");
11+
} else {
12+
workaroundResolveModule = require("@ngtools/webpack/src/compiler_host");
13+
}
314

4-
export function getResolvedEntryModule(ngCompiler: AngularCompilerPlugin) {
515
return ngCompiler.entryModule
6-
? { path: workaroundResolve(ngCompiler.entryModule.path), className: ngCompiler.entryModule.className }
16+
? { path: workaroundResolveModule.workaroundResolve(ngCompiler.entryModule.path), className: ngCompiler.entryModule.className }
717
: ngCompiler.entryModule;
818
}

0 commit comments

Comments
 (0)