Skip to content

Commit

Permalink
fix: compatibility with commonjs
Browse files Browse the repository at this point in the history
  • Loading branch information
davej committed Nov 5, 2024
1 parent 96a25e5 commit 821f18c
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,28 @@ export default defineConfig({
format: ["esm", "cjs"],
outDir: "lib",
sourcemap: true,
plugins: [
{
// https://github.com/egoist/tsup/issues/953#issuecomment-2294998890
// ensuring that all local requires/imports in `.cjs` files import from `.cjs` files.
// require('./path') → require('./path.cjs') in `.cjs` files
// require('../path') → require('../path.cjs') in `.cjs` files
// from './path' → from './path.cjs' in `.cjs` files
// from '../path' → from '../path.cjs' in `.cjs` files
name: "fix-cjs-imports",
renderChunk(code) {
if (this.format === "cjs") {
const regexCjs =
/require\((?<quote>['"])(?<import>\.[^'"]+)\.js['"]\)/g;
const regexEsm =
/from(?<space>[\s]*)(?<quote>['"])(?<import>\.[^'"]+)\.js['"]/g;
return {
code: code
.replace(regexCjs, "require($<quote>$<import>.cjs$<quote>)")
.replace(regexEsm, "from$<space>$<quote>$<import>.cjs$<quote>"),
};
}
},
},
],
});

0 comments on commit 821f18c

Please sign in to comment.