-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsup.config.ts
138 lines (129 loc) · 4.08 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as path from 'node:path'
import { defineConfig, Options } from 'tsup'
import * as glob from 'glob'
import {
getWorkerChecksum,
copyWorkerPlugin,
} from './config/plugins/esbuild/copyWorkerPlugin'
import { resolveCoreImportsPlugin } from './config/plugins/esbuild/resolveCoreImportsPlugin'
import { forceEsmExtensionsPlugin } from './config/plugins/esbuild/forceEsmExtensionsPlugin'
import packageJson from './package.json'
// Externalize the in-house dependencies so that the user
// would get the latest published version automatically.
const ecosystemDependencies = /^@mswjs\/(.+)$/
// Externalize the core functionality (reused across environments)
// so that it can be shared between the environments.
const mswCore = /\/core(\/.+)?$/
const SERVICE_WORKER_CHECKSUM = getWorkerChecksum()
const coreConfig: Options = {
name: 'core',
platform: 'neutral',
entry: glob.sync('./src/core/**/*.ts', {
ignore: '**/*.test.ts',
}),
external: [ecosystemDependencies],
format: ['esm', 'cjs'],
outDir: './lib/core',
bundle: false,
splitting: false,
sourcemap: true,
dts: true,
tsconfig: path.resolve(__dirname, 'src/tsconfig.core.build.json'),
esbuildPlugins: [forceEsmExtensionsPlugin()],
}
const nodeConfig: Options = {
name: 'node',
platform: 'node',
entry: ['./src/node/index.ts'],
inject: ['./config/polyfills-node.ts'],
external: [mswCore, ecosystemDependencies],
format: ['esm', 'cjs'],
outDir: './lib/node',
bundle: true,
splitting: false,
sourcemap: true,
dts: true,
tsconfig: path.resolve(__dirname, 'src/tsconfig.node.build.json'),
esbuildPlugins: [resolveCoreImportsPlugin(), forceEsmExtensionsPlugin()],
}
const browserConfig: Options = {
name: 'browser',
platform: 'browser',
entry: ['./src/browser/index.ts'],
external: [mswCore, ecosystemDependencies],
format: ['esm', 'cjs'],
outDir: './lib/browser',
bundle: true,
splitting: false,
sourcemap: true,
dts: true,
noExternal: Object.keys(packageJson.dependencies).filter((packageName) => {
/**
* @note Never bundle MSW core so all builds reference the *same*
* JavaScript and TypeScript care files. This way types across
* export paths remain compatible:
* import { http } from 'msw' // <- core
* import { setupWorker } from 'msw/browser' // <- /browser
* setupWorker(http.get(path, resolver)) // OK
*/
return !mswCore.test(packageName)
}),
/**
* @note Use a proxy TypeScript configuration where the "compilerOptions.composite"
* option is set to false.
* @see https://github.com/egoist/tsup/issues/571
*/
tsconfig: path.resolve(__dirname, 'src/browser/tsconfig.browser.build.json'),
define: {
SERVICE_WORKER_CHECKSUM: JSON.stringify(SERVICE_WORKER_CHECKSUM),
},
esbuildPlugins: [
resolveCoreImportsPlugin(),
forceEsmExtensionsPlugin(),
copyWorkerPlugin(SERVICE_WORKER_CHECKSUM),
],
}
const reactNativeConfig: Options = {
name: 'react-native',
platform: 'node',
entry: ['./src/native/index.ts'],
external: ['chalk', 'util', 'events', mswCore, ecosystemDependencies],
format: ['esm', 'cjs'],
outDir: './lib/native',
bundle: true,
splitting: false,
sourcemap: true,
dts: true,
tsconfig: path.resolve(__dirname, 'src/tsconfig.node.build.json'),
esbuildPlugins: [resolveCoreImportsPlugin(), forceEsmExtensionsPlugin()],
}
const iifeConfig: Options = {
name: 'iife',
platform: 'browser',
globalName: 'MockServiceWorker',
entry: ['./src/iife/index.ts'],
/**
* @note Legacy output format will automatically create
* a "iife" directory under the "outDir".
*/
outDir: './lib',
format: ['iife'],
legacyOutput: true,
bundle: true,
splitting: false,
sourcemap: true,
dts: false,
tsconfig: path.resolve(__dirname, 'src/browser/tsconfig.browser.build.json'),
define: {
// Sign the IIFE build as well because any bundle containing
// the worker API must have the the integrity checksum defined.
SERVICE_WORKER_CHECKSUM: JSON.stringify(SERVICE_WORKER_CHECKSUM),
},
}
export default defineConfig([
coreConfig,
nodeConfig,
reactNativeConfig,
browserConfig,
iifeConfig,
])