Skip to content

Commit 970847e

Browse files
devversionjelbourn
authored andcommitted
build: fix universal prerendering task (#4931)
Fixes the Universal Prerendering task by using the same workaround as for the AOT job (building sources in the output directory already) Building the sources in the output directory (similar as for the AOT task) is theonly known workaround for angular/angular#12249
1 parent 615fa2a commit 970847e

File tree

5 files changed

+41
-128
lines changed

5 files changed

+41
-128
lines changed

package-lock.json

Lines changed: 14 additions & 110 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"sorcery": "^0.10.0",
112112
"stylelint": "^7.10.1",
113113
"ts-node": "^3.0.4",
114+
"tsconfig-paths": "^2.2.0",
114115
"tslint": "^5.2.0",
115116
"typescript": "~2.2.1",
116117
"uglify-js": "^2.8.14",

src/universal-app/tsconfig-build.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
// TypeScript config file that is used to compile the universal-app.
1+
// TypeScript config file that is used to compile the Universal App. All sources are compiled
2+
// inside of the output folder and therefore all paths can be relative to the output folder.
23
{
34
"compilerOptions": {
45
"declaration": true,
56
"stripInternal": false,
67
"experimentalDecorators": true,
78
"module": "commonjs",
89
"moduleResolution": "node",
9-
"outDir": "../../dist/packages/universal-app",
10+
"outDir": ".",
1011
"rootDir": ".",
1112
"sourceMap": true,
1213
"target": "es2015",
@@ -23,7 +24,6 @@
2324
"main.ts"
2425
],
2526
"angularCompilerOptions": {
26-
"annotateForClosureCompiler": true,
27-
"genDir": "../../dist/packages/universal-app"
27+
"annotateForClosureCompiler": true
2828
}
2929
}

tools/gulp/tasks/universal.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,39 @@ import {copySync} from 'fs-extra';
77
const appDir = join(SOURCE_ROOT, 'universal-app');
88
const outDir = join(DIST_ROOT, 'packages', 'universal-app');
99

10-
/** Path to the universal-app tsconfig files. */
11-
const tsconfigAppPath = join(appDir, 'tsconfig-build.json');
10+
// Paths to the different tsconfig files of the Universal app.
11+
// Building the sources in the output directory is part of the workaround for
12+
// https://github.com/angular/angular/issues/12249
13+
const tsconfigAppPath = join(outDir, 'tsconfig-build.json');
1214
const tsconfigPrerenderPath = join(outDir, 'tsconfig-prerender.json');
1315

14-
/** Glob that matches all assets that need to copied to the dist. */
15-
const assetsGlob = join(appDir, '**/*.+(html|css|json)');
16-
17-
/** Path to the file that prerenders the universal app using platform-server. */
18-
const prerenderFile = join(appDir, 'prerender.ts');
19-
2016
/** Path to the compiled prerender file. Running this file just dumps the HTML output for now. */
2117
const prerenderOutFile = join(outDir, 'prerender.js');
2218

23-
task('universal:test-prerender', ['universal:build'], execTask('node', [prerenderOutFile]));
19+
/** Task that builds the universal-app and runs the prerender script. */
20+
task('universal:test-prerender', ['universal:build'], execTask(
21+
// Runs node with the tsconfig-paths module to alias the @angular/material dependency.
22+
'node', ['-r', 'tsconfig-paths/register', prerenderOutFile], {
23+
env: {TS_NODE_PROJECT: tsconfigPrerenderPath}
24+
}
25+
));
2426

2527
task('universal:build', sequenceTask(
2628
'clean',
2729
['material:build-release', 'cdk:build-release'],
28-
'universal:copy-release',
29-
['universal:build-app-ts', 'universal:copy-app-assets', 'universal:copy-prerender-source'],
30+
['universal:copy-release', 'universal:copy-files'],
31+
'universal:build-app-ts',
3032
'universal:build-prerender-ts'
3133
));
3234

35+
/** Task that builds the universal app in the output directory. */
3336
task('universal:build-app-ts', ngcBuildTask(tsconfigAppPath));
34-
task('universal:copy-app-assets', copyTask(assetsGlob, outDir));
3537

38+
/** Task that copies all files to the output directory. */
39+
task('universal:copy-files', copyTask(appDir, outDir));
40+
41+
/** Task that builds the prerender script in the output directory. */
3642
task('universal:build-prerender-ts', tsBuildTask(tsconfigPrerenderPath));
37-
task('universal:copy-prerender-source', copyTask(prerenderFile, outDir));
3843

3944
// As a workaround for https://github.com/angular/angular/issues/12249, we need to
4045
// copy the Material and CDK ESM output inside of the universal-app output.

tools/gulp/util/task_helpers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ export interface ExecTaskOptions {
6161
silentStdout?: boolean;
6262
// If an error happens, this will replace the standard error.
6363
errMessage?: string;
64+
// Environment variables being passed to the child process.
65+
env?: any;
6466
}
6567

6668
/** Create a task that executes a binary as if from the command line. */
6769
export function execTask(binPath: string, args: string[], options: ExecTaskOptions = {}) {
6870
return (done: (err?: string) => void) => {
69-
const childProcess = child_process.spawn(binPath, args);
71+
const env = Object.assign({}, process.env, options.env);
72+
const childProcess = child_process.spawn(binPath, args, {env});
7073

7174
if (!options.silentStdout && !options.silent) {
7275
childProcess.stdout.on('data', (data: string) => process.stdout.write(data));

0 commit comments

Comments
 (0)