Skip to content

Commit 3c153a5

Browse files
authored
Merge pull request #24 from mariczne/main
Fix CJS compatibility (use .cjs file extensions)
2 parents 2d45bc0 + 36e0678 commit 3c153a5

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

build.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@ import { build } from "esbuild"
1616
import type { Plugin, PluginBuild, BuildOptions } from "esbuild"
1717
import glob from "glob"
1818

19+
// Plugin to fix CommonJS imports by appending .cjs to require statements
20+
const fixCjsImportsPlugin = (): Plugin => ({
21+
name: "fix-cjs-imports",
22+
setup(build: PluginBuild) {
23+
// Run in the onEnd hook after all files have been written
24+
build.onEnd((result) => {
25+
// Only proceed if the build is successful
26+
if (result.errors.length === 0) {
27+
// Get the output directory from the build options
28+
const outdir = build.initialOptions.outdir
29+
if (!outdir) return
30+
31+
// Find all .cjs files in the output directory
32+
const files = glob.sync(`${outdir}/**/*.cjs`)
33+
34+
files.forEach((file) => {
35+
let content = fs.readFileSync(file, "utf8")
36+
37+
// Replace all require('./something') with require('./something.cjs')
38+
content = content.replace(/require\(["'](\.[^"']+)["']\)/g, (match, importPath) => {
39+
// Don't add .cjs if it already has an extension
40+
if (path.extname(importPath) !== "") {
41+
return match
42+
}
43+
return `require('${importPath}.cjs')`
44+
})
45+
46+
fs.writeFileSync(file, content)
47+
})
48+
}
49+
})
50+
}
51+
})
52+
1953
const entryPoints = glob.sync("./src/**/*.ts", {
2054
ignore: ["./src/**/*.test.ts", "./src/mod.ts", "./src/middleware.ts", "./src/deno/**/*.ts"]
2155
})
@@ -65,6 +99,8 @@ const cjsBuild = () =>
6599
outbase: "./src",
66100
outdir: "./dist/cjs",
67101
format: "cjs",
102+
outExtension: { ".js": ".cjs" },
103+
plugins: [fixCjsImportsPlugin()],
68104
tsconfig: "tsconfig.cjs.json"
69105
})
70106

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
"name": "might-fail",
33
"version": "0.7.3",
44
"description": "A better way to handle errors in JavaScript and TypeScript. Handle async and sync errors without `try` and `catch` blocks.",
5-
"main": "dist/cjs/index.js",
5+
"main": "dist/cjs/index.cjs",
66
"module": "dist/index.js",
77
"types": "dist/index.d.ts",
88
"type": "module",
99
"exports": {
1010
".": {
1111
"import": "./dist/index.js",
12-
"require": "./dist/cjs/index.js",
12+
"require": "./dist/cjs/index.cjs",
1313
"types": "./dist/index.d.ts"
1414
},
1515
"./go": {
1616
"import": "./dist/go/index.js",
17-
"require": "./dist/cjs/go/index.js",
17+
"require": "./dist/cjs/go/index.cjs",
1818
"types": "./dist/go/index.d.ts"
1919
}
2020
},
@@ -26,6 +26,7 @@
2626
},
2727
"files": [
2828
"dist/**/*.js",
29+
"dist/**/*.cjs",
2930
"dist/**/*.d.ts",
3031
"dist/**/*.ts"
3132
],
@@ -61,4 +62,3 @@
6162
"vitest": "^2.1.1"
6263
}
6364
}
64-

0 commit comments

Comments
 (0)