Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ElementRenderer and DOM shim directly into SSR package, remove lit-labs/ssr dependency #5997

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/web-components/fast-ssr/ACKNOWLEDGEMENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Acknowledgements
A big thank you to [Lit](https://github.com/lit) for their work on server-side rendering for Web Components. This package builds off the ideas and tentative community protocols proposed and implemented in [@lit-labs/ssr](https://github.com/lit/lit/tree/main/packages/labs/ssr), with the goal of aligning `@microsoft/fast-ssr` to these community protocols as they mature.

Several modules have been copied with some modification from [@lit-labs/ssr](https://github.com/lit/lit/tree/main/packages/labs/ssr). Our hope is that as these protocols mature, these files can be removed and common resources leveraged instead. The following list of files have been copied / modified:
- [./src/element-renderer/element-renderer.ts]()
- [./src/template-parser/template-parser.ts]()
- [./src/escape-html.ts]()
- [./src/dom-shim.ts]()
- [./src/render-info.ts]()

and written with the following [license](https://github.com/lit/lit/blob/main/packages/labs/ssr/LICENSE):

---
BSD 3-Clause License

Copyright (c) 2019 Google LLC. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 changes: 30 additions & 2 deletions packages/web-components/fast-ssr/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,37 @@
import { Binding } from '@microsoft/fast-element';
import { ComposableStyles } from '@microsoft/fast-element';
import { Constructable } from '@microsoft/fast-element';
import { ElementRenderer } from '@lit-labs/ssr';
import { ExecutionContext } from '@microsoft/fast-element';
import { FASTElement } from '@microsoft/fast-element';
import { RenderInfo } from '@lit-labs/ssr';
import { ViewBehaviorFactory } from '@microsoft/fast-element';
import { ViewTemplate } from '@microsoft/fast-element';

// @beta
export type ComponentDOMEmissionMode = "shadow";

// @beta (undocumented)
export type ConstructableElementRenderer = (new (tagName: string) => ElementRenderer) & typeof ElementRenderer;

// @beta (undocumented)
export abstract class ElementRenderer {
constructor(tagName: string);
// (undocumented)
abstract attributeChangedCallback(name: string, prev: string | null, next: string | null): void;
// (undocumented)
abstract connectedCallback(): void;
// (undocumented)
abstract readonly element?: HTMLElement;
// Warning: (ae-forgotten-export) The symbol "AttributesMap" needs to be exported by the entry point exports.d.ts
static matchesClass(ctor: typeof HTMLElement, tagName: string, attributes: AttributesMap): boolean;
renderAttributes(): IterableIterator<string>;
// (undocumented)
abstract renderShadow(renderInfo: RenderInfo): IterableIterator<string>;
setAttribute(name: string, value: string): void;
setProperty(name: string, value: unknown): void;
// (undocumented)
readonly tagName: string;
}

// @beta
export abstract class FASTElementRenderer extends ElementRenderer {
constructor(tagName: string);
Expand All @@ -38,6 +59,13 @@ function fastSSR(): {
};
export default fastSSR;

// @beta (undocumented)
export type RenderInfo = {
elementRenderers: ConstructableElementRenderer[];
customElementInstanceStack: ElementRenderer[];
customElementHostStack: ElementRenderer[];
};

// @beta
export interface StyleRenderer {
render(styles: ComposableStyles): string;
Expand Down
11 changes: 7 additions & 4 deletions packages/web-components/fast-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@
"types": "./dist/fast-ssr.d.ts",
"exports": {
"." : {
"default": "./dist/esm/exports.js",
"types": "./dist/fast-ssr.d.ts"
"types": "./dist/fast-ssr.d.ts",
"default": "./dist/esm/exports.js"
},
"./install-dom-shim": "./dist/esm/dom-shim.js"
"./install-dom-shim": "./dist/esm/install-dom-shim.js",
"./dom-shim": {
"types": "./dist/dts/dom-shim.d.ts",
"default": "./dist/esm/dom-shim.js"
}
},
"private": true,
"dependencies": {
"@lit-labs/ssr": "^2.1.0",
"@microsoft/fast-element": "^1.5.0",
"parse5": "^6.0.1",
"tslib": "^2.4.0"
Expand Down
23 changes: 23 additions & 0 deletions packages/web-components/fast-ssr/src/dom-shim.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect, test } from "@playwright/test";
import { createWindow } from "./dom-shim.js";

test.describe("createWindow", () => {
test("should create a window with a document property that is an instance of the window's Document constructor", () => {
const window = createWindow();

expect(window.document instanceof (window.Document as any)).toBe(true);

class MyDocument {}
const windowOverride = createWindow({ Document: MyDocument });
expect(windowOverride.document instanceof MyDocument).toBe(true);
});
test("should create a window with a customElements property that is an instance of the window's CustomElementRegistry constructor", () => {
const window = createWindow();

expect(window.customElements instanceof (window.CustomElementRegistry as any)).toBe(true);

class MyRegistry {}
const windowOverride = createWindow({ CustomElementRegistry: MyRegistry });
expect(windowOverride.customElements instanceof MyRegistry).toBe(true);
});
})
Loading