-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsdown.config.ts
More file actions
74 lines (71 loc) · 2.11 KB
/
tsdown.config.ts
File metadata and controls
74 lines (71 loc) · 2.11 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
import { defineConfig, type UserConfig } from "tsdown";
import * as esbuild from "esbuild";
const esbuildTypescript = await esbuild.build({
entryPoints: ["node_modules/typescript/lib/typescript.js"],
bundle: true,
format: "esm",
platform: "browser",
minify: true,
write: false,
});
/**
* Rolldown cannot bundle TypeScript directly due to its use of Node.js-specific features and dynamic imports.
* To work around this, we pre-bundle TypeScript using esbuild, which can handle these features and produce a browser-compatible ESM bundle.
* We then create a custom plugin for tsdown that serves this pre-bundled TypeScript whenever the "typescript" module is imported.
*/
const bundleTypescriptPlugin: UserConfig["plugins"] = {
name: "bundle-typescript-with-esbuild",
resolveId: {
order: "pre",
handler(id: string) {
if (id === "typescript") return "virtual:typescript-esm";
},
},
async load(id: string) {
if (id === "virtual:typescript-esm") {
return esbuildTypescript.outputFiles[0].text;
}
},
};
export default defineConfig([
// index.js — ESM, unbundled (external deps)
{
dts: true,
entry: { index: "./src/index.ts" },
format: "esm",
platform: "browser",
target: ["esnext"],
outputOptions: {
entryFileNames: "[name].js",
},
},
// index.mjs — ESM, bundled (typescript bundled via esbuild for browser compatibility)
{
dts: false,
entry: { index: "./src/index.ts" },
format: "esm",
platform: "browser",
target: ["esnext"],
deps: { alwaysBundle: (id) => id !== "typescript", onlyBundle: false },
minify: true,
plugins: bundleTypescriptPlugin,
outputOptions: {
entryFileNames: "[name].mjs",
},
},
// index.umd.js — UMD, bundled + minified
{
dts: false,
entry: { index: "./src/index.ts" },
format: "umd",
platform: "browser",
target: ["esnext"],
deps: { alwaysBundle: (id) => id !== "typescript", onlyBundle: false },
minify: true,
plugins: bundleTypescriptPlugin,
outputOptions: {
name: "ModuleTSX",
entryFileNames: "[name].umd.js",
},
},
]);