Skip to content

Commit 2a34af8

Browse files
committed
Use tsup to build package
1 parent 7b9b0e9 commit 2a34af8

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

package.json

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,23 @@
2323
"Dan Abramov <[email protected]> (https://github.com/gaearon)",
2424
"Andrew Clark <[email protected]> (https://github.com/acdlite)"
2525
],
26-
"type": "module",
27-
"module": "dist/es/index.js",
28-
"main": "dist/cjs/index.cjs",
29-
"types": "types/index.d.ts",
26+
"main": "dist/cjs/redux.cjs",
27+
"module": "dist/redux.mjs",
28+
"types": "dist/redux.d.ts",
3029
"exports": {
3130
"./package.json": "./package.json",
3231
".": {
33-
"types": "./types/index.d.ts",
34-
"import": "./dist/es/index.js",
35-
"default": "./dist/cjs/index.cjs"
32+
"types": "./dist/redux.d.ts",
33+
"import": "./dist/redux.mjs",
34+
"default": "./dist/cjs/redux.cjs"
3635
}
3736
},
3837
"files": [
3938
"dist",
40-
"lib",
41-
"es",
42-
"src",
43-
"types"
39+
"src"
4440
],
4541
"scripts": {
46-
"clean": "rimraf lib dist es coverage types",
42+
"clean": "rimraf dist",
4743
"format": "prettier --write \"{src,test}/**/*.{js,ts}\" \"**/*.md\"",
4844
"format:check": "prettier --list-different \"{src,test}/**/*.{js,ts}\" \"**/*.md\"",
4945
"lint": "eslint --ext js,ts src test",
@@ -52,7 +48,7 @@
5248
"test:types": "tsc -p test/typescript && echo \"Typetests passed\"",
5349
"test:watch": "vitest",
5450
"test:cov": "vitest --coverage",
55-
"build": "rollup -c",
51+
"build": "tsup",
5652
"prepublishOnly": "yarn clean && yarn check-types && yarn format:check && yarn lint && yarn test",
5753
"prepack": "yarn build",
5854
"examples:lint": "eslint --ext js,ts examples",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"noUnusedLocals": true,
1616
"noUnusedParameters": true,
1717
"baseUrl": "./",
18-
"types": ["vitest/globals"],
18+
"types": ["vitest/globals", "esbuild-extra/global"],
1919
"paths": {
2020
"redux": ["src/index.ts"], // @remap-prod-remove-line
2121
"@internal/*": ["src/*"]

tsup.config.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { defineConfig, Options } from 'tsup'
2+
3+
import * as babel from '@babel/core'
4+
import { Plugin } from 'esbuild'
5+
import { getBuildExtensions } from 'esbuild-extra'
6+
7+
// Extract error strings, replace them with error codes, and write messages to a file
8+
const mangleErrorsTransform: Plugin = {
9+
name: 'mangle-errors-plugin',
10+
setup(build) {
11+
const { onTransform } = getBuildExtensions(build, 'mangle-errors-plugin')
12+
13+
onTransform({ loaders: ['ts', 'tsx'] }, async args => {
14+
const res = babel.transformSync(args.code, {
15+
parserOpts: {
16+
plugins: ['typescript']
17+
},
18+
plugins: [['./scripts/mangleErrors.cjs', { minify: false }]]
19+
})!
20+
return {
21+
code: res.code!,
22+
map: res.map!
23+
}
24+
})
25+
}
26+
}
27+
28+
export default defineConfig(options => {
29+
const commonOptions: Partial<Options> = {
30+
entry: {
31+
redux: 'src/index.ts'
32+
},
33+
esbuildPlugins: [mangleErrorsTransform],
34+
sourcemap: true,
35+
...options
36+
}
37+
38+
return [
39+
// Standard ESM, embedded `process.env.NODE_ENV` checks
40+
{
41+
...commonOptions,
42+
format: ['esm'],
43+
outExtension: () => ({ js: '.mjs' }),
44+
dts: true,
45+
clean: true
46+
},
47+
// Browser-ready ESM, production + minified
48+
{
49+
...commonOptions,
50+
entry: {
51+
'redux.browser': 'src/index.ts'
52+
},
53+
define: {
54+
'process.env.NODE_ENV': JSON.stringify('production')
55+
},
56+
format: ['esm'],
57+
outExtension: () => ({ js: '.mjs' }),
58+
minify: true
59+
},
60+
{
61+
...commonOptions,
62+
format: 'cjs',
63+
outDir: './dist/cjs/',
64+
outExtension: () => ({ js: '.cjs' })
65+
}
66+
]
67+
})

0 commit comments

Comments
 (0)