Skip to content

Commit

Permalink
Switch package to use ES modules everywhere
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
robertknight committed Jan 2, 2021
1 parent 9b87405 commit 55c4b8f
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 56 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
},
"files": [
"dist/**"
]
],
"type": "module"
}
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { registerContext, useRef } from "./hooks";
import { registerContext, useRef } from "./hooks.js";

export class ContextProvider<T> {
private _listeners: Array<() => void>;
Expand Down
2 changes: 1 addition & 1 deletion src/dom-props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Props } from "./jsx";
import { Props } from "./jsx.js";

// Properties added to DOM elements rendered by UReact.
interface UReactElement extends Element {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContextProvider } from "./context";
import { ContextProvider } from "./context.js";

interface StateHook<S> {
type: "state";
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { createElement, isValidElement, jsx } from "./jsx";
export { render, unmountComponentAtNode } from "./render";
export { createContext } from "./context";
export { createElement, isValidElement, jsx } from "./jsx.js";
export { render, unmountComponentAtNode } from "./render.js";
export { createContext } from "./context.js";
export {
useCallback,
useContext,
Expand All @@ -10,5 +10,5 @@ export {
useReducer,
useRef,
useState,
} from "./hooks";
export { Fragment, createRef, memo } from "./utils";
} from "./hooks.js";
export { Fragment, createRef, memo } from "./utils.js";
8 changes: 4 additions & 4 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Props, VNode, VNodeChildren, isValidElement } from "./jsx";
import { ContextProvider } from "./context";
import { EffectTiming, HookState, setHookState } from "./hooks";
import { diffElementProps } from "./dom-props";
import { Props, VNode, VNodeChildren, isValidElement } from "./jsx.js";
import { ContextProvider } from "./context.js";
import { EffectTiming, HookState, setHookState } from "./hooks.js";
import { diffElementProps } from "./dom-props.js";

/**
* Backing tree for a rendered vnode.
Expand Down
2 changes: 1 addition & 1 deletion src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRoots } from "./render";
import { getRoots } from "./render.js";

let actDepth = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Props, Ref, VNodeChildren } from "./jsx";
import { Props, Ref, VNodeChildren } from "./jsx.js";

import { useMemo, useRef } from "./hooks";
import { useMemo, useRef } from "./hooks.js";

export function Fragment(props: Props) {
return props.children;
Expand Down
15 changes: 8 additions & 7 deletions test/context.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const { assert } = require("chai");
const sinon = require("sinon");
const { JSDOM } = require("jsdom");
import chai from "chai";
import sinon from "sinon";
import { JSDOM } from "jsdom";
const { assert } = chai;

const {
createElement: h,
import {
createElement as h,
render,
createContext,
useContext,
useMemo,
} = require("../build/index");
} from "../build/index.js";

const { delay } = require("./utils/delay");
import { delay } from "./utils/delay.js";

describe("context", () => {
let jsdom;
Expand Down
14 changes: 7 additions & 7 deletions test/hooks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { assert } = require("chai");
const sinon = require("sinon");
const { JSDOM } = require("jsdom");
import chai from "chai";
import * as sinon from "sinon";
import { JSDOM } from "jsdom";
const { assert } = chai;

const {
createElement: h,
import {
createElement as h,
render,

useCallback,
// `useContext` is not here because it is tested separately.
useEffect,
Expand All @@ -14,7 +14,7 @@ const {
useReducer,
useRef,
useState,
} = require("../build/index");
} from "../build/index.js";

describe("hooks", () => {
let jsdom;
Expand Down
7 changes: 4 additions & 3 deletions test/jsx.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { assert } = require("chai");
import chai from "chai";
const { assert } = chai;

const { elementSymbol } = require("../build/jsx");
const { createElement, isValidElement } = require("../build");
import { elementSymbol } from "../build/jsx.js";
import { createElement, isValidElement } from "../build/index.js";

describe("JSX", () => {
describe("createElement", () => {
Expand Down
13 changes: 7 additions & 6 deletions test/render.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { assert } = require("chai");
const sinon = require("sinon");
const { JSDOM } = require("jsdom");
import chai from "chai";
import sinon from "sinon";
import { JSDOM } from "jsdom";
const { assert } = chai;

const {
createElement: h,
import {
createElement as h,
render,
unmountComponentAtNode,
} = require("../build/index");
} from "../build/index.js";

/**
* Attach a numeric tag, starting at 1, to each node in a sequence.
Expand Down
15 changes: 8 additions & 7 deletions test/test-utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const { assert } = require("chai");
const sinon = require("sinon");
const { JSDOM } = require("jsdom");
import chai from "chai";
import * as sinon from "sinon";
import { JSDOM } from "jsdom";
const { assert } = chai;

const {
createElement: h,
import {
createElement as h,
render,
useEffect,
useLayoutEffect,
useState,
} = require("../build/index");
} from "../build/index.js";

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

describe("test-utils", () => {
let jsdom;
Expand Down
14 changes: 7 additions & 7 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const { assert } = require("chai");
const sinon = require("sinon");
const { JSDOM } = require("jsdom");
import chai from "chai";
import * as sinon from "sinon";
import { JSDOM } from "jsdom";
const { assert } = chai;

const {
createElement: h,
import {
createElement as h,
render,

Fragment,
createRef,
memo,
} = require("../build/index");
} from "../build/index.js";

describe("utilities", () => {
let jsdom;
Expand Down
4 changes: 1 addition & 3 deletions test/utils/delay.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
function delay(ms) {
export function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

module.exports = { delay };

0 comments on commit 55c4b8f

Please sign in to comment.