-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathtsup.config.ts
More file actions
53 lines (51 loc) · 1.99 KB
/
tsup.config.ts
File metadata and controls
53 lines (51 loc) · 1.99 KB
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
import { readFileSync } from 'fs';
import { join } from 'path';
import type { Options } from 'tsup';
const pkg = JSON.parse(readFileSync(join(process.cwd(), 'package.json'), 'utf-8'));
const defaultOptions: Options = {
entry: ['src/**/*.ts'],
format: ['cjs', 'esm'],
splitting: false,
sourcemap: true,
// for the type maps to work, we use tsc's declaration-only command on the success callback
dts: false,
clean: true,
target: 'node16',
bundle: false,
shims: true,
define: {
__PACKAGE_NAME__: JSON.stringify(pkg.name),
__PACKAGE_VERSION__: JSON.stringify(pkg.version),
},
esbuildOptions: (options, context) => {
if (context.format === 'esm') {
options.packages = 'external';
}
},
plugins: [
{
// https://github.com/egoist/tsup/issues/953#issuecomment-2294998890
// ensuring that all local requires/imports in `.cjs` files import from `.cjs` files.
// require('./path') → require('./path.cjs') in `.cjs` files
// require('../path') → require('../path.cjs') in `.cjs` files
// from './path' → from './path.cjs' in `.cjs` files
// from '../path' → from '../path.cjs' in `.cjs` files
// (0, import_node_child_process.fork)(new URL("./path.js" → (0, import_node_child_process.fork)(new URL("./path.cjs" in `.cjs` files
name: 'fix-cjs-imports',
renderChunk(code) {
if (this.format === 'cjs') {
const regexCjs = /require\((?<quote>['"])(?<import>\.[^'"]+)\.js['"]\)/g;
const regexDynamic = /import\((?<quote>['"])(?<import>\.[^'"]+)\.js['"]\)/g;
const regexEsm = /from(?<space>[\s]*)(?<quote>['"])(?<import>\.[^'"]+)\.js['"]/g;
return {
code: code
.replace(regexCjs, 'require($<quote>$<import>.cjs$<quote>)')
.replace(regexDynamic, 'import($<quote>$<import>.cjs$<quote>)')
.replace(regexEsm, 'from$<space>$<quote>$<import>.cjs$<quote>'),
};
}
},
},
],
};
export default defaultOptions;