Skip to content

Commit

Permalink
[lit-ssr] Initial commit of lit-ssr package (#1347)
Browse files Browse the repository at this point in the history
* Initial SSR commit (code copied from #hydration & lit-ssr)

* Initial lit-html rendering/hydration (element tests disabled)

* Add LitElement support (no renderLight yet)

Fix lit-element import paths

For now, build template-shadowroot as part of lit-ssr

Add lit-ssr tests to CI

Fix merge error (Template:_options)

Remove tsconfig.tsbuildinfo

Uncomment element tests ffrom server-only suite

Fix return value of createRenderRoot patch

* Add private-ssr-support to lit-html for re-exporting private symbols

* Cleanup & update README

Rename $litPrivate -> $private

Fix demo hydration

README updates

* Add renderLight directive

* Disable incremental compilation for now

* Update ssr code to new lint rules
Some minor cleanup included

* Format all and fix formatting errors

* Remove format script from lit-ssr package.json

Co-authored-by: Abdón Rodríguez Davila <[email protected]>

* Cleanup package.json

* Add a strawman for files to publish

* Add test from PolymerLabs/lit-ssr#63

* Remove `only` from test

* Add pacakge to README

* Add hydrate-support.js to exports

* Add TODOs for async directives

* Update comment to avoid it being specific to LitElement

* Remove seemingly unnecessary export

* Add comments

* Review feedback
Rename Directive:resolve -> _resolve
Add comments

* Make _start/_endNode @internal

* Remove prettier config from package

* Fix code sample to match text

* Remove dirty-check; use elementStyles

* Add todo/issue for reflecting attributes

* Add 'koa-' prefix to middleware module name

* Fix comment, remove duplication.

* Cleanup

* Address more review feedback

* Switch to escape-html

* Patch update instead of render
Return `noChage` from default render now that we don't clear the container before rendering, which was the reason for renderNotImplemented.

* Address more review feedback

* Remove middleware, move to new PR

* Add test for directive in EventPart

* Address review feedback

* Add licenses

* Add a little more to dom-shim to remove client checks

* Return an array from getTemplateHtml for terseness

* Revert _resolveValue factoring (had perf impact); use optional commitValue override

* Downgrade Chrome for time being

* Try the other direction with Chrome

* Switch to using a boolean for overriding commit, and patch _commitValue during SSR

* Revise comment

Co-authored-by: Abdón Rodríguez Davila <[email protected]>
  • Loading branch information
kevinpschaaf and abdonrd authored Oct 30, 2020
1 parent 0cf140e commit dcd088f
Show file tree
Hide file tree
Showing 52 changed files with 15,038 additions and 211 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
}
},
{
"files": ["*_test.ts"],
"files": [
"*_test.ts",
"**/custom_typings/*.ts",
"packages/lit-ssr/src/test/integration/tests/**",
"packages/lit-ssr/src/lib/util/parse5-utils.ts"
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ jobs:
if: steps.cache.outputs.cache-hit != 'true'
run: npm run bootstrap

- name: Lint
run: npm run lint

- name: Build
run: npm run build

Expand Down
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# global
/node_modules/
/lerna-debug.log
.DS_Store
tsconfig.tsbuildinfo

# shared in packages
/packages/*/node_modules/
Expand Down Expand Up @@ -29,11 +31,14 @@
/packages/lit-element/lit-element.*
/packages/lit-element/lit.min.*
/packages/lit-element/platform-support.*
/packages/lit-element/hydrate-support.*

# lit-html package
/packages/lit-html/directives/
/packages/lit-html/lit-html.*
/packages/lit-html/parts.*
/packages/lit-html/hydrate.*
/packages/lit-html/private-ssr-support.*
/packages/lit-html/platform-support.*

# updating-element package
Expand All @@ -42,3 +47,15 @@
/packages/updating-element/decorators.*
/packages/updating-element/platform-support.*
/packages/updating-element/updating-element.*

# lit-ssr package
/packages/lit-ssr/lib
/packages/lit-ssr/demo
/packages/lit-ssr/test/**/*.js
/packages/lit-ssr/test/**/*.js.map
/packages/lit-ssr/test/**/*.d.ts
/packages/lit-ssr/test/**/*.d.ts.map
/packages/lit-ssr/index.js
/packages/lit-ssr/index.js.map
/packages/lit-ssr/index.d.ts
/packages/lit-ssr/index.d.ts.map
48 changes: 48 additions & 0 deletions .vscode/terminals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"autorun": false,
"terminals": [
{
"name": "TS: lit-html",
"icon": "checklist",
"cwd": "[workspaceFolder]/packages/lit-html",
"command": "npm run build:ts:watch"
},
{
"name": "TS: updating-element",
"icon": "checklist",
"cwd": "[workspaceFolder]/packages/updating-element",
"command": "npm run build:ts:watch"
},
{
"name": "TS: lit-element",
"icon": "checklist",
"cwd": "[workspaceFolder]/packages/lit-element",
"command": "npm run build:ts:watch"
},
{
"name": "TS: lit-ssr",
"icon": "checklist",
"cwd": "[workspaceFolder]/packages/lit-ssr",
"command": "npm run build:watch",
"focus": true
},
{
"name": "Rollup: lit-html",
"icon": "checklist",
"cwd": "[workspaceFolder]/packages/lit-html",
"command": "npm run build:watch"
},
{
"name": "Rollup: updating-element",
"icon": "checklist",
"cwd": "[workspaceFolder]/packages/updating-element",
"command": "npm run build:watch"
},
{
"name": "Rollup: lit-element",
"icon": "checklist",
"cwd": "[workspaceFolder]/packages/lit-element",
"command": "npm run build:watch"
}
]
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ following links for details on the changes being made:

- [`lit-html`](./packages/lit-html)
- [`lit-element`](./packages/lit-element)
- [`updating-element`](./packages/updating-element)
- [`lit-ssr`](./packages/lit-ssr)
- [`tests`](./packages/tests)
- [`benchmarks`](./packages/benchmarks)

Expand Down
63 changes: 34 additions & 29 deletions lit-next.code-workspace
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
{
"folders": [
{
"name": "lit-html",
"path": "packages/lit-html"
},
{
"name": "lit-element",
"path": "packages/lit-element"
},
{
"name": "updating-element",
"path": "packages/updating-element"
},
{
"name": "tests",
"path": "packages/tests"
},
{
"name": "benchmarks",
"path": "packages/benchmarks"
},
{
"name": "lit-next",
"path": "."
}
],
"settings": {
"typescript.tsdk": "lit-html/node_modules/typescript/lib"
}
"folders": [
{
"name": "lit-html",
"path": "packages/lit-html"
},
{
"name": "lit-element",
"path": "packages/lit-element"
},
{
"name": "updating-element",
"path": "packages/updating-element"
},
{
"name": "lit-ssr",
"path": "packages/lit-ssr"
},
{
"name": "tests",
"path": "packages/tests"
},
{
"name": "benchmarks",
"path": "packages/benchmarks"
},
{
"name": "lit-next",
"path": "."
}
],
"settings": {
"typescript.tsdk": "lit-html/node_modules/typescript/lib",
"debug.javascript.warnOnLongPrediction": false
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"format:prettier": "prettier \"**/*.{cjs,html,js,json,md,ts}\" --ignore-path .gitignore --write",
"lint": "eslint \"**/*.{js,ts}\" --ignore-path .gitignore",
"nuke": "rm -rf node_modules package-lock.json packages/*/{node_modules,package-lock.json} && npm install && npm run bootstrap",
"test": "cd packages/tests && npm test"
"test": "(cd packages/tests && npm test) && (cd packages/lit-ssr && npm test)"
},
"dependencies": {
"lerna": "^3.22.1"
Expand Down
7 changes: 6 additions & 1 deletion packages/lit-element/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"development": "./development/platform-support.js",
"default": "./platform-support.js"
},
"./hydrate-support.js": {
"development": "./development/hydrate-support.js",
"default": "./hydrate-support.js"
},
"./decorators.js": {
"development": "./development/decorators.js",
"default": "./decorators.js"
Expand All @@ -32,13 +36,14 @@
},
"scripts": {
"build": "npm run clean && tsc && rollup -c",
"build:watch": "rollup -c --watch",
"build:babel-test": "babel src/test/lib/decorators_test.ts --out-file test/lib/decorators-babel_test.js",
"build:bundle": "npm run clean:bundle && tsc && rollup -c rollup.bundle.config.js",
"build:ts": "tsc",
"build:ts:watch": "tsc --watch",
"check-version": "node scripts/check-version-tracker.js",
"checksize": "rollup -c --environment=CHECKSIZE",
"clean": "rm -rf lit-element.{js,js.map,d.ts} decorators.{js,js.map,d.ts} platform-support.{js,js.map,d.ts} decorators/ development/",
"clean": "rm -rf lit-element.{js,js.map,d.ts} decorators.{js,js.map,d.ts} platform-support.{js,js.map,d.ts} hydrate-support.{js,js.map,d.ts} decorators/ development/",
"clean:bundle": "rm -rf lit.min.{js,js.map,d.ts}",
"dev": "scripts/dev.sh",
"prepublishOnly": "npm run check-version && npm run lint && npm test",
Expand Down
1 change: 1 addition & 0 deletions packages/lit-element/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {litProdConfig} from '../../rollup-common.js';
export default litProdConfig({
entryPoints: [
'lit-element',
'hydrate-support',
'decorators',
'decorators/customElement',
'decorators/eventOptions',
Expand Down
62 changes: 62 additions & 0 deletions packages/lit-element/src/hydrate-support.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/

/**
* LitElement support for hydration of content rendered using lit-ssr.
*
* @packageDocumentation
*/

import {PropertyValues, UpdatingElement} from 'updating-element';
import {render} from 'lit-html';
import {hydrate} from 'lit-html/hydrate.js';

interface PatchableLitElement extends HTMLElement {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-misused-new
new (...args: any[]): PatchableLitElement;
createRenderRoot(): Element | ShadowRoot;
_needsHydration: boolean;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litElementHydrateSupport'] = ({
LitElement,
}: {
LitElement: PatchableLitElement;
}) => {
// Capture whether we need hydration or not
const createRenderRoot = LitElement.prototype.createRenderRoot;
LitElement.prototype.createRenderRoot = function () {
if (this.shadowRoot) {
this._needsHydration = true;
return this.shadowRoot;
} else {
return createRenderRoot.call(this);
}
};

// Hydrate on first update when needed
LitElement.prototype.update = function (changedProperties: PropertyValues) {
const value = this.render();
// Since this is a patch, we can't call super.update()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(UpdatingElement.prototype as any).update.call(this, changedProperties);
if (this._needsHydration) {
this._needsHydration = false;
hydrate(value, this.renderRoot, this._renderOptions);
} else {
render(value, this.renderRoot, this._renderOptions);
}
};
};
21 changes: 8 additions & 13 deletions packages/lit-element/src/lit-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* @packageDocumentation
*/
import {PropertyValues, UpdatingElement} from 'updating-element';
import {render, RenderOptions} from 'lit-html';
import {render, RenderOptions, noChange} from 'lit-html';
export * from 'updating-element';
export * from 'lit-html';

Expand All @@ -77,12 +77,6 @@ declare global {
'3.0.0-pre.1'
);

/**
* Sentinal value used to avoid calling lit-html's render function when
* subclasses do not implement `render`
*/
const renderNotImplemented = {};

/**
* Base element class that manages element properties and attributes, and
* renders a lit-html template.
Expand Down Expand Up @@ -124,12 +118,9 @@ export class LitElement extends UpdatingElement {
// Setting properties in `render` should not trigger an update. Since
// updates are allowed after super.update, it's important to call `render`
// before that.
const templateResult = this.render();
const value = this.render();
super.update(changedProperties);
// If render is not implemented by the component, don't call lit-html render
if (templateResult !== renderNotImplemented) {
render(templateResult, this.renderRoot, this._renderOptions);
}
render(value, this.renderRoot, this._renderOptions);
}

/**
Expand All @@ -139,10 +130,14 @@ export class LitElement extends UpdatingElement {
* the element to update.
*/
protected render(): unknown {
return renderNotImplemented;
return noChange;
}
}

// Install hydration if available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litElementHydrateSupport']?.({LitElement});

// Apply polyfills if available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any)['litElementPlatformSupport']?.({LitElement});
19 changes: 18 additions & 1 deletion packages/lit-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@
"./directives/": {
"development": "./development/directives/",
"default": "./directives/"
},
"./hydrate.js": {
"development": "./development/hydrate.js",
"default": "./hydrate.js"
},
"./private-ssr-support.js": {
"development": "./development/private-ssr-support.js",
"default": "./private-ssr-support.js"
}
},
"scripts": {
"build": "npm run clean && tsc && rollup -c",
"build:watch": "rollup -c --watch",
"build:ts": "tsc",
"build:ts:watch": "tsc --watch",
"check-version": "node scripts/check-version-tracker.js",
"checksize": "rollup -c --environment=CHECKSIZE",
"clean": "rm -rf lit-html.{js,js.map,d.ts} platform-support.{js,js.map,d.ts} parts.{js,js.map,d.ts} directives/ development/",
"clean": "rm -rf lit-html.{js,js.map,d.ts} platform-support.{js,js.map,d.ts} parts.{js,js.map,d.ts} hydrate.{js,js.map,d.ts} private-ssr-support.{js,js.map,d.ts} directives/ development/",
"dev": "scripts/dev.sh",
"prepublishOnly": "npm run check-version",
"test": "npm run test:dev && npm run test:prod",
Expand All @@ -56,6 +65,14 @@
"/platform-support.d.ts.map",
"/platform-support.js",
"/platform-support.js.map",
"/hydrate.js",
"/hydrate.js.map",
"/hydrate.d.ts",
"/hydrate.d.ts.map",
"/private-ssr-support.js",
"/private-ssr-support.js.map",
"/private-ssr-support.d.ts",
"/private-ssr-support.d.ts.map",
"/src/",
"!/src/test/"
],
Expand Down
Loading

0 comments on commit dcd088f

Please sign in to comment.