Skip to content

Commit be06d23

Browse files
committed
refactor: move injector feature to own rule
BREAKING CHANGE: ts_devserver and web_package no longer have an index_html attribute. They expect an index.html file to be among the assets, and to already have the script and link tags needed for the app to work. The feature where those tags were injected into the html dynamically has been moved to its own rule, inject_html.
1 parent 5cd4848 commit be06d23

49 files changed

Lines changed: 389 additions & 248 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ npm_package(
9191
"//third_party/github.com/bazelbuild/bazel-skylib:package_contents",
9292
"//third_party/github.com/buffer-from:package_contents",
9393
"//third_party/github.com/gjtorikian/isBinaryFile:package_contents",
94-
"//third_party/github.com/inikulin/parse5:package_contents",
9594
"//third_party/github.com/juanjoDiaz/removeNPMAbsolutePaths:package_contents",
9695
"//third_party/github.com/source-map:package_contents",
9796
"//third_party/github.com/source-map-support:package_contents",

e2e/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ e2e_integration_test(
8383
name = "e2e_ts_devserver",
8484
npm_packages = {
8585
"//packages/hide-bazel-files:npm_package": "@bazel/hide-bazel-files",
86+
"//packages/inject-html:npm_package": "@bazel/inject-html",
8687
"//packages/protractor:npm_package": "@bazel/protractor",
8788
"//packages/typescript:npm_package": "@bazel/typescript",
8889
},

e2e/ts_devserver/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {
33
"@bazel/hide-bazel-files": "^0.40.0",
4+
"@bazel/inject-html": "^0.40.0",
45
"@bazel/protractor": "^0.40.0",
56
"@bazel/typescript": "^0.40.0",
67
"@types/jasmine": "2.8.2",

e2e/ts_devserver/subpackage/BUILD.bazel

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@npm_bazel_inject_html//:index.bzl", "inject_html")
1516
load("@npm_bazel_protractor//:index.bzl", "protractor_web_test_suite")
1617
load("@npm_bazel_typescript//:index.bzl", "ts_devserver", "ts_library")
1718

19+
inject_html(
20+
name = "inject",
21+
src = "index.html",
22+
assets = ["//:red-body-style.css"],
23+
)
24+
1825
ts_devserver(
1926
name = "devserver",
20-
index_html = "index.html",
21-
static_files = ["//:red-body-style.css"],
27+
static_files = [
28+
"inject",
29+
"//:red-body-style.css",
30+
],
2231
)
2332

2433
ts_library(

examples/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ example_integration_test(
1818
name = "examples_angular",
1919
timeout = "long",
2020
npm_packages = {
21+
"//packages/inject-html:npm_package": "@bazel/inject-html",
2122
"//packages/karma:npm_package": "@bazel/karma",
2223
"//packages/protractor:npm_package": "@bazel/protractor",
2324
"//packages/rollup:npm_package": "@bazel/rollup",
@@ -51,6 +52,7 @@ example_integration_test(
5152
name = "examples_angular_view_engine",
5253
timeout = "long",
5354
npm_packages = {
55+
"//packages/inject-html:npm_package": "@bazel/inject-html",
5456
"//packages/karma:npm_package": "@bazel/karma",
5557
"//packages/protractor:npm_package": "@bazel/protractor",
5658
"//packages/rollup:npm_package": "@bazel/rollup",
@@ -90,6 +92,7 @@ example_integration_test(
9092
name = "examples_app",
9193
npm_packages = {
9294
"//packages/hide-bazel-files:npm_package": "@bazel/hide-bazel-files",
95+
"//packages/inject-html:npm_package": "@bazel/inject-html",
9396
"//packages/protractor:npm_package": "@bazel/protractor",
9497
"//packages/rollup:npm_package": "@bazel/rollup",
9598
"//packages/terser:npm_package": "@bazel/terser",
@@ -115,6 +118,7 @@ example_integration_test(
115118
example_integration_test(
116119
name = "examples_kotlin",
117120
npm_packages = {
121+
"//packages/inject-html:npm_package": "@bazel/inject-html",
118122
"//packages/jasmine:npm_package": "@bazel/jasmine",
119123
"//packages/rollup:npm_package": "@bazel/rollup",
120124
"//packages/terser:npm_package": "@bazel/terser",

examples/angular/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@bazel/benchmark-runner": "^0.1.0",
3737
"@bazel/buildifier": "^0.29.0",
3838
"@bazel/ibazel": "^0.10.3",
39+
"@bazel/inject-html": "^0.40.0",
3940
"@bazel/karma": "^0.40.0",
4041
"@bazel/protractor": "^0.40.0",
4142
"@bazel/rollup": "^0.40.0",

examples/angular/src/BUILD.bazel

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load("@io_bazel_rules_sass//:defs.bzl", "sass_binary")
55
load("@npm//@babel/cli:index.bzl", "babel")
66
load("@npm//history-server:index.bzl", "history_server")
77
load("@npm_angular_bazel//:index.bzl", "ng_module")
8+
load("@npm_bazel_inject_html//:index.bzl", "inject_html")
89
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
910
load("@npm_bazel_terser//:index.bzl", "terser_minified")
1011
load("@npm_bazel_typescript//:index.bzl", "ts_config", "ts_devserver", "ts_library")
@@ -85,23 +86,22 @@ _ASSETS = [
8586
"@npm//:node_modules/zone.js/dist/zone.min.js",
8687
]
8788

89+
inject_html(
90+
name = "inject_scripts_for_dev",
91+
src = "//src:example/index.html",
92+
assets = _ASSETS + ["_/ts_scripts.js"],
93+
)
94+
8895
# This devserver is written in Go and is super-fast.
8996
# It doesn't run any bundler or code splitter. Instead, it concatenates
9097
# named UMD and named AMD JavaScript code on-the-fly in-memory.
9198
# This scales really well for massive codebases.
9299
ts_devserver(
93100
name = "devserver",
94-
# Serve these files but don't inject tags for them into the index file
95-
# This might be because we only want to lazy-load these scripts on-demand,
96-
# or because they aren't compatible with Require.js so we must use a runtime
97-
# loader to load them.
98-
data = [
99-
"//src/assets",
100-
],
101-
# Start from the development version of the main
101+
# Serve src/example/index.html at /index.html
102+
additional_root_paths = ["src/example"],
103+
# Run the program from the development version of the main
102104
entry_module = "examples_angular/src/main.dev",
103-
# <script> and <link> tags will be automatically injected into this index file
104-
index_html = "//src/example:index.dev.html",
105105
# These scripts will be included in the JS bundle after require.js
106106
# They should have only named UMD modules, or require.js will throw.
107107
scripts = [
@@ -114,8 +114,10 @@ ts_devserver(
114114
"@npm//date-fns:date-fns.umd.js",
115115
],
116116
# Serve these files in addition to the JavaScript bundle
117-
# The corresponding <script> or <link> tags will be injected into the index_html
118-
static_files = _ASSETS,
117+
static_files = _ASSETS + [
118+
":inject_scripts_for_dev",
119+
"//src/assets",
120+
],
119121
# Tell Bazel to build the sources first
120122
deps = ["//src"],
121123
)
@@ -161,28 +163,40 @@ terser_minified(
161163
src = ":bundle-es5",
162164
)
163165

166+
inject_html(
167+
name = "inject_scripts_for_prod",
168+
src = "//src:example/index.prod.html",
169+
# we can't produce "example/index.html" since that collides with the devmode file
170+
# web_package rule will re-root this path because it starts with _prodapp
171+
out = "_prodapp/src/example/index.html",
172+
assets = _ASSETS,
173+
)
174+
164175
web_package(
165176
name = "prodapp",
166177
# In production mode we serve some polyfills with script tags that have hard-coded paths in the index.html
167178
# so we must serve them at that path, by stripping a prefix
168179
additional_root_paths = [
169180
"npm/node_modules/core-js/client",
170181
"npm/node_modules/systemjs/dist",
182+
"src/_prodapp",
171183
],
172-
# do not sort
173184
assets = _ASSETS + [
174185
":bundle-es2015.min",
175186
":bundle-es5.min",
176-
],
177-
data = [
187+
":inject_scripts_for_prod",
178188
"//src/assets",
179189
# Include polyfills that will be requested by old browsers
180190
"@npm//:node_modules/systemjs/dist/system.js",
181191
"@npm//:node_modules/core-js/client/core.min.js",
182192
"index.html",
183193
],
184-
# <script> and <link> tags will be automatically injected into this index.
185-
index_html = "//src/example:index.html",
194+
# In production mode we serve some polyfills with script tags that have hard-coded paths in the index.html
195+
# so we must serve them at that path, by stripping a prefix
196+
additional_root_paths = [
197+
"npm/node_modules/core-js/client",
198+
"npm/node_modules/systemjs/dist",
199+
],
186200
)
187201

188202
history_server(

examples/angular/src/example/BUILD.bazel

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

examples/angular/src/example/index.dev.html

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

examples/angular/src/example/index.html

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99
</head>
1010
<body>
1111
<!-- The Angular application will be bootstrapped into this element. -->
12-
<app-component></app-component>
13-
14-
<!-- in browsers that don't support ESModules, we polyfill with system.js-->
15-
<script nomodule src="/core.min.js"></script>
16-
<script nomodule src="/system.js"></script>
17-
18-
<!-- TODO: figure out how diff. loading interacts with
19-
https://www.npmjs.com/package/rollup-plugin-bundle-html -->
20-
<script type="module" src="/bundle-es2015.min/index.js"></script>
21-
<script nomodule="" src="/bundle-es5.min/index.js"></script>
12+
<app-component></app-component>
2213
</body>
2314
</html>

0 commit comments

Comments
 (0)