-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Declarative Shadow DOM component.
Refs #38. This component simply includes the Declarative Shadow DOM polyfill in the resulting JavaScript bundle. I left out tests for the polyfill because we don't have a Karma/browser test setup yet and even then it may be difficult to test since the browser would automatically processes any declared shadow roots. It may be possible to work around, but that's something to deal with later. Most of the complexity here is around making the component publishable to NPM. This requires an additional component at the root of the workspace just to re-export the real implementation so users can import it at `rules_prerender/declarative_shadow_dom`. See [this issue](#39 (comment)) for more information on why publishing a component looks the way it does. One awkward aspect is that there are technically two components here (`//packages/rules_prerender/declarative_shadow_dom` and `//:declarative_shadow_dom`), the former is the real implementation and the latter is the re-export. This means that both components need to be published, but both also need hand-written `BUILD.publish` files. As a result, we use `prerender_component_publish_files()` twice, even though it is a bit redundant to have two and they heavily overlap. Regardless, I think it makes more sense for managing the `BUILD.publish` files, even if one of the `prerender_component_publish_files()` isn't strictly necessary to include.
- Loading branch information
Showing
10 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
"readonly", | ||
"runfile", | ||
"semver", | ||
"shadowroot", | ||
"testonly", | ||
"transpiled", | ||
"tsjs", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,28 @@ | ||
load("@build_bazel_rules_nodejs//:index.bzl", "js_library") | ||
load("@npm//rules_prerender:index.bzl", "prerender_component") | ||
|
||
exports_files(glob(["**/*.bzl"]), visibility = ["//visibility:public"]) | ||
|
||
prerender_component( | ||
name = "declarative_shadow_dom", | ||
srcs = [ | ||
"declarative_shadow_dom.js", | ||
"declarative_shadow_dom.d.ts", | ||
], | ||
deps = ["//packages/rules_prerender/declarative_shadow_dom"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
# `js_library()` for client-side scripts must be in the workspace root so the | ||
# `path` attribute links correctly and Rollup will resolve and bundle the script | ||
# as expected. | ||
js_library( | ||
name = "_declarative_shadow_dom_scripts_lib", | ||
srcs = [ | ||
"@npm//:node_modules/rules_prerender/packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom_polyfill.mjs", | ||
"@npm//:node_modules/rules_prerender/packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom_polyfill.d.ts", | ||
], | ||
# Link under `external/npm/rules_prerender/...`. | ||
package_name = "rules_prerender", | ||
visibility = ["@npm//rules_prerender/packages/rules_prerender/declarative_shadow_dom:__pkg__"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* @fileoverview Re-exports Declarative Shadow DOM library at the root of the | ||
* `rules_prerender` package so consumers can use it by importing | ||
* `rules_prerender/declarative_shadow_dom`. | ||
*/ | ||
|
||
export * from './packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/rules_prerender/declarative_shadow_dom/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
load("@npm//@bazel/typescript:index.bzl", "ts_library") | ||
load( | ||
"//packages/rules_prerender:prerender_component.bzl", | ||
"prerender_component", | ||
) | ||
load( | ||
"//packages/rules_prerender:prerender_component_publish_files.bzl", | ||
"prerender_component_publish_files", | ||
) | ||
load("//tools:jasmine.bzl", "jasmine_node_test") | ||
load("//tools:publish.bzl", "publish_files") | ||
|
||
publish_files( | ||
name = "publish_files", | ||
files = [":declarative_shadow_dom_publish_files"], | ||
visibility = ["//packages/rules_prerender:__pkg__"], | ||
) | ||
|
||
prerender_component_publish_files( | ||
name = "declarative_shadow_dom_publish_files", | ||
dep = ":declarative_shadow_dom", | ||
) | ||
|
||
prerender_component( | ||
name = "declarative_shadow_dom", | ||
srcs = ["declarative_shadow_dom.ts"], | ||
scripts = [":declarative_shadow_dom_polyfill"], | ||
lib_deps = ["//packages/rules_prerender:scripts"], | ||
visibility = ["//:__pkg__"], | ||
) | ||
|
||
ts_library( | ||
name = "declarative_shadow_dom_test_lib", | ||
srcs = ["declarative_shadow_dom_test.ts"], | ||
testonly = True, | ||
deps = [ | ||
":declarative_shadow_dom_prerender_for_test", | ||
"@npm//@types/jasmine", | ||
], | ||
) | ||
|
||
jasmine_node_test( | ||
name = "declarative_shadow_dom_test", | ||
deps = [":declarative_shadow_dom_test_lib"], | ||
) | ||
|
||
ts_library( | ||
name = "declarative_shadow_dom_polyfill", | ||
srcs = ["declarative_shadow_dom_polyfill.ts"], | ||
) |
11 changes: 11 additions & 0 deletions
11
packages/rules_prerender/declarative_shadow_dom/BUILD.publish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
load("@npm//rules_prerender:index.bzl", "prerender_component") | ||
|
||
prerender_component( | ||
name = "declarative_shadow_dom", | ||
srcs = [ | ||
"declarative_shadow_dom.js", | ||
"declarative_shadow_dom.d.ts", | ||
], | ||
scripts = ["@npm//rules_prerender:_declarative_shadow_dom_scripts_lib"], | ||
visibility = ["@npm//rules_prerender:__pkg__"], | ||
) |
10 changes: 10 additions & 0 deletions
10
packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { includeScript } from 'rules_prerender/packages/rules_prerender/scripts'; | ||
|
||
/** | ||
* Returns a prerender annotation used by the bundler to inject the declarative | ||
* shadow DOM polyfill into the document. | ||
*/ | ||
export function polyfillDeclarativeShadowDom(): string { | ||
return includeScript( | ||
'rules_prerender/packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom_polyfill'); | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom_polyfill.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @fileoverview Polyfills declarative shadow DOM by searching for templates | ||
* which match its attributes and attaching them as real shadow roots. | ||
* | ||
* Shamelessly stolen from: https://web.dev/declarative-shadow-dom/#polyfill and | ||
* adapted for TypeScript. | ||
*/ | ||
|
||
const templates = document.querySelectorAll('template[shadowroot]') as | ||
NodeListOf<HTMLTemplateElement>; | ||
for (const template of Array.from(templates)) { | ||
const mode = template.getAttribute('shadowroot'); | ||
if (mode !== 'open' && mode !== 'closed') { | ||
console.error( | ||
`Found declarative shadow root with invalid mode: ${mode}.`); | ||
continue; | ||
} | ||
|
||
const parent = template.parentNode as Element; | ||
const shadowRoot = parent.attachShadow({ mode }); | ||
shadowRoot.appendChild(template.content); | ||
template.remove(); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom_test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'jasmine'; | ||
import { polyfillDeclarativeShadowDom } from 'rules_prerender/packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom'; | ||
|
||
describe('declarative_shadow_dom', () => { | ||
describe('polyfillDeclarativeShadowDom()', () => { | ||
it('returns an annotation to include the declarative shadow DOM polyfill', () => { | ||
expect(polyfillDeclarativeShadowDom()) | ||
.toBe('<!-- bazel:rules_prerender:PRIVATE_DO_NOT_DEPEND_OR_ELSE - {"type":"script","path":"rules_prerender/packages/rules_prerender/declarative_shadow_dom/declarative_shadow_dom_polyfill"} -->'); | ||
}); | ||
}); | ||
}); |