Skip to content

Commit

Permalink
[lit-html] Add isServer environment checker module (#3318)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
augustjk committed Sep 29, 2022
1 parent 305852d commit 2131307
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 0 deletions.
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
24 changes: 24 additions & 0 deletions packages/lit-html/src/is-server.ts
@@ -0,0 +1,24 @@
/**
* @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;

/**
* A boolean that will be `true` in server environments like Node, and `false`
* in browser environments. Note that your server environment or toolchain must
* support the `"node"` export condition for this to be `true`.
*
* This can be used when authoring components to change behavior based on
* whether or not the component is executing in an SSR context.
*/
export const isServer = NODE_MODE;
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';
import {isServer} from 'lit';
assert.strictEqual(isServer, true, 'Expected isServer to be true');

0 comments on commit 2131307

Please sign in to comment.