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

Using ES modules in custom rules #477

Closed
pxl9588 opened this issue Jan 10, 2022 · 14 comments
Closed

Using ES modules in custom rules #477

pxl9588 opened this issue Jan 10, 2022 · 14 comments

Comments

@pxl9588
Copy link

pxl9588 commented Jan 10, 2022

Is it possible to use ES module in custom rules? I’m running into this error when I use const urlExist = require(‘url-exist’);:

Must use import to load ES module

When I use import urlExist from ‘url-exist’ I get

Cannot use import statement outside of a module

When I change the custom rule to a .mjs I get:

Property ‘names’ of custom rule at index 0 is incorrect

I believe that happens because the rules are imported by markdowlint by require.

Is there a work around for this?

@DavidAnson
Copy link
Owner

This is not supported currently for the reasons you outline. It may be as simple as changing from require to import, but my other experiences incorporating ESM suggest otherwise. I'll have a look at this for the next round of updates, thanks!

@DavidAnson
Copy link
Owner

To clarify, while your rule needs to be CommonJS, it should be able to use an import expression to pull in an ESM dependency. Because that is an asynchronous operation, it would need to take advantage of the new support for asynchronous custom rules. (Which is only available in the library for now; I am rolling it out to CLI2 and the VS Code extension soon.)

@DavidAnson
Copy link
Owner

Which is what you asked about, so yes, I think that's the workaround! :)

@pxl9588
Copy link
Author

pxl9588 commented Jan 10, 2022 via email

@DavidAnson
Copy link
Owner

@DavidAnson
Copy link
Owner

That should work today. And since you are planning to use an asynchronous function (network access), you already needed the new support for asynchronous custom rules.

@pxl9588
Copy link
Author

pxl9588 commented Jan 10, 2022 via email

@DavidAnson
Copy link
Owner

Not an import statement, an import expression. Check the docs I link to again - I think this should work.

@DavidAnson
Copy link
Owner

I wrote a simple custom rule that imports an ESM module to make sure this works like I claim: https://github.com/DavidAnson/markdownlint/blob/next/test/rules/validate-json.js

@pxl9588
Copy link
Author

pxl9588 commented Jan 12, 2022 via email

@pxl9588
Copy link
Author

pxl9588 commented Jan 12, 2022 via email

@DavidAnson
Copy link
Owner

I mentioned above that asynchronous support is only released for the library so far. CLI2 should be in a few days and the VS Code extension a week or two after that.

@pxl9588
Copy link
Author

pxl9588 commented Jan 12, 2022 via email

@DavidAnson
Copy link
Owner

I think we got this sorted out and the corresponding releases should all be out.

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

No branches or pull requests

2 participants