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

Lit workers #3618

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ packages/labs/vue-utils/node_modules/
packages/labs/vue-utils/index.*
packages/labs/vue-utils/wrapper-utils.*

packages/labs/workers/development/
packages/labs/workers/test/
packages/labs/workers/lib/
packages/labs/workers/index.*

packages/labs/test-projects/test-element-a/**/*.d.ts
packages/labs/test-projects/test-element-a/**/*.d.ts.map
packages/labs/test-projects/test-element-a/**/*.js
Expand Down
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ packages/labs/vue-utils/node_modules/
packages/labs/vue-utils/index.*
packages/labs/vue-utils/wrapper-utils.*

packages/labs/workers/development/
packages/labs/workers/test/
packages/labs/workers/lib/
packages/labs/workers/index.*

packages/lit-starter-js/node_modules/*
packages/lit-starter-js/docs/*
packages/lit-starter-js/docs-src/*
Expand Down
19 changes: 15 additions & 4 deletions packages/labs/ssr/src/lib/dom-shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* CustomElementRegistry however.
*/

import fetch from 'node-fetch';
import {
HTMLElement,
Element,
Expand Down Expand Up @@ -52,6 +51,7 @@ export const getWindow = ({

class CSSStyleSheet {
replace() {}
replaceSync() {}
}

const window = {
Expand Down Expand Up @@ -80,7 +80,7 @@ export const getWindow = ({
requestAnimationFrame() {},

// Set below
window: undefined as unknown,
window: globalThis,

// User-provided globals, like `require`
...props,
Expand Down Expand Up @@ -123,11 +123,22 @@ export const getWindow = ({
return window;
};

export const installWindowOnGlobal = (props: {[key: string]: unknown} = {}) => {
export const installWindowOnGlobal = (
props: {[key: string]: unknown} = {},
installWindow = false
) => {
// Avoid installing the DOM shim if one already exists
if (globalThis.window === undefined) {
const window = getWindow({props});
// Copy initial window globals to node global
Object.assign(globalThis, window);
// Object.assign(globalThis, window);
for (const [key, value] of Object.entries(window)) {
Object.defineProperty(globalThis, key, {value, writable: true});
}
if (installWindow) {
// Object.defineProperty(globalThis, 'window', {value: globalThis});
// eslint-disable-next-line
// (globalThis as any).window = globalThis;
}
}
};
4 changes: 4 additions & 0 deletions packages/labs/workers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/development/
/test/
/lib/
/index.*
1 change: 1 addition & 0 deletions packages/labs/workers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Change Log
28 changes: 28 additions & 0 deletions packages/labs/workers/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BSD 3-Clause License

Copyright (c) 2023 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.
18 changes: 18 additions & 0 deletions packages/labs/workers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# @lit-labs/workers

This package will allow Lit templates and components to be run inside a worker and render to the main document. It is highly experimental.

## Example

```ts
import {createWorkerElement} from '../lib/worker-element.js';

const localElementClass = createWorkerElement({
tagName: 'test-element',
url: new URL('./test-element.js', import.meta.url).href,
attributes: ['name'],
});
customElements.define('local-test-element', localElementClass);
```

Then every instance of `<local-test-element>` will get and run in its own worker.
149 changes: 149 additions & 0 deletions packages/labs/workers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"name": "@lit-labs/workers",
"version": "0.0.0",
"private": true,
"description": "Allows Lit to be run in web workers",
"license": "BSD-3-Clause",
"homepage": "https://lit.dev/",
"repository": {
"type": "git",
"url": "https://github.com/lit/lit.git",
"directory": "packages/labs/workers"
},
"type": "module",
"main": "index.js",
"module": "index.js",
"typings": "index.d.ts",
"directories": {
"test": "test"
},
"exports": {
".": {
"development": "./development/index.js",
"default": "./index.js"
}
},
"files": [
"/development/",
"!/development/test/",
"/index.{d.ts,d.ts.map,js,js.map}"
],
"scripts": {
"build": "wireit",
"build:ts": "wireit",
"build:ts:types": "wireit",
"build:rollup": "wireit",
"test": "wireit",
"test:dev": "wireit",
"test:prod": "wireit",
"checksize": "wireit"
},
"wireit": {
"build": {
"dependencies": [
"build:ts",
"build:ts:types",
"build:rollup",
"../../lit:build"
]
},
"build:ts": {
"command": "tsc --build --pretty",
"clean": "if-file-deleted",
"dependencies": [
"../../lit:build:ts:types",
"../ssr:build:ts"
],
"files": [
"src/**/*.ts",
"tsconfig.json"
],
"output": [
"development",
"tsconfig.tsbuildinfo"
]
},
"build:ts:types": {
"command": "treemirror development . \"**/*.d.ts{,.map}\"",
"dependencies": [
"../../internal-scripts:build",
"build:ts"
],
"files": [],
"output": [
"*.d.ts{,.map}"
]
},
"build:rollup": {
"command": "rollup -c",
"dependencies": [
"build:ts"
],
"files": [
"rollup.config.js",
"../../../rollup-common.js"
],
"output": [
"index.js{,.map}"
]
},
"test": {
"dependencies": [
"test:dev",
"test:prod"
]
},
"test:dev": {
"command": "MODE=dev node ../../tests/run-web-tests.js \"development/test/**/*_test.js\" --config ../../tests/web-test-runner.config.js",
"dependencies": [
"build",
"../../tests:build"
],
"env": {
"BROWSERS": {
"external": true
}
},
"files": [],
"output": []
},
"test:prod": {
"command": "MODE=prod node ../../tests/run-web-tests.js \"development/test/**/*_test.js\" --config ../../tests/web-test-runner.config.js",
"dependencies": [
"build",
"../../tests:build"
],
"env": {
"BROWSERS": {
"external": true
}
},
"files": [],
"output": []
},
"checksize": {
"command": "rollup -c --environment=CHECKSIZE",
"dependencies": [
"build:ts"
],
"files": [
"rollup.config.js",
"../../../rollup-common.js"
],
"output": []
}
},
"author": "Google LLC",
"devDependencies": {
"@lit-internal/scripts": "^1.0.0"
},
"dependencies": {
"lit": "^2.0.0",
"lit-html": "^2.0.0",
"@lit/reactive-element": "^1.0.0",
"@lit-labs/ssr": "^3.0.0"
},
"publishConfig": {
"access": "public"
}
}
14 changes: 14 additions & 0 deletions packages/labs/workers/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

import {litProdConfig} from '../../../rollup-common.js';
import {createRequire} from 'module';

export default litProdConfig({
packageName: createRequire(import.meta.url)('./package.json').name,
entryPoints: ['index'],
external: [],
});
7 changes: 7 additions & 0 deletions packages/labs/workers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

export {};
2 changes: 2 additions & 0 deletions packages/labs/workers/src/lib/install-dom-shim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import {installWindowOnGlobal} from '@lit-labs/ssr/lib/dom-shim.js';
installWindowOnGlobal({}, true);
59 changes: 59 additions & 0 deletions packages/labs/workers/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

import type {PropertyDeclaration, PropertyValues, TemplateResult} from 'lit';

/**
* @fileoverview
*
* Common interfaces for cross-thread messages.
*/

export type Message =
| InitializeMessage
| InitializeReplyMessage
| RenderMessage
| RenderReplyMessage
| ErrorMessage;

export type MessageKind = Message['kind'];

export interface InitializeMessage {
kind: 'initialize';
url: string;
tagName: string;
}

export interface InitializeReplyMessage {
kind: 'initialize-reply';
styles: Array<StyleMessage>;
properties: {[key: string]: PropertyDeclaration};
}

export interface RenderMessage {
kind: 'render';
changedProperties: PropertyValues;
currentValues: Record<string, unknown>;
currentAttributes: Record<string, string>;
}

export interface RenderReplyMessage {
kind: 'render-reply';
result: TemplateResult;
}

export interface ErrorMessage {
kind: 'error';
errors: Array<string>;
}

/**
* Represents a CSSResult
*/
export interface StyleMessage {
id: number;
cssText?: string;
}
Loading