diff --git a/package.json b/package.json index 5648063..14fede5 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "access": "public", "legacy": "lib/index.cjs", "main": "lib/index.cjs", - "browser": "lib/index.js", + "browser": "lib/index.umd.js", "module": "lib/index.js", "exports": { ".": { @@ -119,6 +119,7 @@ "@types/node": "^20.10.4", "@types/web": "^0.0.127", "esbuild": "^0.19.9", + "esbuild-plugin-umd-wrapper": "^2.0.0", "husky": "^8.0.3", "pnpm": "^8.12.0", "semantic-release": "^22.0.12", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5711216..cf1c691 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ devDependencies: esbuild: specifier: ^0.19.9 version: 0.19.9 + esbuild-plugin-umd-wrapper: + specifier: ^2.0.0 + version: 2.0.0 husky: specifier: ^8.0.3 version: 8.0.3 @@ -1396,6 +1399,10 @@ packages: is-arrayish: 0.2.1 dev: true + /esbuild-plugin-umd-wrapper@2.0.0: + resolution: {integrity: sha512-pcu2/lcm29S85VCnSJuValrQ8FqeFJs5VWEwfp7vBRsOHjxZypcxgwXjxDIxDRo17uOcENZIbgz2szjln029eQ==} + dev: true + /esbuild@0.19.9: resolution: {integrity: sha512-U9CHtKSy+EpPsEBa+/A2gMs/h3ylBC0H0KSqIg7tpztHerLi6nrrcoUJAkNCEPumx8yJ+Byic4BVwHgRbN0TBg==} engines: {node: '>=12'} diff --git a/tsup.config.ts b/tsup.config.ts index 40fb95a..93cec53 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,6 +1,12 @@ +import type { Options } from 'tsup'; import { defineConfig } from 'tsup'; +import { umdWrapper } from 'esbuild-plugin-umd-wrapper'; -export default defineConfig({ +import { umd } from "./package.json"; + +const GLOBAL_NAME = umd; + +const baseConfig: Options = { target: ["es2022", "node21", "chrome105"], entry: ['src/index.ts'], format: ["esm", "cjs"], @@ -8,4 +14,24 @@ export default defineConfig({ clean: true, dts: true, outDir: "lib", -}) \ No newline at end of file + platform: 'browser', + globalName: GLOBAL_NAME, + + outExtension({ format, options }) { + const ext = ({ "esm": "js", "cjs": "cjs", "umd": "umd.js" })[format] + const outputExtension = options.minify ? `min.${ext}` : `${ext}` + return { + js: `.${outputExtension}`, + } + }, +} + +export default defineConfig([ + { ...baseConfig }, + { + ...baseConfig, + target: 'es5', + format: ['umd'], + esbuildPlugins: [umdWrapper({ libraryName: GLOBAL_NAME, external: 'inherit' })], + }, +]) \ No newline at end of file