A POC to recreate the ESBuild bug related to ESLint (CJS) plugin workflow.
There are two packages:
esm-dep
— ES modules package which exports functionmult()
which multiplies two numberscjs-main
— the CJS package which consumes the aforementionedesm-dep
. It's built to CJS target using esbuild's settings:platform=node, format=cjs, bundle=true, target=esnext
.
This setup mimics the ESLint plugins — they are in CJS, and the exported default plain object contains the methods. The problem is that while esbuild
output exports an object, its methods become inaccessible.
mkdir deleteme && cd deleteme
git clone git@github.com:revelt/esbuild-bug.git
cd esbuild-bug
npm i
cd esm-dep
npm run build
— it will build into../cjs-main/node_modules/esm-dep
mimicking a real npm package. In real life, it would be an ESLint plugin. Notice we deliberately are not git-ignoring the folder./cjs-main/node-modules/*
— it's mimicking the case whereesm-dep
would be a real npm package.cd ..
cd cjs-main
npm run build
npm run test
At this moment, the test will fail, complaining about:
TypeError: tryme is not a function
You can fix that by manually editing ./cjs-main/dist/cjs-main.cjs.js
- delete the line:
module.exports = __toCommonJS(main_exports);
Then append a new line at the bottom of the file:
module.exports = __toCommonJS(main_default);
Now, if you rerun tests inside cjs-main
folder, npm run test
, they will pass.
Practically, this bug forces us to hard-patch the esbuild's CJS output to make the default exports work, specifically those of ESLint plugins.
- Real ESLint plugin: https://github.com/codsen/codsen/tree/main/packages/eslint-plugin-test-num-tbc
- ESBuild output patch script: https://github.com/codsen/codsen/blob/main/ops/scripts/fix-cjs.js