Skip to content

Commit ce71a45

Browse files
devversionjelbourn
authored andcommitted
build: convert prerender gulp task to bazel (#16943)
1 parent ff6ee4e commit ce71a45

File tree

12 files changed

+75
-234
lines changed

12 files changed

+75
-234
lines changed

.circleci/config.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -255,20 +255,6 @@ jobs:
255255

256256
- run: ./scripts/circleci/run-saucelabs-tests.sh
257257

258-
# -------------------------------------------------------------------------
259-
# Job that pre-render's the universal app with `@angular/platform-server`.
260-
# This verifies that Angular Material can be rendered within Node.
261-
# -------------------------------------------------------------------------
262-
prerender_build:
263-
<<: *job_defaults
264-
steps:
265-
- *checkout_code
266-
- *restore_cache
267-
- *yarn_download
268-
- *yarn_install
269-
270-
- run: yarn gulp ci:prerender
271-
272258
# ----------------------------------
273259
# Lint job. Runs the gulp lint task.
274260
# ----------------------------------
@@ -523,8 +509,6 @@ workflows:
523509
jobs:
524510
- e2e_tests:
525511
filters: *ignore_presubmit_branch_filter
526-
- prerender_build:
527-
filters: *ignore_presubmit_branch_filter
528512

529513
release_output:
530514
jobs:

src/universal-app/BUILD.bazel

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_test")
4+
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary")
5+
load("//:packages.bzl", "CDK_EXPERIMENTAL_TARGETS", "CDK_TARGETS", "MATERIAL_EXPERIMENTAL_SCSS_LIBS", "MATERIAL_EXPERIMENTAL_TARGETS", "MATERIAL_TARGETS")
6+
load("//tools:defaults.bzl", "ng_module", "ts_library")
7+
8+
ng_module(
9+
name = "kitchen-sink",
10+
srcs = [
11+
"kitchen-sink-mdc/kitchen-sink-mdc.ts",
12+
"kitchen-sink-root.ts",
13+
"kitchen-sink/kitchen-sink.ts",
14+
],
15+
assets = [
16+
"kitchen-sink/kitchen-sink.html",
17+
"kitchen-sink-mdc/kitchen-sink-mdc.html",
18+
],
19+
deps = [
20+
"@npm//@angular/platform-server",
21+
] + CDK_TARGETS + CDK_EXPERIMENTAL_TARGETS + MATERIAL_TARGETS + MATERIAL_EXPERIMENTAL_TARGETS,
22+
)
23+
24+
ts_library(
25+
name = "server",
26+
srcs = [
27+
"prerender.ts",
28+
],
29+
deps = [
30+
":kitchen-sink",
31+
"@npm//@angular/platform-server",
32+
"@npm//@types/node",
33+
"@npm//reflect-metadata",
34+
"@npm//zone.js",
35+
],
36+
)
37+
38+
sass_binary(
39+
name = "theme_scss",
40+
src = "theme.scss",
41+
include_paths = [
42+
"external/npm/node_modules",
43+
],
44+
deps = [
45+
"//src/material/core:all_themes",
46+
] + MATERIAL_EXPERIMENTAL_SCSS_LIBS,
47+
)
48+
49+
nodejs_test(
50+
name = "server_test",
51+
data = [
52+
"index.html",
53+
":server",
54+
":theme_scss",
55+
],
56+
entry_point = ":prerender.ts",
57+
)

src/universal-app/DEBUG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
### Debugging the pre-rendered HTML file
2+
3+
Since the pre-rendered HTML file is built through a Bazel test target, the
4+
generated HTML file will not be stored in a folder of the repository. Instead,
5+
the file will be stored in the `bazel-out` folder.
6+
7+
You can retrieve the path to the file by either running:
8+
9+
* `bazel test //src/universal-app:server_test --test_output=all`
10+
* `echo $(bazel info bazel-bin)/src/universal-app/index-prerendered.html`

src/universal-app/kitchen-sink-root.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export class KitchenSinkRoot {
1717

1818
@NgModule({
1919
imports: [
20-
BrowserModule.withServerTransition({appId: 'kitchen-sink'}), KitchenSinkMdcModule,
20+
BrowserModule.withServerTransition({appId: 'kitchen-sink'}),
21+
KitchenSinkMdcModule,
2122
KitchenSinkModule
2223
],
2324
declarations: [KitchenSinkRoot],

src/universal-app/prerender.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import 'reflect-metadata';
22
import 'zone.js';
33

44
import {renderModuleFactory} from '@angular/platform-server';
5-
import {readFileSync, writeFileSync} from 'fs-extra';
6-
import {log} from 'gulp-util';
5+
import {readFileSync, writeFileSync} from 'fs';
76
import {join} from 'path';
87

98
import {KitchenSinkRootServerModuleNgFactory} from './kitchen-sink-root.ngfactory';
109

1110
// Do not enable production mode, because otherwise the `MatCommonModule` won't execute
1211
// the browser related checks that could cause NodeJS issues.
1312

13+
// Resolve the path to the "index.html" through Bazel runfile resolution.
14+
const indexHtmlPath = require.resolve('./index.html');
15+
1416
const result = renderModuleFactory(
1517
KitchenSinkRootServerModuleNgFactory,
16-
{document: readFileSync(join(__dirname, 'index.html'), 'utf-8')});
18+
{document: readFileSync(indexHtmlPath, 'utf-8')});
1719

1820
result
1921
.then(content => {
@@ -22,7 +24,7 @@ result
2224
console.log('Inspect pre-rendered page here:');
2325
console.log(`file://${filename}`);
2426
writeFileSync(filename, content, 'utf-8');
25-
log('Prerender done.');
27+
console.log('Prerender done.');
2628
})
2729
// If rendering the module factory fails, exit the process with an error code because otherwise
2830
// the CI task will not recognize the failure and will show as "success". The error message

src/universal-app/tsconfig-build.json

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/universal-app/tsconfig-prerender.json

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/universal-app/tsconfig.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

tools/gulp/gulpfile.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import './tasks/example-module';
2929
import './tasks/lint';
3030
import './tasks/material-release';
3131
import './tasks/unit-test';
32-
import './tasks/universal';
3332

3433
/** Task that builds all available release packages. */
3534
task('build-release-packages', sequenceTask(

tools/gulp/tasks/ci.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,5 @@ task('ci:test', ['test:single-run'], () => process.exit(0));
1212
*/
1313
task('ci:aot', ['build-aot:no-release-build']);
1414

15-
/** Task that verifies if all Material components are working with platform-server. */
16-
task('ci:prerender', ['prerender']);
17-
1815
/** Task that builds all release packages. */
1916
task('ci:build-release-packages', ['build-release-packages']);

tools/gulp/tasks/universal.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.

tools/gulp/util/task-helpers.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
11
import * as child_process from 'child_process';
2-
import * as fs from 'fs';
32
import * as gulp from 'gulp';
4-
import * as path from 'path';
53

64
// This import lacks type definitions.
75
const gulpClean = require('gulp-clean');
86

97
// There are no type definitions available for these imports.
108
const resolveBin = require('resolve-bin');
119

12-
13-
/** If the string passed in is a glob, returns it, otherwise append '**\/*' to it. */
14-
function _globify(maybeGlob: string, suffix = '**/*') {
15-
if (maybeGlob.indexOf('*') > -1) {
16-
return maybeGlob;
17-
}
18-
try {
19-
if (fs.statSync(maybeGlob).isFile()) {
20-
return maybeGlob;
21-
}
22-
} catch {}
23-
return path.join(maybeGlob, suffix);
24-
}
25-
26-
27-
/** Creates a task that runs the TypeScript compiler */
28-
export function tsBuildTask(tsConfigPath: string) {
29-
return execNodeTask('typescript', 'tsc', ['-p', tsConfigPath]);
30-
}
31-
32-
/** Creates a task that runs the Angular Compiler CLI. */
33-
export function ngcBuildTask(tsConfigPath: string) {
34-
return execNodeTask('@angular/compiler-cli', 'ngc', ['-p', tsConfigPath]);
35-
}
36-
3710
/** Options that can be passed to execTask or execNodeTask. */
3811
export interface ExecTaskOptions {
3912
// Whether STDOUT and STDERR messages should be printed.
@@ -101,17 +74,6 @@ export function execNodeTask(packageName: string, executable: string | string[],
10174
};
10275
}
10376

104-
105-
/** Copy files from a glob to a destination. */
106-
export function copyTask(srcGlobOrDir: string | string[], outRoot: string) {
107-
if (typeof srcGlobOrDir === 'string') {
108-
return () => gulp.src(_globify(srcGlobOrDir)).pipe(gulp.dest(outRoot));
109-
} else {
110-
return () => gulp.src(srcGlobOrDir.map(name => _globify(name))).pipe(gulp.dest(outRoot));
111-
}
112-
}
113-
114-
11577
/** Delete files. */
11678
export function cleanTask(glob: string) {
11779
return () => gulp.src(glob, { read: false }).pipe(gulpClean(null));

0 commit comments

Comments
 (0)