-
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 polyfill.
Refs #38. TODO
- Loading branch information
Showing
8 changed files
with
124 additions
and
1 deletion.
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
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,4 +1,22 @@ | ||
load(":prerender_component.bzl", "prerender_component") | ||
load("@npm//@bazel/typescript:index.bzl", "ts_library") | ||
|
||
exports_files( | ||
["rollup-default.config.js"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
# TESTME | ||
# TODO(#39): Don't ship TypeScript code to NPM and compile it on the user's cpu. | ||
prerender_component( | ||
name = "declarative_shadow_dom", | ||
srcs = ["declarative_shadow_dom.ts"], | ||
scripts = [":declarative_shadow_dom_polyfill"], | ||
lib_deps = ["@npm//rules_prerender"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
ts_library( | ||
name = "declarative_shadow_dom_polyfill", | ||
srcs = ["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,10 @@ | ||
import { includeScript } from 'rules_prerender'; | ||
|
||
/** | ||
* 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_script'); | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/rules_prerender/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,21 @@ | ||
/** | ||
* Shamelessly stolen from: https://web.dev/declarative-shadow-dom/#polyfill and | ||
* adapted for TypeScript. | ||
*/ | ||
export function polyfillDeclarativeShadowDom(): void { | ||
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(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/rules_prerender/declarative_shadow_dom_polyfill_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,9 @@ | ||
import 'jasmine'; | ||
|
||
describe('declarative_shadow_dom_polyfill', () => { | ||
describe('polyfillDeclarativeShadowDom()', () => { | ||
it('polyfills declarative shadow DOM', () => { | ||
// TODO | ||
}); | ||
}); | ||
}); |
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,4 @@ | ||
import { polyfillDeclarativeShadowDom } from './declarative_shadow_dom_polyfill'; | ||
|
||
// Polyfill on page load. | ||
polyfillDeclarativeShadowDom(); |
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'; | ||
|
||
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_script"} -->'); | ||
}); | ||
}); | ||
}); |
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