diff --git a/package.json b/package.json index b140b41b..ebf5d580 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "test": "jest --pass-with-no-tests", "test:ci": "jest --no-stack-trace --verbose --pass-with-no-tests", "prebuild": "npm run lint", - "build": "tsup --format cjs --clean && tsup --dts --format esm", + "build": "tsup", + "postbuild": "node scripts/postbuild.mjs", "lint": "eslint src --ext mjs,js,ts", "lint:fix": "eslint src --ext mjs,js,ts --fix", "format": "prettier --write **/*.{ts,js,json,yml,yaml}", diff --git a/scripts/postbuild.mjs b/scripts/postbuild.mjs new file mode 100644 index 00000000..6146e325 --- /dev/null +++ b/scripts/postbuild.mjs @@ -0,0 +1,7 @@ +import { readFile, writeFile } from 'node:fs/promises'; + +const data = await readFile('./dist/index.mjs', 'utf-8'); +await writeFile( + './dist/index.mjs', + `import{createRequire as topLevelCreateRequire}from"module";const require=topLevelCreateRequire(import.meta.url);${data}`, +); diff --git a/tsup.config.ts b/tsup.config.ts index f407b83f..7089a5cc 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -1,17 +1,12 @@ import { defineConfig } from 'tsup'; -export default defineConfig((options) => { - return { - banner: { - js: - options.format[0] === 'esm' - ? 'import{resolve as requireResolve}from"path";import{createRequire as topLevelCreateRequire}from"module";const require=topLevelCreateRequire(requireResolve(""));' - : '', - }, - entryPoints: ['src/index.ts'], - minify: true, - skipNodeModulesBundle: true, - sourcemap: true, - target: 'es2021', - }; +export default defineConfig({ + clean: true, + dts: true, + entryPoints: ['src/index.ts'], + format: ['esm', 'cjs'], + minify: true, + skipNodeModulesBundle: true, + sourcemap: true, + target: 'es2021', });