Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 38d3868

Browse files
committed
refactor: update prerender utils to work with TS 3.9
TypeScript 3.9 changed the way exports/imports work, by making them readonly in conformance with ES2015 spec. This, however breaks `spyOn`
1 parent 52e7505 commit 38d3868

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

modules/builders/src/prerender/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async function _parallelRenderRoutes(
7070
outputPath: string,
7171
indexFile: string,
7272
serverBundlePath: string,
73-
): Promise<void> {
73+
): Promise<void> {
7474
const workerFile = path.join(__dirname, 'render.js');
7575
const childProcesses = shardedRoutes.map(routes =>
7676
new Promise((resolve, reject) => {
@@ -148,7 +148,13 @@ export async function execute(
148148
options: PrerenderBuilderOptions,
149149
context: BuilderContext
150150
): Promise<PrerenderBuilderOutput> {
151-
const routes = await getRoutes(options, context);
151+
const browserTarget = targetFromTargetString(options.browserTarget);
152+
const browserOptions =
153+
await context.getTargetOptions(browserTarget) as unknown as BrowserBuilderOptions;
154+
const tsConfigPath =
155+
typeof browserOptions.tsConfig === 'string' ? browserOptions.tsConfig : undefined;
156+
157+
const routes = await getRoutes(options, tsConfigPath, context);
152158
if (!routes.length) {
153159
throw new Error(`Could not find any routes to prerender.`);
154160
}
@@ -159,10 +165,6 @@ export async function execute(
159165
return { success, error } as BuilderOutput;
160166
}
161167

162-
const browserTarget = targetFromTargetString(options.browserTarget);
163-
const browserOptions =
164-
await context.getTargetOptions(browserTarget) as unknown as BrowserBuilderOptions;
165-
166168
return _renderUniversal(
167169
routes,
168170
context,

modules/builders/src/prerender/utils.spec.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import * as Architect from '@angular-devkit/architect';
9+
import { BuilderContext } from '@angular-devkit/architect';
1010
import { BrowserBuilderOptions } from '@angular-devkit/build-angular';
1111
import { logging } from '@angular-devkit/core';
1212
import * as fs from 'fs';
@@ -28,18 +28,17 @@ describe('Prerender Builder Utils', () => {
2828
{ path: '/user/:id' },
2929
];
3030

31+
const TSCONFIG_PATH = 'tsconfig.app.json';
3132
const CONTEXT = {
3233
workspaceRoot: '/path/to/angular/json',
33-
getTargetOptions: () => ({ tsConfig: 'tsconfig.app.json' }),
3434
logger: new logging.NullLogger(),
35-
} as unknown as Architect.BuilderContext;
35+
} as unknown as BuilderContext;
3636

3737
let parseAngularRoutesSpy: jasmine.Spy;
3838
let loggerErrorSpy: jasmine.Spy;
3939

4040
beforeEach(() => {
4141
spyOn(fs, 'readFileSync').and.returnValue(ROUTES_FILE_CONTENT);
42-
spyOn(Architect, 'targetFromTargetString').and.returnValue({} as Architect.Target);
4342
parseAngularRoutesSpy = spyOn(guessParser, 'parseAngularRoutes')
4443
.and.returnValue(GUESSED_ROUTES);
4544
loggerErrorSpy = spyOn(CONTEXT.logger, 'error');
@@ -51,7 +50,7 @@ describe('Prerender Builder Utils', () => {
5150
routesFile: ROUTES_FILE,
5251
guessRoutes: true,
5352
} as PrerenderBuilderOptions;
54-
const routes = await getRoutes(options, CONTEXT);
53+
const routes = await getRoutes(options, TSCONFIG_PATH, CONTEXT);
5554
expect(routes).toEqual(
5655
jasmine.arrayContaining([
5756
'/route1',
@@ -65,7 +64,7 @@ describe('Prerender Builder Utils', () => {
6564

6665
it('Should return only the given routes', async () => {
6766
const options = { routes: ROUTES } as PrerenderBuilderOptions;
68-
const routes = await getRoutes(options, CONTEXT);
67+
const routes = await getRoutes(options, TSCONFIG_PATH, CONTEXT);
6968
expect(routes).toEqual(jasmine.arrayContaining([
7069
'/route3',
7170
'/route4',
@@ -74,7 +73,7 @@ describe('Prerender Builder Utils', () => {
7473

7574
it('Should return the routes from the routesFile', async () => {
7675
const options = { routesFile: ROUTES_FILE } as PrerenderBuilderOptions;
77-
const routes = await getRoutes(options, CONTEXT);
76+
const routes = await getRoutes(options, TSCONFIG_PATH, CONTEXT);
7877
expect(routes).toEqual(jasmine.arrayContaining([
7978
'/route1',
8079
'/route2',
@@ -85,7 +84,7 @@ describe('Prerender Builder Utils', () => {
8584
it('Should catch errors thrown by parseAngularRoutes', async () => {
8685
const options = { routes: ROUTES, guessRoutes: true } as PrerenderBuilderOptions;
8786
parseAngularRoutesSpy.and.throwError('Test Error');
88-
const routes = await getRoutes(options, CONTEXT);
87+
const routes = await getRoutes(options, TSCONFIG_PATH, CONTEXT);
8988
expect(routes).toEqual(jasmine.arrayContaining([
9089
'/route3',
9190
'/route4',

modules/builders/src/prerender/utils.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { BuilderContext, targetFromTargetString } from '@angular-devkit/architect';
9+
import { BuilderContext } from '@angular-devkit/architect';
1010
import { BrowserBuilderOptions } from '@angular-devkit/build-angular';
1111
import * as fs from 'fs';
1212
import { parseAngularRoutes } from 'guess-parser';
1313
import * as os from 'os';
1414
import * as path from 'path';
15-
1615
import { PrerenderBuilderOptions } from './models';
1716

1817
/**
@@ -21,32 +20,29 @@ import { PrerenderBuilderOptions } from './models';
2120
*/
2221
export async function getRoutes(
2322
options: PrerenderBuilderOptions,
23+
tsConfigPath: string | undefined,
2424
context: BuilderContext,
2525
): Promise<string[]> {
2626
let routes = options.routes || [];
27-
27+
const { logger, workspaceRoot } = context;
2828
if (options.routesFile) {
29-
const routesFilePath = path.resolve(context.workspaceRoot, options.routesFile);
29+
const routesFilePath = path.join(workspaceRoot, options.routesFile);
3030
routes = routes.concat(
3131
fs.readFileSync(routesFilePath, 'utf8')
3232
.split(/\r?\n/)
3333
.filter(v => !!v)
3434
);
3535
}
3636

37-
if (options.guessRoutes) {
38-
const browserTarget = targetFromTargetString(options.browserTarget);
39-
const { tsConfig } = await context.getTargetOptions(browserTarget);
40-
if (typeof tsConfig === 'string') {
41-
try {
42-
routes = routes.concat(
43-
parseAngularRoutes(path.join(context.workspaceRoot, tsConfig))
44-
.map(routeObj => routeObj.path)
45-
.filter(route => !route.includes('*') && !route.includes(':'))
46-
);
47-
} catch (e) {
48-
context.logger.error('Unable to extract routes from application.', e);
49-
}
37+
if (options.guessRoutes && tsConfigPath) {
38+
try {
39+
routes = routes.concat(
40+
parseAngularRoutes(path.join(workspaceRoot, tsConfigPath))
41+
.map(routeObj => routeObj.path)
42+
.filter(route => !route.includes('*') && !route.includes(':'))
43+
);
44+
} catch (e) {
45+
logger.error('Unable to extract routes from application.', e);
5046
}
5147
}
5248

0 commit comments

Comments
 (0)