From 8f026f364a2a4049a10c37004cae198650fdc756 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 6 Apr 2022 14:37:01 -0700 Subject: [PATCH] feat: improve ESM output --- jest.config.js | 12 +++++++---- package.json | 5 +++-- src/context.ts | 2 +- src/core.ts | 15 +++++++------- src/effect.ts | 5 +++-- src/errors.ts | 4 ++-- src/execute-result.ts | 6 +++--- src/index.ts | 18 ++++++++--------- src/react/createFizzContext.tsx | 13 ++++++------ src/react/index.ts | 2 +- src/runtime.ts | 20 +++++++++++-------- src/state.ts | 5 +++-- src/subscriptions.ts | 4 +++- .../{createMachine.tsx => createMachine.ts} | 8 ++++---- src/svelte/index.ts | 2 +- tsconfig.json | 4 +--- yarn.lock | 16 +++++++-------- 17 files changed, 77 insertions(+), 64 deletions(-) rename src/svelte/{createMachine.tsx => createMachine.ts} (88%) diff --git a/jest.config.js b/jest.config.js index 5264dd16..e21446b5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -62,10 +62,13 @@ module.exports = { // A set of global variables that need to be available in all test environments globals: { "ts-jest": { + useESM: true, diagnostics: false, }, }, + // extensionsToTreatAsEsm: [".ts", ".tsx"], + // An array of directory names to be searched recursively up from the requiring module's location // moduleDirectories: [ // "node_modules" @@ -75,7 +78,9 @@ module.exports = { moduleFileExtensions: ["js", "json", "jsx", "ts", "tsx", "node", "svelte"], // A map from regular expressions to module names that allow to stub out resources with a single module - // moduleNameMapper: {}, + moduleNameMapper: { + "^(\\.{1,2}/.*)\\.js$": "$1", + }, // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader // modulePathIgnorePatterns: [], @@ -87,7 +92,7 @@ module.exports = { // notifyMode: "failure-change", // A preset that is used as a base for Jest's configuration - preset: "ts-jest", + preset: "ts-jest/presets/default-esm", // Run tests from one or more projects // projects: null, @@ -161,8 +166,7 @@ module.exports = { // A map from regular expressions to paths to transformers transform: { - "^.+\\.ts$": "ts-jest", - "^.+\\.tsx$": "ts-jest", + "^.+\\.tsx?$": "ts-jest", "^.+\\.svelte$": [ "svelte-jester", { diff --git a/package.json b/package.json index 62061d2b..eb118509 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ } }, "files": [ + "src", "dist" ], "scripts": { @@ -48,7 +49,7 @@ "@semantic-release/github": "^8.0.4", "@testing-library/jest-dom": "^5.16.1", "@testing-library/react": "^12.1.3", - "@testing-library/svelte": "^3.0.3", + "@testing-library/svelte": "^3.1.0", "@types/jest": "^27.4.0", "@types/lodash.flatten": "^4.4.6", "@types/lodash.isfunction": "^3.0.6", @@ -75,7 +76,7 @@ "svelte-jester": "^2.3.2", "ts-jest": "^27.0.7", "typescript": "^4.6.2", - "svelte-preprocess": "^4.10.4", + "svelte-preprocess": "^4.10.5", "svelte": "^3.46" }, "dependencies": { diff --git a/src/context.ts b/src/context.ts index 2b85ab5b..044118e9 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,4 +1,4 @@ -import { StateTransition } from "./state" +import { StateTransition } from "./state.js" export class History< T extends StateTransition = StateTransition, diff --git a/src/core.ts b/src/core.ts index ecd97e0f..f8185c52 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,16 +1,17 @@ /* eslint-disable @typescript-eslint/no-use-before-define, @typescript-eslint/no-misused-promises */ -import { Task } from "@tdreyno/pretty-please" -import { Action, enter, exit, isAction } from "./action" -import { Context } from "./context" -import { __internalEffect, Effect, isEffect, log } from "./effect" +import { Action, enter, exit, isAction } from "./action.js" +import { Effect, __internalEffect, isEffect, log } from "./effect.js" +import { ExecuteResult, executeResultfromTask } from "./execute-result.js" import { MissingCurrentState, StateDidNotRespondToAction, UnknownStateReturnType, } from "./errors" -import { isStateTransition, StateReturn, StateTransition } from "./state" -import { ExecuteResult, executeResultfromTask } from "./execute-result" -import { arraySingleton } from "./util" +import { StateReturn, StateTransition, isStateTransition } from "./state.js" + +import { Context } from "./context.js" +import { Task } from "@tdreyno/pretty-please" +import { arraySingleton } from "./util.js" const enterState = ( context: Context, diff --git a/src/effect.ts b/src/effect.ts index b76a857d..b3a19786 100644 --- a/src/effect.ts +++ b/src/effect.ts @@ -1,7 +1,8 @@ /* eslint-disable @typescript-eslint/no-misused-promises */ import { Subscription, Task } from "@tdreyno/pretty-please" -import { Action } from "./action" -import { Context } from "./context" + +import { Action } from "./action.js" +import { Context } from "./context.js" export interface Effect { label: string diff --git a/src/errors.ts b/src/errors.ts index 05ddd9e2..2475b67f 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,5 +1,5 @@ -import { Action } from "./action" -import { StateTransition } from "./state" +import { Action } from "./action.js" +import { StateTransition } from "./state.js" export class StateDidNotRespondToAction extends Error { constructor( diff --git a/src/execute-result.ts b/src/execute-result.ts index a1989a72..6b5e9175 100644 --- a/src/execute-result.ts +++ b/src/execute-result.ts @@ -1,7 +1,7 @@ +import { Effect } from "./effect.js" +import { StateReturn } from "./state.js" import { Task } from "@tdreyno/pretty-please" -import { Effect } from "./effect" -import { StateReturn } from "./state" -import { arraySingleton } from "./util" +import { arraySingleton } from "./util.js" class ExecuteResult_ { constructor( diff --git a/src/index.ts b/src/index.ts index 8ba5bbc3..b2d61553 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,10 @@ -export * from "./action" -export * from "./context" -export * from "./core" -export * from "./effect" -export * from "./errors" -export * from "./runtime" -export * from "./state" -export * from "./subscriptions" -export * from "./execute-result" +export * from "./action.js" +export * from "./context.js" +export * from "./core.js" +export * from "./effect.js" +export * from "./errors.js" +export * from "./runtime.js" +export * from "./state.js" +export * from "./subscriptions.js" +export * from "./execute-result.js" export { Task } from "@tdreyno/pretty-please" diff --git a/src/react/createFizzContext.tsx b/src/react/createFizzContext.tsx index b74477b8..8c8a5457 100644 --- a/src/react/createFizzContext.tsx +++ b/src/react/createFizzContext.tsx @@ -1,4 +1,6 @@ -import isFunction from "lodash.isfunction" +import { Action, enter } from "../action.js" +import { BoundStateFn, StateTransition, stateWrapper } from "../state.js" +import { Context, createInitialContext } from "../context.js" import React, { ReactNode, useContext, @@ -6,11 +8,10 @@ import React, { useMemo, useState, } from "react" -import { Action, enter } from "../action" -import { Context, createInitialContext } from "../context" -import { noop } from "../effect" -import { createRuntime, Runtime } from "../runtime" -import { BoundStateFn, stateWrapper, StateTransition } from "../state" +import { Runtime, createRuntime } from "../runtime.js" + +import isFunction from "lodash.isfunction" +import { noop } from "../effect.js" export interface CreateProps< SM extends { [key: string]: BoundStateFn }, diff --git a/src/react/index.ts b/src/react/index.ts index c50c51e5..1a2b27d7 100644 --- a/src/react/index.ts +++ b/src/react/index.ts @@ -1 +1 @@ -export * from "./createFizzContext" +export * from "./createFizzContext.js" diff --git a/src/runtime.ts b/src/runtime.ts index b433a53a..f9f66326 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -1,12 +1,16 @@ +import { Action, isAction } from "./action.js" +import { BoundStateFn, StateTransition } from "./state.js" +import { ExecuteResult, executeResultfromTask } from "./execute-result.js" import { ExternalTask, Subscription, Task } from "@tdreyno/pretty-please" -import { Action, isAction } from "./action" -import { Context } from "./context" -import { execute, processStateReturn, runEffects } from "./core" -import { Effect } from "./effect" -import { NoStatesRespondToAction, StateDidNotRespondToAction } from "./errors" -import { ExecuteResult, executeResultfromTask } from "./execute-result" -import { BoundStateFn, StateTransition } from "./state" -import { isNotEmpty } from "./util" +import { + NoStatesRespondToAction, + StateDidNotRespondToAction, +} from "./errors.js" +import { execute, processStateReturn, runEffects } from "./core.js" + +import { Context } from "./context.js" +import { Effect } from "./effect.js" +import { isNotEmpty } from "./util.js" type ContextChangeSubscriber = (context: Context) => void diff --git a/src/state.ts b/src/state.ts index 87a86abf..2466ec2c 100644 --- a/src/state.ts +++ b/src/state.ts @@ -1,8 +1,9 @@ +import { Action, ActionName, ActionPayload } from "./action.js" + +import { Effect } from "./effect.js" import { Task } from "@tdreyno/pretty-please" import isPlainObject from "lodash.isplainobject" import mapValues from "lodash.mapvalues" -import { Action, ActionName, ActionPayload } from "./action" -import { Effect } from "./effect" /** * States can return either: diff --git a/src/subscriptions.ts b/src/subscriptions.ts index 3b993b05..bb1ab631 100644 --- a/src/subscriptions.ts +++ b/src/subscriptions.ts @@ -1,5 +1,7 @@ +import { Action, onFrame } from "./action.js" + import { Subscription } from "@tdreyno/pretty-please" -import { Action, onFrame } from "./action" + export { Subscription } export const onFrameSubscription = >( diff --git a/src/svelte/createMachine.tsx b/src/svelte/createMachine.ts similarity index 88% rename from src/svelte/createMachine.tsx rename to src/svelte/createMachine.ts index ed4f6aaf..f62f3863 100644 --- a/src/svelte/createMachine.tsx +++ b/src/svelte/createMachine.ts @@ -1,8 +1,8 @@ +import { Action, enter } from "../action.js" +import { BoundStateFn, StateTransition } from "../state.js" +import { Context, createInitialContext } from "../context.js" import { Readable, readable } from "svelte/store" -import { Action, enter } from "../action" -import { Context, createInitialContext } from "../context" -import { createRuntime, Runtime } from "../runtime" -import { BoundStateFn, StateTransition } from "../state" +import { Runtime, createRuntime } from "../runtime.js" export interface ContextValue< SM extends { [key: string]: BoundStateFn }, diff --git a/src/svelte/index.ts b/src/svelte/index.ts index 6a630b9f..c9228b36 100644 --- a/src/svelte/index.ts +++ b/src/svelte/index.ts @@ -1 +1 @@ -export * from "./createMachine" +export * from "./createMachine.js" diff --git a/tsconfig.json b/tsconfig.json index 58880f06..9b8fb031 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,10 +17,8 @@ "noEmit": false, "sourceMap": true, "declaration": true, - "declarationMap": true, "rootDir": "./src", - "outDir": "./dist/esm", - "resolveJsonModule": true + "outDir": "./dist/esm" }, "include": ["./src"], "exclude": ["build", "dist", "node_modules"] diff --git a/yarn.lock b/yarn.lock index 939c9eff..96225809 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1172,10 +1172,10 @@ "@testing-library/dom" "^8.0.0" "@types/react-dom" "*" -"@testing-library/svelte@^3.0.3": - version "3.0.3" - resolved "https://registry.npmjs.org/@testing-library/svelte/-/svelte-3.0.3.tgz" - integrity sha512-GxafAllShGM2nkntFGURZ7fYVlUYwv7K62lqv1aFqtTYzzeZ2Cu8zTVhtE/Qt3bk2zMl6+FPKP03wjLip/G8mA== +"@testing-library/svelte@^3.1.0": + version "3.1.0" + resolved "https://registry.yarnpkg.com/@testing-library/svelte/-/svelte-3.1.0.tgz#57dff119fe2a4776873990fd0331e83a0599916c" + integrity sha512-xTN6v4xRLQb75GTJn2mrjSUJN4PkhpNZFjwvtdzbOTS6OvxMrkRdm6hFRGauwiFd0LPV7/SqdWbbtMAOC7a+Dg== dependencies: "@testing-library/dom" "^7.0.3" @@ -6388,10 +6388,10 @@ svelte-jester@^2.3.2: resolved "https://registry.yarnpkg.com/svelte-jester/-/svelte-jester-2.3.2.tgz#9eb818da30807bbcc940b6130d15b2c34408d64f" integrity sha512-JtxSz4FWAaCRBXbPsh4LcDs4Ua7zdXgLC0TZvT1R56hRV0dymmNP+abw67DTPF7sQPyNxWsOKd0Sl7Q8SnP8kg== -svelte-preprocess@^4.10.4: - version "4.10.4" - resolved "https://registry.yarnpkg.com/svelte-preprocess/-/svelte-preprocess-4.10.4.tgz#308a410266bfc55b4e608da8d552b63580141260" - integrity sha512-fuwol0N4UoHsNQolLFbMqWivqcJ9N0vfWO9IuPAiX/5okfoGXURyJ6nECbuEIv0nU3M8Xe2I1ONNje2buk7l6A== +svelte-preprocess@^4.10.5: + version "4.10.5" + resolved "https://registry.yarnpkg.com/svelte-preprocess/-/svelte-preprocess-4.10.5.tgz#c4d20fd67b92559e5cac80281154c813c1c17353" + integrity sha512-VKXPRScCzAZqeBZOGq4LLwtNrAu++mVn7XvQox3eFDV7Ciq0Lg70Q8QWjH9iXF7J+pMlXhPsSFwpCb2E+hoeyA== dependencies: "@types/pug" "^2.0.4" "@types/sass" "^1.16.0"