Skip to content

Commit

Permalink
Use CJS files as package.json#main for @xstate/fsm (#2899)
Browse files Browse the repository at this point in the history
* Use similar rollup.config as in core

* Update examples

* Revert version bump

* Revert "Update examples"

This reverts commit 5204fd0.

* Remove redundant Babel config from 's rollup.config.js

* Add changeset

Co-authored-by: Anna Bansaghi <[email protected]>
  • Loading branch information
Andarist and annaghi authored Dec 28, 2021
1 parent df5ffce commit 4999395
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .changeset/green-carpets-report.md
Original file line number Diff line number Diff line change
@@ -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/)).
7 changes: 3 additions & 4 deletions packages/xstate-fsm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
"author": "David Khourshid <[email protected]>",
"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",
Expand All @@ -34,7 +33,7 @@
"url": "git+ssh://[email protected]/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"
Expand Down
53 changes: 52 additions & 1 deletion packages/xstate-fsm/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down

0 comments on commit 4999395

Please sign in to comment.