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: forward options when calling this.resolve #128

Merged
merged 1 commit into from Dec 7, 2023
Merged

fix: forward options when calling this.resolve #128

merged 1 commit into from Dec 7, 2023

Conversation

pcattori
Copy link
Contributor

@pcattori pcattori commented Dec 7, 2023

Currently, passing only skipSelf and not forwarding options causes custom to be set to undefined, but other plugins rely on the presence of custom for correctness.

For example, Remix would like to throw whenever .server modules are resolved from the client bundle. We'd like to show something like:

Server module {original ID} is being referenced in the client bundle by {importer}

We need the resolved ID to check if the actual file path contains .server, but we need the original ID for the error message. So we call this.resolve to get the resolved ID. To make sure our plugin doesn't participate in further resolution, we rely on custom to setup some custom logic. But since vite-tsconfig-paths is also used in this project and it sets options with custom as undefined implicitly, the data set in custom is lost and our custom logic never triggers.

Additionally, omitting options altogether results in this warning from @rollup/plugin-commonjs:

[plugin:commonjs--resolver] It appears a plugin has implemented a "resolveId" hook that uses "this.resolve" without forwarding the third "options" parameter of "resolveId". This is problematic as it can lead to wrong module resolutions especially for the node-resolve plugin and in certain cases cause early exit errors for the commonjs plugin.
In rare cases, this warning can appear if the same file is both imported and required from the same mixed ES/CommonJS module, in which case it can be ignored.

Since they can't reliably check that you forwarded the options, they just check that options was passed in at all, but its clear from the message that the intent is to forward options.

if (importer && !relativeImportRE.test(id) && !isAbsolute(id)) {
const viteResolve: ViteResolve = async (id, importer) =>
(await this.resolve(id, importer, { skipSelf: true }))?.id
(await this.resolve(id, importer, options))?.id
Copy link
Owner

Choose a reason for hiding this comment

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

It still needs skipSelf: true I think?

Copy link
Contributor Author

@pcattori pcattori Dec 7, 2023

Choose a reason for hiding this comment

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

From the Vite docs for this.resolve:

The default of skipSelf is true

But no harm (I think?) in setting it explicitly too if you prefer.

Copy link
Owner

Choose a reason for hiding this comment

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

Oh nice. That's a welcome change from Rollup :)

Copy link

Choose a reason for hiding this comment

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

Isn't this a breaking changes for people still using Vite 3-4?
Rollup changed the default into v4, which is used by Vite 5, thus this is a breaking change and would need a proper major release

Copy link
Owner

Choose a reason for hiding this comment

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

@IlCallo Ah good point! I think I'll explicitly set skipSelf: true for backwards compatibility.

Copy link
Owner

Choose a reason for hiding this comment

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

Fixed in v4.2.3

Copy link

Choose a reason for hiding this comment

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

Thanks 😁

@aleclarson aleclarson merged commit d496ea9 into aleclarson:master Dec 7, 2023
@aleclarson
Copy link
Owner

Released in v4.2.2

@pcattori pcattori deleted the this-resolve-forward-options branch December 7, 2023 19:56
@pcattori
Copy link
Contributor Author

pcattori commented Dec 7, 2023

@aleclarson so swift! 🙏

aleclarson added a commit that referenced this pull request Dec 28, 2023
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

Successfully merging this pull request may close these issues.

None yet

3 participants