From 5a65ca97464839fcd4ea59240b9910002fa64a82 Mon Sep 17 00:00:00 2001 From: Augustine Kim Date: Wed, 17 Aug 2022 18:09:35 -0700 Subject: [PATCH] [lit-html] Use existing `document` in Node build (#3223) * Use existing document if present even in node mode * This allows node build to run in DOM emulated node environments like node test runners --- .changeset/selfish-lobsters-cheat.md | 6 ++++++ packages/lit-html/src/lit-html.ts | 15 ++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 .changeset/selfish-lobsters-cheat.md diff --git a/.changeset/selfish-lobsters-cheat.md b/.changeset/selfish-lobsters-cheat.md new file mode 100644 index 0000000000..ecb7584b19 --- /dev/null +++ b/.changeset/selfish-lobsters-cheat.md @@ -0,0 +1,6 @@ +--- +'lit-html': patch +'lit': patch +--- + +Use existing `document` in Node build diff --git a/packages/lit-html/src/lit-html.ts b/packages/lit-html/src/lit-html.ts index 489910b855..185396a7ab 100644 --- a/packages/lit-html/src/lit-html.ts +++ b/packages/lit-html/src/lit-html.ts @@ -344,13 +344,14 @@ const markerMatch = '?' + marker; // syntax because it's slightly smaller, but parses as a comment node. const nodeMarker = `<${markerMatch}>`; -const d = NODE_MODE - ? ({ - createTreeWalker() { - return {}; - }, - } as unknown as Document) - : document; +const d = + NODE_MODE && global.document === undefined + ? ({ + createTreeWalker() { + return {}; + }, + } as unknown as Document) + : document; // Creates a dynamic marker. We never have to search for these in the DOM. const createMarker = (v = '') => d.createComment(v);