Skip to content

Commit 6c5d4b4

Browse files
committed
build: bazel typescript targets should use tsickle for decorator processing
We want to enable tsickle for decorator processing. This was one of the blockers for using the bazel packages in releases as unprocessed decorators will end up referencing DOM specific globals (e.g. `HTMLElement`). These references would then break server-side rendering for non CLI projects. Read more about this angular/angular#30586
1 parent 77b0e0e commit 6c5d4b4

File tree

8 files changed

+68
-3
lines changed

8 files changed

+68
-3
lines changed

WORKSPACE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ node_repositories(
3535

3636
yarn_install(
3737
name = "npm",
38-
# Ensure that the script is available when running `postinstall` in the Bazel sandbox.
38+
# Ensure that all resources are available when the "postinstall" or "preinstall" scripts
39+
# are executed in the Bazel sandbox.
3940
data = [
4041
"//:angular-tsconfig.json",
42+
"//:tools/bazel/postinstall-patches.js",
4143
"//:tools/npm/check-npm.js",
4244
],
4345
package_json = "//:package.json",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"yarn": ">= 1.17.3"
1414
},
1515
"scripts": {
16-
"postinstall": "ngc -p angular-tsconfig.json",
16+
"postinstall": "node --preserve-symlinks --preserve-symlinks-main tools/bazel/postinstall-patches.js",
1717
"build": "gulp build-release-packages",
1818
"bazel:buildifier": "find . -type f \\( -name \"*.bzl\" -or -name WORKSPACE -or -name BUILD -or -name BUILD.bazel \\) ! -path \"*/node_modules/*\" | xargs buildifier -v --warnings=attr-cfg,attr-license,attr-non-empty,attr-output-default,attr-single-file,constant-glob,ctx-args,depset-iteration,depset-union,dict-concatenation,duplicated-name,filetype,git-repository,http-archive,integer-division,load,load-on-top,native-build,native-package,output-group,package-name,package-on-top,redefined-variable,repository-name,same-origin-load,string-iteration,unused-variable,unsorted-dict-items,out-of-order-load",
1919
"bazel:format-lint": "yarn -s bazel:buildifier --lint=warn --mode=check",
@@ -141,6 +141,7 @@
141141
"run-sequence": "^1.2.2",
142142
"scss-bundle": "^2.0.1-beta.7",
143143
"selenium-webdriver": "^3.6.0",
144+
"shelljs": "^0.8.3",
144145
"sorcery": "^0.10.0",
145146
"stylelint": "^10.1.0",
146147
"ts-api-guardian": "^0.4.6",

src/bazel-tsconfig-build.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
// some options here just in favor of the standard tsconfig's which extending this one.
3434
"suppressTsconfigOverrideWarnings": true,
3535

36+
// Enable tsickle so that decorators are processed by tsickle. This is necessary because
37+
// we don't want to ship JavaScript files that contain references to DOM elements. This
38+
// breaks server-side rendering for non-CLI projects: see: angular#30586.
39+
"tsickle": true,
40+
3641
// See https://github.com/angular/angular/issues/29107
3742
"devmodeTargetOverride": "es5"
3843
}

src/bazel-tsconfig-test.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"bazelOptions": {
1111
"suppressTsconfigOverrideWarnings": true,
1212

13+
// Disable tsickle. Tsickle is only needed for building library code that will be
14+
// shipped through npm packages. This should speed up compilation for tests.
15+
"tsickle": false,
16+
1317
// See https://github.com/angular/angular/issues/29107
1418
"devmodeTargetOverride": "es5"
1519
}

tools/bazel/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
4+
5+
# Custom "tsc_wrapped" binary that has "tsickle" available as runtime dependency.
6+
# This is needed as the default compiler for a "ts_library" does not have a dependency
7+
# on "tsickle" by default.
8+
nodejs_binary(
9+
name = "tsc_wrapped_with_tsickle",
10+
data = [
11+
"@npm//@bazel/typescript",
12+
"@npm//tsickle",
13+
],
14+
entry_point = "@npm//:node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js",
15+
install_source_map_support = False,
16+
)

tools/bazel/postinstall-patches.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Script that runs after node modules have been installed (including Bazel managed
3+
* node modules). This script can be used to apply postinstall patches. Similarly
4+
* to Bazel's "patches" attribute on repository fetch rules.
5+
*/
6+
7+
const shelljs = require('shelljs');
8+
const path = require('path');
9+
10+
shelljs.set('-e');
11+
shelljs.cd(path.join(__dirname, '../..'));
12+
13+
// Workaround for https://github.com/angular/angular/issues/18810.
14+
shelljs.exec('ngc -p angular-tsconfig.json');
15+
16+
// Workaround for https://github.com/angular/angular/issues/30586. It's not possible to
17+
// enable tsickle decorator processing without enabling import rewriting to closure.
18+
// This replacement allows us to enable decorator processing without rewriting imports.
19+
shelljs.sed('-i', /(this\.transformTypesToClosure) = bazelOpts\.tsickle;/, '$1 = false;',
20+
'node_modules/@bazel/typescript/internal/tsc_wrapped/compiler_host.js');
21+
shelljs.sed('-i', /bazelOpts\.tsickleExternsPath/, 'null',
22+
'node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js');

tools/defaults.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ def _getDefaultTsConfig(testonly):
2727
def ts_library(tsconfig = None, deps = [], testonly = False, **kwargs):
2828
# Add tslib because we use import helpers for all public packages.
2929
local_deps = ["@npm//tslib"] + deps
30+
all_args = dict(**kwargs)
3031

3132
if not tsconfig:
3233
tsconfig = _getDefaultTsConfig(testonly)
3334

35+
# If the default build tsconfig with "tsickle" enabled is used, wire up
36+
# a different "compiler" binary where "tsickle" is available as runfile.
37+
if not testonly:
38+
all_args["compiler"] = "//tools/bazel:tsc_wrapped_with_tsickle"
39+
3440
_ts_library(
3541
tsconfig = tsconfig,
3642
testonly = testonly,
3743
deps = local_deps,
38-
**kwargs
44+
**all_args
3945
)
4046

4147
def ng_module(deps = [], tsconfig = None, testonly = False, **kwargs):

yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10280,6 +10280,15 @@ shelljs@^0.7.0:
1028010280
interpret "^1.0.0"
1028110281
rechoir "^0.6.2"
1028210282

10283+
shelljs@^0.8.3:
10284+
version "0.8.3"
10285+
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.3.tgz#a7f3319520ebf09ee81275b2368adb286659b097"
10286+
integrity sha512-fc0BKlAWiLpwZljmOvAOTE/gXawtCoNrP5oaY7KIaQbbyHeQVg01pSEuEGvGh3HEdBU4baCD7wQBwADmM/7f7A==
10287+
dependencies:
10288+
glob "^7.0.0"
10289+
interpret "^1.0.0"
10290+
rechoir "^0.6.2"
10291+
1028310292
sigmund@~1.0.0:
1028410293
version "1.0.1"
1028510294
resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590"

0 commit comments

Comments
 (0)