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-html] Add isServer environment checker module #3318

Merged
merged 11 commits into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/eight-hairs-run.md
@@ -0,0 +1,6 @@
---
'lit': minor
'lit-html': minor
---

Adds an `isServer` variable export to `lit` and `lit-html/is-server.js` which will be `true` in Node and `false` in the browser. This can be used when authoring components to change behavior based on whether or not the component is executing in an SSR context.
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -75,6 +75,7 @@ packages/lit-html/async-directive.*
packages/lit-html/polyfill-support.*
packages/lit-html/private-ssr-support.*
packages/lit-html/static.*
packages/lit-html/is-server.*

packages/lit-starter-js/node_modules/*
packages/lit-starter-js/docs/*
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -75,6 +75,7 @@ packages/lit-html/async-directive.*
packages/lit-html/polyfill-support.*
packages/lit-html/private-ssr-support.*
packages/lit-html/static.*
packages/lit-html/is-server.*

packages/lit-starter-js/node_modules/
packages/lit-starter-js/**/custom-elements.json
Expand Down
1 change: 1 addition & 0 deletions packages/lit-html/.gitignore
Expand Up @@ -12,3 +12,4 @@
/polyfill-support.*
/private-ssr-support.*
/static.*
/is-server.*
8 changes: 8 additions & 0 deletions packages/lit-html/package.json
Expand Up @@ -180,6 +180,12 @@
"node": "./node/static.js",
"development": "./development/static.js",
"default": "./static.js"
},
"./is-server.js": {
"types": "./development/is-server.d.ts",
"node": "./node/is-server.js",
"development": "./development/is-server.js",
"default": "./is-server.js"
}
},
"scripts": {
Expand All @@ -205,6 +211,7 @@
"/polyfill-support.{d.ts,d.ts.map,js,js.map}",
"/private-ssr-support.{d.ts,d.ts.map,js,js.map}",
"/static.{d.ts,d.ts.map,js,js.map}",
"/is-server.{d.ts,d.ts.map,js,js.map}",
"/development/",
"!/development/test/",
"/directives/",
Expand Down Expand Up @@ -266,6 +273,7 @@
"polyfill-support.js{,.map}",
"private-ssr-support.js{,.map}",
"static.js{,.map}",
"is-server.js{,.map}",
"directives/*.js{,.map}",
"test/*_test.html",
"development/test/*_test.html",
Expand Down
1 change: 1 addition & 0 deletions packages/lit-html/rollup.config.js
Expand Up @@ -39,6 +39,7 @@ export const defaultConfig = (options = {}) =>
'experimental-hydrate',
'private-ssr-support',
'polyfill-support',
'is-server',
],
bundled: [
{
Expand Down
20 changes: 20 additions & 0 deletions packages/lit-html/src/is-server.ts
@@ -0,0 +1,20 @@
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

/**
* @fileoverview
*
* This file exports a boolean const whose value will depend on what environment
* the module is being imported from.
*/

const NODE_MODE = false;

/**
* Boolean value that will `true` when imported via the "node" export condition
augustjk marked this conversation as resolved.
Show resolved Hide resolved
* and `false` otherwise.
augustjk marked this conversation as resolved.
Show resolved Hide resolved
*/
export const isServer = NODE_MODE;
augustjk marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 14 additions & 0 deletions packages/lit-html/src/test/is-server_test.ts
@@ -0,0 +1,14 @@
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/

import {isServer} from 'lit-html/is-server.js';
import {assert} from '@esm-bundle/chai';

suite('is-server', () => {
test('isServer is false', () => {
assert.strictEqual(isServer, false);
});
});
4 changes: 4 additions & 0 deletions packages/lit-html/src/test/node-imports.ts
Expand Up @@ -35,3 +35,7 @@ import 'lit-html/static.js';
import 'lit-html/experimental-hydrate.js';
import 'lit-html/private-ssr-support.js';
import 'lit-html/polyfill-support.js';

import assert from 'node:assert/strict';
import {isServer} from 'lit-html/is-server.js';
assert.strictEqual(isServer, true, 'Expected isServer to be true');
1 change: 1 addition & 0 deletions packages/lit/src/index.ts
Expand Up @@ -11,3 +11,4 @@ import '@lit/reactive-element';
import 'lit-html';

export * from 'lit-element/lit-element.js';
export * from 'lit-html/is-server.js';
4 changes: 4 additions & 0 deletions packages/lit/src/test/node-imports.ts
Expand Up @@ -45,3 +45,7 @@ import 'lit/html.js';
import 'lit/experimental-hydrate-support.js';
import 'lit/experimental-hydrate.js';
import 'lit/static-html.js';

import assert from 'node:assert/strict';
augustjk marked this conversation as resolved.
Show resolved Hide resolved
import {isServer} from 'lit';
assert.strictEqual(isServer, true, 'Expected isServer to be true');