Skip to content

Commit

Permalink
support cjs
Browse files Browse the repository at this point in the history
note: esbuild doesn't have a config like most tooling.  They suggest
instead to use their api.  See
evanw/esbuild#952 (comment)
  • Loading branch information
olsonpm committed Dec 1, 2023
1 parent 4a1cd0b commit d899e47
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pnpm-lock.yaml

output.json
deno.lock

bundle.cjs
bundle.cjs.map
20 changes: 20 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { build } from 'esbuild'
import fs from 'fs'

const { dependencies } = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
// we need esbuild to process esm dependencies while leaving cjs compatible ones
// out of the bundle
const esmDependencies = new Set(['bellajs'])
const externalDeps = Object.keys(dependencies)
.filter(dep => !esmDependencies.has(dep))

build({
entryPoints: ['./src/main.js'],
bundle: true,
platform: 'node',
target: 'node16',
outfile: 'bundle.cjs',
minify: true,
sourcemap: true,
external: externalDeps,
})
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"author": "@extractus",
"main": "./src/main.js",
"type": "module",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./src/main.js",
"require": "./bundle.cjs",
"default": "./src/main.js"
}
},
"imports": {
"cross-fetch": "./src/deno/cross-fetch.js"
},
Expand All @@ -23,6 +31,7 @@
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"build": "node build",
"pretest": "npm run lint",
"test": "NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest --verbose --coverage=true",
"eval": "node eval",
Expand All @@ -35,6 +44,7 @@
"html-entities": "^2.4.0"
},
"devDependencies": {
"esbuild": "^0.19.8",
"eslint": "^8.53.0",
"https-proxy-agent": "^7.0.2",
"jest": "^29.7.0",
Expand Down

0 comments on commit d899e47

Please sign in to comment.