mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2026-04-25 17:26:22 +03:00
[GH-ISSUE #477] Using ES modules in custom rules #391
Labels
No labels
bug
enhancement
enhancement
enhancement
fixed in next
fixed in next
fixed in next
new rule
new rule
new rule
pull-request
question
refactoring
refactoring
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/markdownlint#391
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @pxl9588 on GitHub (Jan 10, 2022).
Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/477
Is it possible to use ES module in custom rules? I’m running into this error when I use
const urlExist = require(‘url-exist’);:When I use
import urlExist from ‘url-exist’I getWhen I change the custom rule to a .mjs I get:
I believe that happens because the rules are imported by markdowlint by
require.Is there a work around for this?
@DavidAnson commented on GitHub (Jan 10, 2022):
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 commented on GitHub (Jan 10, 2022):
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 commented on GitHub (Jan 10, 2022):
Which is what you asked about, so yes, I think that's the workaround! :)
@pxl9588 commented on GitHub (Jan 10, 2022):
Sorry I’m advance,I’m not very familiar with this.
As far as I understand I can’t use import in a CommonJS file because it needs to be a module. So right now I don’t have a way to use the url-exist package. Did you mean there currently is a workaround or that it will be available in future releases?
@DavidAnson commented on GitHub (Jan 10, 2022):
Docs: https://nodejs.org/api/esm.html#import-expressions
Example:
github.com/DavidAnson/markdownlint@05b9e6e43c@DavidAnson commented on GitHub (Jan 10, 2022):
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 commented on GitHub (Jan 10, 2022):
Ah right so the error isn’t about it being CommonJS, it’s about it being a module.
Which brings back the original problem,
“Cannot use import statement outside a module”. Once it’s a module markdownlint can’t ‘require’ the custom rules.cjs any more.
@DavidAnson commented on GitHub (Jan 10, 2022):
Not an import statement, an import expression. Check the docs I link to again - I think this should work.
@DavidAnson commented on GitHub (Jan 12, 2022):
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 commented on GitHub (Jan 12, 2022):
I was also having trouble with the async so this is a great example, I will give it a try. Thank you!
@pxl9588 commented on GitHub (Jan 12, 2022):
Is there something I have to enable to allow async custom rules? I followed your example but I don’t see any of the on errors printing.
@DavidAnson commented on GitHub (Jan 12, 2022):
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 commented on GitHub (Jan 12, 2022):
Ah, right. Okay, I’ll keep an eye out for that. Thank you for all the help.
@DavidAnson commented on GitHub (Apr 11, 2022):
I think we got this sorted out and the corresponding releases should all be out.