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