diff --git a/.changeset/green-carpets-report.md b/.changeset/green-carpets-report.md new file mode 100644 index 0000000000..6fb1b5a4aa --- /dev/null +++ b/.changeset/green-carpets-report.md @@ -0,0 +1,8 @@ +--- +'@xstate/fsm': patch +--- + +author: @annaghi +pr: #2474 + +Use CommonJS files as `package.json#main` (instead of UMD files) as this plays better with native ESM loader in node (and by extension fixes compatibility issues with projects like [SvelteKit](https://kit.svelte.dev/)). diff --git a/packages/xstate-fsm/package.json b/packages/xstate-fsm/package.json index be64538f54..0325c86b71 100644 --- a/packages/xstate-fsm/package.json +++ b/packages/xstate-fsm/package.json @@ -17,13 +17,12 @@ "author": "David Khourshid ", "homepage": "https://github.com/davidkpiano/xstate/tree/main/packages/xstate-fsm#readme", "license": "MIT", - "main": "dist/xstate.fsm.js", + "main": "lib/index.js", "types": "lib/index.d.ts", "module": "es/index.js", "sideEffects": false, "files": [ - "dist/**/*.js", - "dist/**/*.d.ts", + "dist", "lib/**/*.js", "lib/**/*.d.ts", "es/**/*.js", @@ -34,7 +33,7 @@ "url": "git+ssh://git@github.com/davidkpiano/xstate.git" }, "scripts": { - "clean": "rm -rf dist lib tsconfig.tsbuildinfo", + "clean": "rm -rf lib es dist tsconfig.tsbuildinfo", "build": "rollup -c", "test": "jest", "prepublish": "npm run build" diff --git a/packages/xstate-fsm/rollup.config.js b/packages/xstate-fsm/rollup.config.js index 5edce7b5a0..6f2fed7680 100644 --- a/packages/xstate-fsm/rollup.config.js +++ b/packages/xstate-fsm/rollup.config.js @@ -3,6 +3,39 @@ import { terser } from 'rollup-plugin-terser'; import rollupReplace from 'rollup-plugin-replace'; import fileSize from 'rollup-plugin-filesize'; +const createTsPlugin = ({ declaration = true, target } = {}) => + typescript({ + clean: true, + tsconfigOverride: { + compilerOptions: { + declaration, + ...(target && { target }) + } + } + }); + +const createNpmConfig = ({ input, output }) => ({ + input, + output, + preserveModules: true, + plugins: [createTsPlugin()] +}); + +const createUmdConfig = ({ input, output, target = undefined }) => ({ + input, + output, + plugins: [ + rollupReplace({ + 'process.env.NODE_ENV': JSON.stringify('production') + }), + createTsPlugin({ declaration: false, target }), + terser({ + toplevel: true + }), + fileSize() + ] +}); + const createConfig = ({ input, output, tsconfig = undefined }) => ({ input, output, @@ -22,7 +55,25 @@ const createConfig = ({ input, output, tsconfig = undefined }) => ({ }); export default [ - createConfig({ + createNpmConfig({ + input: 'src/index.ts', + output: [ + { + dir: 'lib', + format: 'cjs' + } + ] + }), + createNpmConfig({ + input: 'src/index.ts', + output: [ + { + dir: 'es', + format: 'esm' + } + ] + }), + createUmdConfig({ input: 'src/index.ts', output: { file: 'dist/xstate.fsm.js',