Repository navigation
Expand file tree
/
Copy pathtsdown.config.ts
More file actions
124 lines (114 loc) · 3.71 KB
/
Copy pathtsdown.config.ts
File metadata and controls
124 lines (114 loc) · 3.71 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
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
import { createRequire } from 'node:module';
import path from 'node:path';
import type { Plugin } from 'rolldown';
import { transformSync } from 'rolldown/experimental';
import { defineConfig } from 'tsdown';
const packageJson = createRequire(import.meta.url)('./package.json') as {
exports: Record<string, string>;
};
const SERVER_ENTRY = './src/server/index.ts';
// Per-function subpaths ("./array/*") are wildcards, not entries: the unbundled build
// already emits one file per module reached from the category entry.
const allEntrypoints = Object.values(packageJson.exports).filter(
(f): f is string => typeof f === 'string' && /^(\.\/)?src\//.test(f) && f.endsWith('.ts') && !f.includes('*')
);
// "./util/hash" is a conditional export (node/default), so its entries are not
// collected above and are wired explicitly: the browser implementation builds
// as a neutral entry, and the node implementation gets its own node-platform
// build below.
const neutralEntrypoints = [...allEntrypoints.filter(f => f !== SERVER_ENTRY), './src/util/hash/browser.ts'];
// Published code is lowered to ES2019 syntax, so that `?.` and `??` can be used freely
// in the source while `es-toolkit/compat` still loads on ES2015 engines such as Node.js 6.
// Only the modules `es-toolkit/compat` reaches are lowered; the rest is published as written.
// TODO: lower only the syntax compat needs once Oxc supports per-feature lowering
// (https://github.com/oxc-project/oxc/issues/15373).
function lowerCompatToES2019(): Plugin {
const compatModules = new Set<string>();
return {
name: 'lower-compat-to-es2019',
// 1. Once the module graph is complete, collect every module the compat entry reaches.
buildEnd() {
const queue = [path.resolve(packageJson.exports['./compat'])];
for (const id of queue) {
if (!compatModules.has(id)) {
compatModules.add(id);
queue.push(...(this.getModuleInfo(id)?.importedIds ?? []));
}
}
},
// 2. Lower only the output files that come from those modules.
renderChunk(code, chunk) {
if (!chunk.moduleIds.some(id => compatModules.has(id))) {
return null;
}
return transformSync(chunk.fileName, code, { target: 'es2019' }).code;
},
};
}
export default defineConfig([
{
entry: neutralEntrypoints,
format: ['esm', 'cjs'],
outDir: 'dist',
platform: 'neutral',
plugins: [lowerCompatToES2019()],
unbundle: true,
fixedExtension: false,
dts: true,
sourcemap: false,
treeshake: false,
clean: true,
exports: false,
attw: false,
publint: false,
},
{
entry: [SERVER_ENTRY],
format: ['esm', 'cjs'],
outDir: 'dist/server',
platform: 'node',
unbundle: true,
fixedExtension: false,
dts: true,
sourcemap: false,
treeshake: false,
clean: false,
exports: false,
attw: false,
publint: false,
},
{
// The node implementation of `es-toolkit/util/hash` imports `node:crypto`,
// so it cannot be part of the neutral build. It is bundled (not unbundled)
// so that its `serialize` dependency does not escape the outDir.
entry: ['./src/util/hash/node.ts'],
format: ['esm', 'cjs'],
outDir: 'dist/util/hash',
platform: 'node',
fixedExtension: false,
dts: true,
sourcemap: false,
treeshake: true,
clean: false,
exports: false,
attw: false,
publint: false,
},
{
entry: { 'browser.global': './src/browser.ts' },
format: 'umd',
outDir: 'dist',
platform: 'browser',
globalName: '_',
minify: true,
sourcemap: false,
dts: false,
clean: false,
exports: false,
attw: false,
publint: false,
outputOptions: {
entryFileNames: '[name].js',
},
},
]);