Skip to content

Commit

Permalink
Adopt ESM (#344)
Browse files Browse the repository at this point in the history
* esm

* node 18

* next

* 5.0.0-rc.1

* remove jsdom

* break circular import

* bump versions in README
  • Loading branch information
mbostock authored Nov 10, 2022
1 parent d9c5077 commit 9e1817b
Show file tree
Hide file tree
Showing 30 changed files with 1,096 additions and 1,792 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8
"ecmaVersion": 2020
},
"env": {
"browser": true,
"es6": true,
"es2020": true,
"node": true
},
"rules": {
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x]
node-version: [14.x, 16.x, 18.x]

steps:
- uses: actions/checkout@v1
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ For example, to render the “hello” cell from the [“Hello World” notebook
```html
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@observablehq/inspector@3/dist/inspector.css">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@observablehq/inspector@4/dist/inspector.css">
<body>
<script type="module">
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js";
import define from "https://api.observablehq.com/@observablehq/hello-world.js?v=3";
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@5/dist/runtime.js";
import define from "https://api.observablehq.com/@observablehq/hello-world.js?v=4";
const runtime = new Runtime();
const main = runtime.module(define, name => {
Expand Down
49 changes: 30 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
{
"name": "@observablehq/runtime",
"version": "4.28.0",
"license": "ISC",
"main": "dist/runtime.umd.js",
"module": "src/index.js",
"version": "5.0.0-rc.1",
"author": {
"name": "Observable, Inc.",
"url": "https://observablehq.com"
},
"license": "ISC",
"type": "module",
"main": "src/index.js",
"module": "src/index.js",
"jsdelivr": "dist/runtime.umd.js",
"unpkg": "dist/runtime.umd.js",
"exports": {
"umd": "./dist/runtime.umd.js",
"default": "./src/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/observablehq/runtime.git"
},
"files": [
"dist/**/*.js",
"src/**/*.js"
],
"scripts": {
"test": "tape -r esm 'test/**/*-test.js'",
"test": "mocha 'test/**/*-test.js' && eslint src test",
"prepublishOnly": "rm -rf dist && rollup -c",
"postpublish": "git push && git push --tags"
},
"files": [
"src/**/*.js",
"dist/**/*.js"
],
"_moduleAliases": {
"@observablehq/runtime": "./src/index.js"
},
"dependencies": {
"@observablehq/inspector": "^3.2.2",
"@observablehq/stdlib": "^3.4.1"
"@observablehq/inspector": "4.0.0-rc.1",
"@observablehq/stdlib": "4.0.0-rc.1"
},
"devDependencies": {
"eslint": "^7.18.0",
"esm": "^3.2.25",
"jsdom": "^17.0.0",
"rollup": "^2.37.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^7.0.2",
"tape": "^4.13.3",
"tape-await": "^0.1.2"
"@rollup/plugin-node-resolve": "^15.0.1",
"eslint": "^8.27.0",
"mocha": "^10.1.0",
"module-alias": "^2.2.2",
"rollup": "^3.2.5",
"rollup-plugin-terser": "^7.0.2"
},
"publishConfig": {
"tag": "next"
}
}
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import node from "rollup-plugin-node-resolve";
import node from "@rollup/plugin-node-resolve";
import {terser} from "rollup-plugin-terser";
import * as meta from "./package.json";
import * as meta from "./package.json" assert {type: "json"};

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

Expand Down
6 changes: 3 additions & 3 deletions src/array.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var prototype = Array.prototype;
export var map = prototype.map;
export var forEach = prototype.forEach;
const prototype = Array.prototype;
export const map = prototype.map;
export const forEach = prototype.forEach;
6 changes: 2 additions & 4 deletions src/constant.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export default function(x) {
return function() {
return x;
};
export function constant(x) {
return () => x;
}
10 changes: 5 additions & 5 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export function RuntimeError(message, input) {
this.message = message + "";
this.input = input;
export class RuntimeError extends Error {
constructor(message, input) {
super(message);
this.input = input;
}
}

RuntimeError.prototype = Object.create(Error.prototype);
RuntimeError.prototype.name = "RuntimeError";
RuntimeError.prototype.constructor = RuntimeError;
2 changes: 1 addition & 1 deletion src/generatorish.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function generatorish(value) {
export function generatorish(value) {
return value
&& typeof value.next === "function"
&& typeof value.return === "function";
Expand Down
2 changes: 1 addition & 1 deletion src/identity.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function(x) {
export function identity(x) {
return x;
}
10 changes: 4 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {Inspector} from "@observablehq/inspector";
import {Library} from "@observablehq/stdlib";
import {RuntimeError} from "./errors";
import Runtime from "./runtime";

export {Inspector, Library, Runtime, RuntimeError};
export {Inspector} from "@observablehq/inspector";
export {Library} from "@observablehq/stdlib";
export {RuntimeError} from "./errors.js";
export {Runtime} from "./runtime.js";
32 changes: 0 additions & 32 deletions src/load.js

This file was deleted.

35 changes: 19 additions & 16 deletions src/module.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import constant from "./constant";
import {RuntimeError} from "./errors";
import identity from "./identity";
import rethrow from "./rethrow";
import {variable_variable, variable_invalidation, variable_visibility} from "./runtime";
import Variable, {TYPE_DUPLICATE, TYPE_IMPLICIT, TYPE_NORMAL, no_observer, variable_stale} from "./variable";

export default function Module(runtime, builtins = []) {
import {constant} from "./constant.js";
import {RuntimeError} from "./errors.js";
import {identity} from "./identity.js";
import {rethrow} from "./rethrow.js";
import {Variable, TYPE_DUPLICATE, TYPE_IMPLICIT, TYPE_NORMAL, no_observer, variable_stale} from "./variable.js";

export const variable_variable = Symbol("variable");
export const variable_invalidation = Symbol("invalidation");
export const variable_visibility = Symbol("visibility");

export function Module(runtime, builtins = []) {
Object.defineProperties(this, {
_runtime: {value: runtime},
_scope: {value: new Map},
Expand All @@ -31,19 +34,19 @@ Object.defineProperties(Module.prototype, {
});

function module_redefine(name) {
var v = this._scope.get(name);
if (!v) throw new RuntimeError(name + " is not defined");
if (v._type === TYPE_DUPLICATE) throw new RuntimeError(name + " is defined more than once");
const v = this._scope.get(name);
if (!v) throw new RuntimeError(`${name} is not defined`);
if (v._type === TYPE_DUPLICATE) throw new RuntimeError(`${name} is defined more than once`);
return v.define.apply(v, arguments);
}

function module_define() {
var v = new Variable(TYPE_NORMAL, this);
const v = new Variable(TYPE_NORMAL, this);
return v.define.apply(v, arguments);
}

function module_import() {
var v = new Variable(TYPE_NORMAL, this);
const v = new Variable(TYPE_NORMAL, this);
return v.import.apply(v, arguments);
}

Expand All @@ -52,8 +55,8 @@ function module_variable(observer) {
}

async function module_value(name) {
var v = this._scope.get(name);
if (!v) throw new RuntimeError(name + " is not defined");
let v = this._scope.get(name);
if (!v) throw new RuntimeError(`${name} is not defined`);
if (v._observer === no_observer) {
v = this.variable(true).define([name], identity);
try {
Expand Down Expand Up @@ -137,7 +140,7 @@ function module_derive(injects, injectModule) {
}

function module_resolve(name) {
var variable = this._scope.get(name), value;
let variable = this._scope.get(name), value;
if (!variable) {
variable = new Variable(TYPE_IMPLICIT, this);
if (this._builtins.has(name)) {
Expand Down
2 changes: 1 addition & 1 deletion src/noop.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function() {}
export function noop() {}
6 changes: 3 additions & 3 deletions src/rethrow.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function(e) {
return function() {
throw e;
export function rethrow(error) {
return () => {
throw error;
};
}
33 changes: 12 additions & 21 deletions src/runtime.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import {Library, FileAttachments} from "@observablehq/stdlib";
import {RuntimeError} from "./errors";
import generatorish from "./generatorish";
import load from "./load";
import Module from "./module";
import noop from "./noop";
import Variable, {TYPE_IMPLICIT, no_observer, variable_stale} from "./variable";
import {RuntimeError} from "./errors.js";
import {generatorish} from "./generatorish.js";
import {Module, variable_variable, variable_invalidation, variable_visibility} from "./module.js";
import {noop} from "./noop.js";
import {Variable, TYPE_IMPLICIT, no_observer, variable_stale} from "./variable.js";

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

export var variable_variable = {};
export var variable_invalidation = {};
export var variable_visibility = {};

export default function Runtime(builtins = new Library, global = window_global) {
var builtin = this.module();
export function Runtime(builtins = new Library, global = window_global) {
const builtin = this.module();
Object.defineProperties(this, {
_dirty: {value: new Set},
_updates: {value: new Set},
Expand All @@ -28,15 +23,11 @@ export default function Runtime(builtins = new Library, global = window_global)
_builtin: {value: builtin},
_global: {value: global}
});
if (builtins) for (var name in builtins) {
if (builtins) for (const name in builtins) {
(new Variable(TYPE_IMPLICIT, builtin)).define(name, [], builtins[name]);
}
}

Object.defineProperties(Runtime, {
load: {value: load, writable: true, configurable: true}
});

Object.defineProperties(Runtime.prototype, {
_precompute: {value: runtime_precompute, writable: true, configurable: true},
_compute: {value: runtime_compute, writable: true, configurable: true},
Expand Down Expand Up @@ -91,7 +82,7 @@ function runtime_computeSoon() {
}

async function runtime_computeNow() {
var queue = [],
let queue = [],
variables,
variable,
precomputes = this._precomputes;
Expand Down Expand Up @@ -247,7 +238,7 @@ function variable_compute(variable) {
if (variable._version !== version) throw variable_stale;

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

function variable_reachable(variable) {
if (variable._observer !== no_observer) return true; // Directly reachable.
var outputs = new Set(variable._outputs);
const outputs = new Set(variable._outputs);
for (const output of outputs) {
if (output._observer !== no_observer) return true;
output._outputs.forEach(outputs.add, outputs);
Expand All @@ -370,5 +361,5 @@ function variable_reachable(variable) {
}

function window_global(name) {
return window[name];
return globalThis[name];
}
Loading

0 comments on commit 9e1817b

Please sign in to comment.