Skip to content

Commit

Permalink
Refactors external example to use a separate component.
Browse files Browse the repository at this point in the history
Refs #48.

Need to refactor the way that `tsconfig.json` was managed and also realized that I needed to have a separate `tsconfig.client.json` to make sure it was compiled targetting ESM.
  • Loading branch information
dgp1130 committed Feb 12, 2023
1 parent ef6703c commit 6694e6a
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 42 deletions.
43 changes: 16 additions & 27 deletions examples/external/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@npm_user//:defs.bzl", "npm_link_all_packages")
load(
"@rules_prerender//:index.bzl",
"css_library",
"link_prerender_component",
"prerender_pages",
"web_resources",
"web_resources_devserver",
)

Expand All @@ -30,35 +28,26 @@ link_prerender_component(
visibility = ["//visibility:public"],
)

ts_config(
name = "tsconfig",
src = "tsconfig.json",
visibility = ["//visibility:public"],
)

ts_config(
name = "tsconfig_client",
src = "tsconfig.client.json",
visibility = ["//visibility:public"],
deps = [":tsconfig"],
)

prerender_pages(
name = "site",
src = "site.ts",
scripts = [":script"],
styles = [":styles"],
resources = [":resources"],
source_map = True, # TODO(#48): This doesn't seem to work?
tsconfig = "//:tsconfig.json",
tsconfig = "//:tsconfig",
lib_deps = ["//:node_modules/rules_prerender"],
deps = ["//:prerender_components/@rules_prerender/declarative_shadow_dom"],
)

ts_project(
name = "script",
srcs = ["script.ts"],
declaration = True,
source_map = True,
)

css_library(
name = "styles",
srcs = ["style.css"],
)

web_resources(
name = "resources",
entries = {
"/logo": "logo.png",
},
deps = ["//component"],
)

web_resources_devserver(
Expand Down
40 changes: 40 additions & 0 deletions examples/external/component/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load(
"@rules_prerender//:index.bzl",
"css_library",
"prerender_component",
"web_resources",
)

prerender_component(
name = "component",
srcs = ["component.ts"],
scripts = [":script"],
styles = [":style"],
resources = [":resources"],
source_map = True,
tsconfig = "//:tsconfig",
visibility = ["//:__subpackages__"],
lib_deps = ["//:node_modules/rules_prerender"],
deps = ["//:prerender_components/@rules_prerender/declarative_shadow_dom"],
)

ts_project(
name = "script",
srcs = ["script.ts"],
declaration = True,
source_map = True,
tsconfig = "//:tsconfig_client",
)

css_library(
name = "style",
srcs = ["style.css"],
)

web_resources(
name = "resources",
entries = {
"/logo": "logo.png",
},
)
18 changes: 18 additions & 0 deletions examples/external/component/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { includeScript, inlineStyle } from 'rules_prerender';
import { polyfillDeclarativeShadowDom } from '@rules_prerender/declarative_shadow_dom';

export function renderComponent(): string {
return `
<my-component>
<template shadowroot="open">
<img src="/logo" />
<div>Component</div>
<div id="replace">This text to be replaced by JavaScript.</div>
${polyfillDeclarativeShadowDom()}
${includeScript('component/script.js')}
${inlineStyle('external/component/style.css')}
</template>
</my-component>
`.trim();
}
File renamed without changes
9 changes: 9 additions & 0 deletions examples/external/component/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class MyComponent extends HTMLElement {
connectedCallback(): void {
const el = this.shadowRoot.querySelector('#replace');
if (!el) throw new Error('Could not find `#replace` element.');
el.textContent = 'This text rendered by page JavaScript!';
}
}

customElements.define('my-component', MyComponent);
7 changes: 7 additions & 0 deletions examples/external/component/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:host {
display: block;
}

span {
color: red;
}
1 change: 0 additions & 1 deletion examples/external/script.ts

This file was deleted.

14 changes: 3 additions & 11 deletions examples/external/site.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { includeScript, inlineStyle, PrerenderResource } from 'rules_prerender';
import { polyfillDeclarativeShadowDom } from '@rules_prerender/declarative_shadow_dom';
import { PrerenderResource } from 'rules_prerender';
import { renderComponent } from './component/component';

export default function*(): Generator<PrerenderResource, void, void> {
yield PrerenderResource.of('/index.html', `
Expand All @@ -11,16 +11,8 @@ export default function*(): Generator<PrerenderResource, void, void> {
</head>
<body>
<h2>Hello, World!</h2>
<img src="/logo">
<div>
<template shadowroot="open">
${polyfillDeclarativeShadowDom()}
<h2>Component</h2>
${includeScript('script.js')}
${inlineStyle('external/style.css')}
</template>
</div>
${renderComponent()}
</body>
</html>
`.trim());
Expand Down
3 changes: 0 additions & 3 deletions examples/external/style.css

This file was deleted.

7 changes: 7 additions & 0 deletions examples/external/tsconfig.client.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
// Override module so `tsc` generates JS with ESM `import` instead of CommonJS `require()`.
"module": "ES2020",
}
}

0 comments on commit 6694e6a

Please sign in to comment.