Skip to content

Commit 9e1817b

Browse files
authored
Adopt ESM (#344)
* esm * node 18 * next * 5.0.0-rc.1 * remove jsdom * break circular import * bump versions in README
1 parent d9c5077 commit 9e1817b

30 files changed

+1096
-1792
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
"extends": "eslint:recommended",
33
"parserOptions": {
44
"sourceType": "module",
5-
"ecmaVersion": 8
5+
"ecmaVersion": 2020
66
},
77
"env": {
88
"browser": true,
99
"es6": true,
10+
"es2020": true,
1011
"node": true
1112
},
1213
"rules": {

.github/workflows/nodejs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
strategy:
1010
matrix:
11-
node-version: [14.x, 16.x]
11+
node-version: [14.x, 16.x, 18.x]
1212

1313
steps:
1414
- uses: actions/checkout@v1

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ For example, to render the “hello” cell from the [“Hello World” notebook
99
```html
1010
<!DOCTYPE html>
1111
<meta charset="utf-8">
12-
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@observablehq/inspector@3/dist/inspector.css">
12+
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@observablehq/inspector@4/dist/inspector.css">
1313
<body>
1414
<script type="module">
1515
16-
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js";
17-
import define from "https://api.observablehq.com/@observablehq/hello-world.js?v=3";
16+
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@5/dist/runtime.js";
17+
import define from "https://api.observablehq.com/@observablehq/hello-world.js?v=4";
1818
1919
const runtime = new Runtime();
2020
const main = runtime.module(define, name => {

package.json

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
11
{
22
"name": "@observablehq/runtime",
3-
"version": "4.28.0",
4-
"license": "ISC",
5-
"main": "dist/runtime.umd.js",
6-
"module": "src/index.js",
3+
"version": "5.0.0-rc.1",
74
"author": {
85
"name": "Observable, Inc.",
96
"url": "https://observablehq.com"
107
},
8+
"license": "ISC",
9+
"type": "module",
10+
"main": "src/index.js",
11+
"module": "src/index.js",
12+
"jsdelivr": "dist/runtime.umd.js",
13+
"unpkg": "dist/runtime.umd.js",
14+
"exports": {
15+
"umd": "./dist/runtime.umd.js",
16+
"default": "./src/index.js"
17+
},
1118
"repository": {
1219
"type": "git",
1320
"url": "https://github.com/observablehq/runtime.git"
1421
},
22+
"files": [
23+
"dist/**/*.js",
24+
"src/**/*.js"
25+
],
1526
"scripts": {
16-
"test": "tape -r esm 'test/**/*-test.js'",
27+
"test": "mocha 'test/**/*-test.js' && eslint src test",
1728
"prepublishOnly": "rm -rf dist && rollup -c",
1829
"postpublish": "git push && git push --tags"
1930
},
20-
"files": [
21-
"src/**/*.js",
22-
"dist/**/*.js"
23-
],
31+
"_moduleAliases": {
32+
"@observablehq/runtime": "./src/index.js"
33+
},
2434
"dependencies": {
25-
"@observablehq/inspector": "^3.2.2",
26-
"@observablehq/stdlib": "^3.4.1"
35+
"@observablehq/inspector": "4.0.0-rc.1",
36+
"@observablehq/stdlib": "4.0.0-rc.1"
2737
},
2838
"devDependencies": {
29-
"eslint": "^7.18.0",
30-
"esm": "^3.2.25",
31-
"jsdom": "^17.0.0",
32-
"rollup": "^2.37.1",
33-
"rollup-plugin-node-resolve": "^5.2.0",
34-
"rollup-plugin-terser": "^7.0.2",
35-
"tape": "^4.13.3",
36-
"tape-await": "^0.1.2"
39+
"@rollup/plugin-node-resolve": "^15.0.1",
40+
"eslint": "^8.27.0",
41+
"mocha": "^10.1.0",
42+
"module-alias": "^2.2.2",
43+
"rollup": "^3.2.5",
44+
"rollup-plugin-terser": "^7.0.2"
45+
},
46+
"publishConfig": {
47+
"tag": "next"
3748
}
3849
}

rollup.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import node from "rollup-plugin-node-resolve";
1+
import node from "@rollup/plugin-node-resolve";
22
import {terser} from "rollup-plugin-terser";
3-
import * as meta from "./package.json";
3+
import * as meta from "./package.json" assert {type: "json"};
44

55
const copyright = `// @observablehq/runtime v${meta.version} Copyright ${(new Date).getFullYear()} Observable, Inc.`;
66

src/array.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
var prototype = Array.prototype;
2-
export var map = prototype.map;
3-
export var forEach = prototype.forEach;
1+
const prototype = Array.prototype;
2+
export const map = prototype.map;
3+
export const forEach = prototype.forEach;

src/constant.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
export default function(x) {
2-
return function() {
3-
return x;
4-
};
1+
export function constant(x) {
2+
return () => x;
53
}

src/errors.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export function RuntimeError(message, input) {
2-
this.message = message + "";
3-
this.input = input;
1+
export class RuntimeError extends Error {
2+
constructor(message, input) {
3+
super(message);
4+
this.input = input;
5+
}
46
}
57

6-
RuntimeError.prototype = Object.create(Error.prototype);
78
RuntimeError.prototype.name = "RuntimeError";
8-
RuntimeError.prototype.constructor = RuntimeError;

src/generatorish.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function generatorish(value) {
1+
export function generatorish(value) {
22
return value
33
&& typeof value.next === "function"
44
&& typeof value.return === "function";

src/identity.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export default function(x) {
1+
export function identity(x) {
22
return x;
33
}

src/index.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import {Inspector} from "@observablehq/inspector";
2-
import {Library} from "@observablehq/stdlib";
3-
import {RuntimeError} from "./errors";
4-
import Runtime from "./runtime";
5-
6-
export {Inspector, Library, Runtime, RuntimeError};
1+
export {Inspector} from "@observablehq/inspector";
2+
export {Library} from "@observablehq/stdlib";
3+
export {RuntimeError} from "./errors.js";
4+
export {Runtime} from "./runtime.js";

src/load.js

-32
This file was deleted.

src/module.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import constant from "./constant";
2-
import {RuntimeError} from "./errors";
3-
import identity from "./identity";
4-
import rethrow from "./rethrow";
5-
import {variable_variable, variable_invalidation, variable_visibility} from "./runtime";
6-
import Variable, {TYPE_DUPLICATE, TYPE_IMPLICIT, TYPE_NORMAL, no_observer, variable_stale} from "./variable";
7-
8-
export default function Module(runtime, builtins = []) {
1+
import {constant} from "./constant.js";
2+
import {RuntimeError} from "./errors.js";
3+
import {identity} from "./identity.js";
4+
import {rethrow} from "./rethrow.js";
5+
import {Variable, TYPE_DUPLICATE, TYPE_IMPLICIT, TYPE_NORMAL, no_observer, variable_stale} from "./variable.js";
6+
7+
export const variable_variable = Symbol("variable");
8+
export const variable_invalidation = Symbol("invalidation");
9+
export const variable_visibility = Symbol("visibility");
10+
11+
export function Module(runtime, builtins = []) {
912
Object.defineProperties(this, {
1013
_runtime: {value: runtime},
1114
_scope: {value: new Map},
@@ -31,19 +34,19 @@ Object.defineProperties(Module.prototype, {
3134
});
3235

3336
function module_redefine(name) {
34-
var v = this._scope.get(name);
35-
if (!v) throw new RuntimeError(name + " is not defined");
36-
if (v._type === TYPE_DUPLICATE) throw new RuntimeError(name + " is defined more than once");
37+
const v = this._scope.get(name);
38+
if (!v) throw new RuntimeError(`${name} is not defined`);
39+
if (v._type === TYPE_DUPLICATE) throw new RuntimeError(`${name} is defined more than once`);
3740
return v.define.apply(v, arguments);
3841
}
3942

4043
function module_define() {
41-
var v = new Variable(TYPE_NORMAL, this);
44+
const v = new Variable(TYPE_NORMAL, this);
4245
return v.define.apply(v, arguments);
4346
}
4447

4548
function module_import() {
46-
var v = new Variable(TYPE_NORMAL, this);
49+
const v = new Variable(TYPE_NORMAL, this);
4750
return v.import.apply(v, arguments);
4851
}
4952

@@ -52,8 +55,8 @@ function module_variable(observer) {
5255
}
5356

5457
async function module_value(name) {
55-
var v = this._scope.get(name);
56-
if (!v) throw new RuntimeError(name + " is not defined");
58+
let v = this._scope.get(name);
59+
if (!v) throw new RuntimeError(`${name} is not defined`);
5760
if (v._observer === no_observer) {
5861
v = this.variable(true).define([name], identity);
5962
try {
@@ -137,7 +140,7 @@ function module_derive(injects, injectModule) {
137140
}
138141

139142
function module_resolve(name) {
140-
var variable = this._scope.get(name), value;
143+
let variable = this._scope.get(name), value;
141144
if (!variable) {
142145
variable = new Variable(TYPE_IMPLICIT, this);
143146
if (this._builtins.has(name)) {

src/noop.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default function() {}
1+
export function noop() {}

src/rethrow.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export default function(e) {
2-
return function() {
3-
throw e;
1+
export function rethrow(error) {
2+
return () => {
3+
throw error;
44
};
55
}

src/runtime.js

+12-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import {Library, FileAttachments} from "@observablehq/stdlib";
2-
import {RuntimeError} from "./errors";
3-
import generatorish from "./generatorish";
4-
import load from "./load";
5-
import Module from "./module";
6-
import noop from "./noop";
7-
import Variable, {TYPE_IMPLICIT, no_observer, variable_stale} from "./variable";
2+
import {RuntimeError} from "./errors.js";
3+
import {generatorish} from "./generatorish.js";
4+
import {Module, variable_variable, variable_invalidation, variable_visibility} from "./module.js";
5+
import {noop} from "./noop.js";
6+
import {Variable, TYPE_IMPLICIT, no_observer, variable_stale} from "./variable.js";
87

98
const frame = typeof requestAnimationFrame === "function" ? requestAnimationFrame
109
: typeof setImmediate === "function" ? setImmediate
1110
: f => setTimeout(f, 0);
1211

13-
export var variable_variable = {};
14-
export var variable_invalidation = {};
15-
export var variable_visibility = {};
16-
17-
export default function Runtime(builtins = new Library, global = window_global) {
18-
var builtin = this.module();
12+
export function Runtime(builtins = new Library, global = window_global) {
13+
const builtin = this.module();
1914
Object.defineProperties(this, {
2015
_dirty: {value: new Set},
2116
_updates: {value: new Set},
@@ -28,15 +23,11 @@ export default function Runtime(builtins = new Library, global = window_global)
2823
_builtin: {value: builtin},
2924
_global: {value: global}
3025
});
31-
if (builtins) for (var name in builtins) {
26+
if (builtins) for (const name in builtins) {
3227
(new Variable(TYPE_IMPLICIT, builtin)).define(name, [], builtins[name]);
3328
}
3429
}
3530

36-
Object.defineProperties(Runtime, {
37-
load: {value: load, writable: true, configurable: true}
38-
});
39-
4031
Object.defineProperties(Runtime.prototype, {
4132
_precompute: {value: runtime_precompute, writable: true, configurable: true},
4233
_compute: {value: runtime_compute, writable: true, configurable: true},
@@ -91,7 +82,7 @@ function runtime_computeSoon() {
9182
}
9283

9384
async function runtime_computeNow() {
94-
var queue = [],
85+
let queue = [],
9586
variables,
9687
variable,
9788
precomputes = this._precomputes;
@@ -247,7 +238,7 @@ function variable_compute(variable) {
247238
if (variable._version !== version) throw variable_stale;
248239

249240
// Replace any reference to invalidation with the promise, lazily.
250-
for (var i = 0, n = inputs.length; i < n; ++i) {
241+
for (let i = 0, n = inputs.length; i < n; ++i) {
251242
switch (inputs[i]) {
252243
case variable_invalidation: {
253244
inputs[i] = invalidation = variable_invalidator(variable);
@@ -361,7 +352,7 @@ function variable_return(generator) {
361352

362353
function variable_reachable(variable) {
363354
if (variable._observer !== no_observer) return true; // Directly reachable.
364-
var outputs = new Set(variable._outputs);
355+
const outputs = new Set(variable._outputs);
365356
for (const output of outputs) {
366357
if (output._observer !== no_observer) return true;
367358
output._outputs.forEach(outputs.add, outputs);
@@ -370,5 +361,5 @@ function variable_reachable(variable) {
370361
}
371362

372363
function window_global(name) {
373-
return window[name];
364+
return globalThis[name];
374365
}

0 commit comments

Comments
 (0)