robinloeffel / esbuild-plugin-eslint

Lint your esbuild bundles with eslint. šŸ§
https://npmjs.com/esbuild-plugin-eslint
MIT License
4 stars 7 forks source link

Plugin problems in a CJS environment #12

Closed james-yeoman closed 4 months ago

james-yeoman commented 4 months ago

After bumping esbuild-plugin-eslint from v0.3.3 to v0.3.8 in order to use the new release from #11, I encountered the problem of importing ESM in a CJS environment (which appears to have been introduced in v0.3.4). Since this is for use with the Serverless Framework (using serverless-esbuild), I can't just move to ESM, unfortunately.

To work around this, I added https://github.com/nktnet1/import-sync in order to import this plugin. This worked great, as I got back to the error of "no eslint configuration found" from v0.3.3. However, once I bumped eslint to the v9 beta, I got the following error

image

The above error, but in a code block ``` āœ˜ [ERROR] A dynamic import callback was not specified. [plugin eslint] /home/james/projects/LabelLogicLive_Monorepo/packages/api/node_modules/eslint/lib/eslint/eslint.js:315:19: 315 ā”‚ const config = (await import(fileURL)).default; ā•µ ^ at new NodeError (node:internal/errors:393:5) at importModuleDynamicallyCallback (node:internal/process/esm_loader:39:9) at loadFlatConfigFile (/home/james/projects/LabelLogicLive_Monorepo/packages/api/node_modules/eslint/lib/eslint/eslint.js:315:20) at async calculateConfigArray (/home/james/projects/LabelLogicLive_Monorepo/packages/api/node_modules/eslint/lib/eslint/eslint.js:394:28) at async ESLint.lintFiles (/home/james/projects/LabelLogicLive_Monorepo/packages/api/node_modules/eslint/lib/eslint/eslint.js:803:25) at async /home/james/projects/LabelLogicLive_Monorepo/packages/api/node_modules/esbuild-plugin-eslint/dist/index.js:15:29 at async /home/james/projects/LabelLogicLive_Monorepo/packages/api/node_modules/esbuild/lib/main.js:1481:27 This error came from the "onEnd" callback registered here: /home/james/projects/LabelLogicLive_Monorepo/packages/api/node_modules/esbuild-plugin-eslint/dist/index.js:14:8: 14 ā”‚ onEnd(async () => { ā•µ ~~~~~ ```

This makes me wonder whether the problem is with the importing of ESM in a CJS environment, since my workaround I had while waiting on your fix for #11 was working perfectly fine without the need for any hacks. I also tried it with v9 of eslint, and it still worked perfectly fine. The problem is somewhere between importing esbuild-plugin-eslint, and the loading of eslint itself.

For reference, this is the plugin workaround code ```js const eslintPlugin = ({ filter }) => ({ name: "eslint", setup: ({ onLoad, onEnd }) => { loadESLint().then(eslintClass => { /** @type {import("esbuild").OnLoadArgs["path"][]} */ const filesToLint = []; const eslint = new eslintClass(); onLoad({ filter }, ({ path }) => { if (!path.includes("node_modules")) filesToLint.push(path); return null; }); onEnd(async () => { const results = await eslint.lintFiles(filesToLint); const formatter = await eslint.loadFormatter("stylish"); const output = await formatter.format(results); if (output.length > 0) console.log(output); const warnings = results.reduce( (count, result) => count + result.warningCount, 0 ); const errors = results.reduce( (count, result) => count + result.errorCount, 0 ); const ret = { errors: [] }; if (warnings > 0) { ret.errors.push({ text: `${warnings} warnings were found by eslint!` }); } if (errors > 0) { ret.errors.push( { text: `${errors} errors were found by eslint!` } ); } return ret; }); }); } }); ```

Would it be possible to get a release of esbuild-plugin-eslint with a cjs build alongside the esm build?

For example, the dist folder would look like

dist
|- cjs
| |- index.cjs
|- esm
| |- index.mjs
|- types
  |- index.d.ts

and the package.json would include the following

"exports": {
  "require": "dist/cjs/index.cjs",
  "import": "dist/esm/index.mjs",
  "types": "dist/types/index.d.ts"
}
james-yeoman commented 4 months ago

Can confirm: the problem is in the module loading. Replacing usage of loadESLint with new ESLint() still works

robinloeffel commented 4 months ago

Fixed in v0.3.11.