Skip to content

Commit 55c4b8f

Browse files
committed
Switch package to use ES modules everywhere
Use native ES modules everywhere for consistency between source and tests and to allow the output of TypeScript compilation to function both as input to Rollup packaging and execution by mocha. Node ES module support requires that file extensions are always explicitly specified. TypeScript does not generate the ".js" extension itself but counter-intuitively does allow it to be specified in the original source [1]. [1] microsoft/TypeScript#42151 (comment)
1 parent 9b87405 commit 55c4b8f

File tree

15 files changed

+59
-56
lines changed

15 files changed

+59
-56
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
},
2525
"files": [
2626
"dist/**"
27-
]
27+
],
28+
"type": "module"
2829
}

src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { registerContext, useRef } from "./hooks";
1+
import { registerContext, useRef } from "./hooks.js";
22

33
export class ContextProvider<T> {
44
private _listeners: Array<() => void>;

src/dom-props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Props } from "./jsx";
1+
import { Props } from "./jsx.js";
22

33
// Properties added to DOM elements rendered by UReact.
44
interface UReactElement extends Element {

src/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ContextProvider } from "./context";
1+
import { ContextProvider } from "./context.js";
22

33
interface StateHook<S> {
44
type: "state";

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { createElement, isValidElement, jsx } from "./jsx";
2-
export { render, unmountComponentAtNode } from "./render";
3-
export { createContext } from "./context";
1+
export { createElement, isValidElement, jsx } from "./jsx.js";
2+
export { render, unmountComponentAtNode } from "./render.js";
3+
export { createContext } from "./context.js";
44
export {
55
useCallback,
66
useContext,
@@ -10,5 +10,5 @@ export {
1010
useReducer,
1111
useRef,
1212
useState,
13-
} from "./hooks";
14-
export { Fragment, createRef, memo } from "./utils";
13+
} from "./hooks.js";
14+
export { Fragment, createRef, memo } from "./utils.js";

src/render.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Props, VNode, VNodeChildren, isValidElement } from "./jsx";
2-
import { ContextProvider } from "./context";
3-
import { EffectTiming, HookState, setHookState } from "./hooks";
4-
import { diffElementProps } from "./dom-props";
1+
import { Props, VNode, VNodeChildren, isValidElement } from "./jsx.js";
2+
import { ContextProvider } from "./context.js";
3+
import { EffectTiming, HookState, setHookState } from "./hooks.js";
4+
import { diffElementProps } from "./dom-props.js";
55

66
/**
77
* Backing tree for a rendered vnode.

src/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getRoots } from "./render";
1+
import { getRoots } from "./render.js";
22

33
let actDepth = 0;
44

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Props, Ref, VNodeChildren } from "./jsx";
1+
import { Props, Ref, VNodeChildren } from "./jsx.js";
22

3-
import { useMemo, useRef } from "./hooks";
3+
import { useMemo, useRef } from "./hooks.js";
44

55
export function Fragment(props: Props) {
66
return props.children;

test/context.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
const { assert } = require("chai");
2-
const sinon = require("sinon");
3-
const { JSDOM } = require("jsdom");
1+
import chai from "chai";
2+
import sinon from "sinon";
3+
import { JSDOM } from "jsdom";
4+
const { assert } = chai;
45

5-
const {
6-
createElement: h,
6+
import {
7+
createElement as h,
78
render,
89
createContext,
910
useContext,
1011
useMemo,
11-
} = require("../build/index");
12+
} from "../build/index.js";
1213

13-
const { delay } = require("./utils/delay");
14+
import { delay } from "./utils/delay.js";
1415

1516
describe("context", () => {
1617
let jsdom;

test/hooks.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const { assert } = require("chai");
2-
const sinon = require("sinon");
3-
const { JSDOM } = require("jsdom");
1+
import chai from "chai";
2+
import * as sinon from "sinon";
3+
import { JSDOM } from "jsdom";
4+
const { assert } = chai;
45

5-
const {
6-
createElement: h,
6+
import {
7+
createElement as h,
78
render,
8-
99
useCallback,
1010
// `useContext` is not here because it is tested separately.
1111
useEffect,
@@ -14,7 +14,7 @@ const {
1414
useReducer,
1515
useRef,
1616
useState,
17-
} = require("../build/index");
17+
} from "../build/index.js";
1818

1919
describe("hooks", () => {
2020
let jsdom;

test/jsx.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const { assert } = require("chai");
1+
import chai from "chai";
2+
const { assert } = chai;
23

3-
const { elementSymbol } = require("../build/jsx");
4-
const { createElement, isValidElement } = require("../build");
4+
import { elementSymbol } from "../build/jsx.js";
5+
import { createElement, isValidElement } from "../build/index.js";
56

67
describe("JSX", () => {
78
describe("createElement", () => {

test/render.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const { assert } = require("chai");
2-
const sinon = require("sinon");
3-
const { JSDOM } = require("jsdom");
1+
import chai from "chai";
2+
import sinon from "sinon";
3+
import { JSDOM } from "jsdom";
4+
const { assert } = chai;
45

5-
const {
6-
createElement: h,
6+
import {
7+
createElement as h,
78
render,
89
unmountComponentAtNode,
9-
} = require("../build/index");
10+
} from "../build/index.js";
1011

1112
/**
1213
* Attach a numeric tag, starting at 1, to each node in a sequence.

test/test-utils.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
const { assert } = require("chai");
2-
const sinon = require("sinon");
3-
const { JSDOM } = require("jsdom");
1+
import chai from "chai";
2+
import * as sinon from "sinon";
3+
import { JSDOM } from "jsdom";
4+
const { assert } = chai;
45

5-
const {
6-
createElement: h,
6+
import {
7+
createElement as h,
78
render,
89
useEffect,
910
useLayoutEffect,
1011
useState,
11-
} = require("../build/index");
12+
} from "../build/index.js";
1213

13-
const { act } = require("../build/test-utils");
14+
import { act } from "../build/test-utils.js";
1415

1516
describe("test-utils", () => {
1617
let jsdom;

test/utils.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const { assert } = require("chai");
2-
const sinon = require("sinon");
3-
const { JSDOM } = require("jsdom");
1+
import chai from "chai";
2+
import * as sinon from "sinon";
3+
import { JSDOM } from "jsdom";
4+
const { assert } = chai;
45

5-
const {
6-
createElement: h,
6+
import {
7+
createElement as h,
78
render,
8-
99
Fragment,
1010
createRef,
1111
memo,
12-
} = require("../build/index");
12+
} from "../build/index.js";
1313

1414
describe("utilities", () => {
1515
let jsdom;

test/utils/delay.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
function delay(ms) {
1+
export function delay(ms) {
22
return new Promise((resolve) => setTimeout(resolve, ms));
33
}
4-
5-
module.exports = { delay };

0 commit comments

Comments
 (0)