-
-
Notifications
You must be signed in to change notification settings - Fork 688
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
Comments
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! |
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.) |
Which is what you asked about, so yes, I think that's the workaround! :) |
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?
… On Jan 10, 2022, at 3:21 PM, David Anson ***@***.***> wrote:
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.)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.
|
Docs: https://nodejs.org/api/esm.html#import-expressions Example: 05b9e6e |
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. |
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.
… On Jan 10, 2022, at 3:41 PM, David Anson ***@***.***> wrote:
Docs: https://nodejs.org/api/esm.html#import-expressions
Example: 05b9e6e
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.
|
Not an import statement, an import expression. Check the docs I link to again - I think this should work. |
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 |
I was also having trouble with the async so this is a great example, I will give it a try. Thank you!
… On Jan 12, 2022, at 2:13 AM, David Anson ***@***.***> wrote:
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
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.
|
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.
… On Jan 12, 2022, at 8:36 AM, Patrick Landers ***@***.***> wrote:
I was also having trouble with the async so this is a great example, I will give it a try. Thank you!
>> On Jan 12, 2022, at 2:13 AM, David Anson ***@***.***> wrote:
>>
>
> 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
>
> —
> Reply to this email directly, view it on GitHub, or unsubscribe.
> Triage notifications on the go with GitHub Mobile for iOS or Android.
> You are receiving this because you authored the thread.
|
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. |
Ah, right. Okay, I’ll keep an eye out for that. Thank you for all the help.
… On Jan 12, 2022, at 1:05 PM, David Anson ***@***.***> wrote:
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.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.
|
I think we got this sorted out and the corresponding releases should all be out. |
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?
The text was updated successfully, but these errors were encountered: