Skip to content

Commit

Permalink
build: es6 compatibiliy for commonjs bundle (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 21, 2023
1 parent afd1489 commit 625f12d
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 0 deletions.
19 changes: 19 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { defineBuildConfig } from "unbuild";
import type { Plugin, OutputChunk } from "rollup";
import { transform } from "esbuild";

export default defineBuildConfig({
declaration: true,
Expand All @@ -9,4 +11,21 @@ export default defineBuildConfig({
},
},
entries: ["src/index"],
hooks: {
"rollup:options"(ctx, rollupConfig) {
(rollupConfig.plugins as Plugin[]).push({
name: "compat",
async generateBundle(_options, bundle) {
const cjsEntry = bundle["index.cjs"] as OutputChunk;
if (!cjsEntry) {
return;
}
cjsEntry.code = await transform(cjsEntry.code, {
target: "es6",
minify: true,
}).then((r) => r.code);
},
} satisfies Plugin);
},
},
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
"@types/node": "^20.5.0",
"@vitest/coverage-v8": "^0.34.2",
"changelogen": "^0.5.4",
"esbuild": "^0.19.2",
"eslint": "^8.47.0",
"eslint-config-unjs": "^0.2.1",
"jiti": "^1.19.2",
"prettier": "^3.0.2",
"rollup": "^3.28.0",
"typescript": "^5.1.6",
"unbuild": "^2.0.0-rc.0",
"vitest": "^0.34.2"
Expand Down
Loading

0 comments on commit 625f12d

Please sign in to comment.