Skip to content

Commit dcd088f

Browse files
[lit-ssr] Initial commit of lit-ssr package (#1347)
* 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]>
1 parent 0cf140e commit dcd088f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+15038
-211
lines changed

.eslintrc.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@
4040
}
4141
},
4242
{
43-
"files": ["*_test.ts"],
43+
"files": [
44+
"*_test.ts",
45+
"**/custom_typings/*.ts",
46+
"packages/lit-ssr/src/test/integration/tests/**",
47+
"packages/lit-ssr/src/lib/util/parse5-utils.ts"
48+
],
4449
"rules": {
4550
"@typescript-eslint/no-explicit-any": "off"
4651
}

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ jobs:
114114
if: steps.cache.outputs.cache-hit != 'true'
115115
run: npm run bootstrap
116116

117+
- name: Lint
118+
run: npm run lint
119+
117120
- name: Build
118121
run: npm run build
119122

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# global
22
/node_modules/
33
/lerna-debug.log
4+
.DS_Store
5+
tsconfig.tsbuildinfo
46

57
# shared in packages
68
/packages/*/node_modules/
@@ -29,11 +31,14 @@
2931
/packages/lit-element/lit-element.*
3032
/packages/lit-element/lit.min.*
3133
/packages/lit-element/platform-support.*
34+
/packages/lit-element/hydrate-support.*
3235

3336
# lit-html package
3437
/packages/lit-html/directives/
3538
/packages/lit-html/lit-html.*
3639
/packages/lit-html/parts.*
40+
/packages/lit-html/hydrate.*
41+
/packages/lit-html/private-ssr-support.*
3742
/packages/lit-html/platform-support.*
3843

3944
# updating-element package
@@ -42,3 +47,15 @@
4247
/packages/updating-element/decorators.*
4348
/packages/updating-element/platform-support.*
4449
/packages/updating-element/updating-element.*
50+
51+
# lit-ssr package
52+
/packages/lit-ssr/lib
53+
/packages/lit-ssr/demo
54+
/packages/lit-ssr/test/**/*.js
55+
/packages/lit-ssr/test/**/*.js.map
56+
/packages/lit-ssr/test/**/*.d.ts
57+
/packages/lit-ssr/test/**/*.d.ts.map
58+
/packages/lit-ssr/index.js
59+
/packages/lit-ssr/index.js.map
60+
/packages/lit-ssr/index.d.ts
61+
/packages/lit-ssr/index.d.ts.map

.vscode/terminals.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"autorun": false,
3+
"terminals": [
4+
{
5+
"name": "TS: lit-html",
6+
"icon": "checklist",
7+
"cwd": "[workspaceFolder]/packages/lit-html",
8+
"command": "npm run build:ts:watch"
9+
},
10+
{
11+
"name": "TS: updating-element",
12+
"icon": "checklist",
13+
"cwd": "[workspaceFolder]/packages/updating-element",
14+
"command": "npm run build:ts:watch"
15+
},
16+
{
17+
"name": "TS: lit-element",
18+
"icon": "checklist",
19+
"cwd": "[workspaceFolder]/packages/lit-element",
20+
"command": "npm run build:ts:watch"
21+
},
22+
{
23+
"name": "TS: lit-ssr",
24+
"icon": "checklist",
25+
"cwd": "[workspaceFolder]/packages/lit-ssr",
26+
"command": "npm run build:watch",
27+
"focus": true
28+
},
29+
{
30+
"name": "Rollup: lit-html",
31+
"icon": "checklist",
32+
"cwd": "[workspaceFolder]/packages/lit-html",
33+
"command": "npm run build:watch"
34+
},
35+
{
36+
"name": "Rollup: updating-element",
37+
"icon": "checklist",
38+
"cwd": "[workspaceFolder]/packages/updating-element",
39+
"command": "npm run build:watch"
40+
},
41+
{
42+
"name": "Rollup: lit-element",
43+
"icon": "checklist",
44+
"cwd": "[workspaceFolder]/packages/lit-element",
45+
"command": "npm run build:watch"
46+
}
47+
]
48+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ following links for details on the changes being made:
1313

1414
- [`lit-html`](./packages/lit-html)
1515
- [`lit-element`](./packages/lit-element)
16+
- [`updating-element`](./packages/updating-element)
17+
- [`lit-ssr`](./packages/lit-ssr)
1618
- [`tests`](./packages/tests)
1719
- [`benchmarks`](./packages/benchmarks)
1820

lit-next.code-workspace

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
{
2-
"folders": [
3-
{
4-
"name": "lit-html",
5-
"path": "packages/lit-html"
6-
},
7-
{
8-
"name": "lit-element",
9-
"path": "packages/lit-element"
10-
},
11-
{
12-
"name": "updating-element",
13-
"path": "packages/updating-element"
14-
},
15-
{
16-
"name": "tests",
17-
"path": "packages/tests"
18-
},
19-
{
20-
"name": "benchmarks",
21-
"path": "packages/benchmarks"
22-
},
23-
{
24-
"name": "lit-next",
25-
"path": "."
26-
}
27-
],
28-
"settings": {
29-
"typescript.tsdk": "lit-html/node_modules/typescript/lib"
30-
}
2+
"folders": [
3+
{
4+
"name": "lit-html",
5+
"path": "packages/lit-html"
6+
},
7+
{
8+
"name": "lit-element",
9+
"path": "packages/lit-element"
10+
},
11+
{
12+
"name": "updating-element",
13+
"path": "packages/updating-element"
14+
},
15+
{
16+
"name": "lit-ssr",
17+
"path": "packages/lit-ssr"
18+
},
19+
{
20+
"name": "tests",
21+
"path": "packages/tests"
22+
},
23+
{
24+
"name": "benchmarks",
25+
"path": "packages/benchmarks"
26+
},
27+
{
28+
"name": "lit-next",
29+
"path": "."
30+
}
31+
],
32+
"settings": {
33+
"typescript.tsdk": "lit-html/node_modules/typescript/lib",
34+
"debug.javascript.warnOnLongPrediction": false
35+
}
3136
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"format:prettier": "prettier \"**/*.{cjs,html,js,json,md,ts}\" --ignore-path .gitignore --write",
1010
"lint": "eslint \"**/*.{js,ts}\" --ignore-path .gitignore",
1111
"nuke": "rm -rf node_modules package-lock.json packages/*/{node_modules,package-lock.json} && npm install && npm run bootstrap",
12-
"test": "cd packages/tests && npm test"
12+
"test": "(cd packages/tests && npm test) && (cd packages/lit-ssr && npm test)"
1313
},
1414
"dependencies": {
1515
"lerna": "^3.22.1"

packages/lit-element/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
"development": "./development/platform-support.js",
2222
"default": "./platform-support.js"
2323
},
24+
"./hydrate-support.js": {
25+
"development": "./development/hydrate-support.js",
26+
"default": "./hydrate-support.js"
27+
},
2428
"./decorators.js": {
2529
"development": "./development/decorators.js",
2630
"default": "./decorators.js"
@@ -32,13 +36,14 @@
3236
},
3337
"scripts": {
3438
"build": "npm run clean && tsc && rollup -c",
39+
"build:watch": "rollup -c --watch",
3540
"build:babel-test": "babel src/test/lib/decorators_test.ts --out-file test/lib/decorators-babel_test.js",
3641
"build:bundle": "npm run clean:bundle && tsc && rollup -c rollup.bundle.config.js",
3742
"build:ts": "tsc",
3843
"build:ts:watch": "tsc --watch",
3944
"check-version": "node scripts/check-version-tracker.js",
4045
"checksize": "rollup -c --environment=CHECKSIZE",
41-
"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/",
46+
"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/",
4247
"clean:bundle": "rm -rf lit.min.{js,js.map,d.ts}",
4348
"dev": "scripts/dev.sh",
4449
"prepublishOnly": "npm run check-version && npm run lint && npm test",

packages/lit-element/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {litProdConfig} from '../../rollup-common.js';
1717
export default litProdConfig({
1818
entryPoints: [
1919
'lit-element',
20+
'hydrate-support',
2021
'decorators',
2122
'decorators/customElement',
2223
'decorators/eventOptions',
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
4+
* This code may only be used under the BSD style license found at
5+
* http://polymer.github.io/LICENSE.txt
6+
* The complete set of authors may be found at
7+
* http://polymer.github.io/AUTHORS.txt
8+
* The complete set of contributors may be found at
9+
* http://polymer.github.io/CONTRIBUTORS.txt
10+
* Code distributed by Google as part of the polymer project is also
11+
* subject to an additional IP rights grant found at
12+
* http://polymer.github.io/PATENTS.txt
13+
*/
14+
15+
/**
16+
* LitElement support for hydration of content rendered using lit-ssr.
17+
*
18+
* @packageDocumentation
19+
*/
20+
21+
import {PropertyValues, UpdatingElement} from 'updating-element';
22+
import {render} from 'lit-html';
23+
import {hydrate} from 'lit-html/hydrate.js';
24+
25+
interface PatchableLitElement extends HTMLElement {
26+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-misused-new
27+
new (...args: any[]): PatchableLitElement;
28+
createRenderRoot(): Element | ShadowRoot;
29+
_needsHydration: boolean;
30+
}
31+
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
(globalThis as any)['litElementHydrateSupport'] = ({
34+
LitElement,
35+
}: {
36+
LitElement: PatchableLitElement;
37+
}) => {
38+
// Capture whether we need hydration or not
39+
const createRenderRoot = LitElement.prototype.createRenderRoot;
40+
LitElement.prototype.createRenderRoot = function () {
41+
if (this.shadowRoot) {
42+
this._needsHydration = true;
43+
return this.shadowRoot;
44+
} else {
45+
return createRenderRoot.call(this);
46+
}
47+
};
48+
49+
// Hydrate on first update when needed
50+
LitElement.prototype.update = function (changedProperties: PropertyValues) {
51+
const value = this.render();
52+
// Since this is a patch, we can't call super.update()
53+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
54+
(UpdatingElement.prototype as any).update.call(this, changedProperties);
55+
if (this._needsHydration) {
56+
this._needsHydration = false;
57+
hydrate(value, this.renderRoot, this._renderOptions);
58+
} else {
59+
render(value, this.renderRoot, this._renderOptions);
60+
}
61+
};
62+
};

0 commit comments

Comments
 (0)