Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin problems in a CJS environment #12

Closed
james-yeoman opened this issue Mar 4, 2024 · 2 comments
Closed

Plugin problems in a CJS environment #12

james-yeoman opened this issue Mar 4, 2024 · 2 comments

Comments

@james-yeoman
Copy link

james-yeoman commented Mar 4, 2024

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
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
Copy link
Author

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

@robinloeffel
Copy link
Owner

Fixed in v0.3.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants