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

fix: use custom Rollup plugin for import.meta.url #77

Merged
merged 2 commits into from Apr 21, 2022

Conversation

mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Apr 13, 2022

Refs eslint/eslint#15766, this should fix the issue.

dist/eslintrc.cjs diff (line 2383):

- const require$1 = Module.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('eslintrc.cjs', document.baseURI).href)));
+ const require$1 = Module.createRequire(require('url').pathToFileURL(__filename).toString());

The new version does not include code for browsers (typeof document ...), I'm not sure why Rollup's default transformation includes it when the output format is set to "cjs".

Without this change, the added test case fails with the same error as in eslint/eslint#15766.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks ok to me, but wondering:

  1. wouldn’t it be better to file an issue with Rollup?
  2. Will this break usage in browsers? (Linter references this package)

rollup.config.js Outdated
name: "custom-import-meta-url",
resolveImportMeta(property) {
if (property === "url") {
return "require('url').pathToFileURL(__filename)";
Copy link
Member

@fasttime fasttime Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note: pathToFileURL returns a URL object, while import.meta.url is a string. This is not a problem as long as import.meta.url is only used for createRequire as it is here the case. But if we accept that, then we could also just return "__filename", because createRequire is perfectly fine with a path as argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, import.meta.url should be transformed to a URL string. I have applied @aladdin-add's suggestion to fix this by using URL#toString(). Thanks!

@aladdin-add
Copy link
Member

aladdin-add commented Apr 13, 2022

I've also noticed the issue when moving eslint to esm. there is a babel plugin doing the same thing - babel-plugin-transform-import-meta. wondering if we want to use that?

rollup.config.js Outdated Show resolved Hide resolved
Co-authored-by: 唯然 <weiran.zsd@outlook.com>
@mdjermanovic
Copy link
Member Author

I've also noticed the issue when moving eslint to esm. there is a babel plugin doing the same thing - babel-plugin-transform-import-meta. wondering if we want to use that?

It's probably more robust than this text transformation, but I'm not sure if it warrants adding babel to the build? If there's a case where the babel plugin handles code better than this, I'd guess it would have to be a very edge case.

@fasttime
Copy link
Member

@nzakas AFAICT Linter uses dist/eslintrc-universal.cjs, while this change would only affect the Node.js bundle dist/eslintrc.cjs, so if I'm not missing anything, at least for Linter, it should not make any difference. Reporting the issue to Rollup is a good idea and I was thinking to do that. For the time being, this PR still seems useful to fix the issue.

Copy link
Contributor

@snitin315 snitin315 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mdjermanovic
Copy link
Member Author

The change looks ok to me, but wondering:

  1. wouldn’t it be better to file an issue with Rollup?

I've filed rollup/rollup#4461 now, waiting for response.

  1. Will this break usage in browsers? (Linter references this package)

I believe not, as this particular module that uses import.meta.url (config-array-factory.js) is not used by Linter, neither directly nor indirectly. As @fasttime mentioned, Linter uses dist/eslintrc-universal.cjs bundle that doesn't include this code, and the custom transformation is applied only to dist/eslintrc.cjs

@nzakas
Copy link
Member

nzakas commented Apr 14, 2022

Thanks @fasttime for all of your help on this.

@mdjermanovic up to you to either merge this or wait for a Rollup response.

@mdjermanovic
Copy link
Member Author

All checks in eslint repo (eslint/eslint#15795) are passing with this branch.

Since there is still no response on rollup/rollup#4461, I'm going to merge this now. If and when Rollup fixes this issue, we can revert this change.

@mdjermanovic mdjermanovic merged commit 18b15ac into main Apr 21, 2022
@mdjermanovic mdjermanovic deleted the rollup-import-meta-url branch April 21, 2022 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants