Skip to content

Commit 5673072

Browse files
committed
feat: update module entry points to use .cdn.mjs for browser compatibility
1 parent 4fbd963 commit 5673072

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</body>
1818
<!-- <script src="./dist/index.umd.js"></script> -->
1919
<script type="module">
20-
import { instance, ModuleTSX } from "./dist/index.mjs"; // the ESM module
20+
import { instance, ModuleTSX } from "./dist/index.cdn.mjs"; // the ESM module
2121
// const { instance, ModuleTSX } = window.ModuleTSX; // the UMD global variable
2222
instance.addEventListener("*", (event) => {
2323
const { type, payload } = event.detail;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dist"
77
],
88
"main": "dist/index.mjs",
9+
"browser": "dist/index.cdn.mjs",
910
"types": "dist/index.d.ts",
1011
"scripts": {
1112
"build": "tsdown",

tsdown.config.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { defineConfig, type UserConfig } from "tsdown";
22
import * as esbuild from "esbuild";
3+
import fs from "node:fs";
4+
5+
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
6+
const dependencies = pkg.dependencies || {};
37

48
const esbuildTypescript = await esbuild.build({
59
entryPoints: ["node_modules/typescript/lib/typescript.js"],
@@ -30,7 +34,45 @@ const bundleTypescriptPlugin: UserConfig["plugins"] = {
3034
},
3135
};
3236

37+
const esmShPlugin: UserConfig["plugins"] = {
38+
name: "rewrite-to-esm-sh",
39+
resolveId: {
40+
order: "pre",
41+
handler(id: string) {
42+
if (!id.startsWith(".") && !id.startsWith("/") && !id.startsWith("virtual:") && !id.startsWith("\0")) {
43+
const parts = id.split("/");
44+
const packageName = id.startsWith("@") ? `${parts[0]}/${parts[1]}` : parts[0];
45+
const subpath = id.slice(packageName.length);
46+
47+
if (dependencies[packageName]) {
48+
let version = dependencies[packageName];
49+
try {
50+
const pkgJson = JSON.parse(fs.readFileSync(`./node_modules/${packageName}/package.json`, "utf-8"));
51+
if (pkgJson.version) version = pkgJson.version;
52+
} catch {
53+
version = version.replace(/^[\^~]/, "");
54+
}
55+
return { id: `https://esm.sh/${packageName}@${version}${subpath}`, external: true };
56+
}
57+
return { id: `https://esm.sh/${id}`, external: true };
58+
}
59+
},
60+
},
61+
};
62+
3363
export default defineConfig([
64+
// index.cdn.js — ESM, unbundled, external deps rewritten to esm.sh
65+
{
66+
dts: false,
67+
entry: { index: "./src/index.ts" },
68+
format: "esm",
69+
platform: "browser",
70+
target: ["esnext"],
71+
plugins: esmShPlugin,
72+
outputOptions: {
73+
entryFileNames: "[name].cdn.mjs",
74+
},
75+
},
3476
// index.js — ESM, unbundled (external deps)
3577
{
3678
dts: true,

0 commit comments

Comments
 (0)