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

relative url resolving in JS via toOutputFilePathInString may introduce the need for the module param in SystemJS #9807

Closed
6 of 7 tasks
Tal500 opened this issue Aug 23, 2022 · 0 comments · Fixed by #9821
Closed
6 of 7 tasks
Labels
p4-important Violate documented behavior or significantly improves performance (priority) plugin: legacy

Comments

@Tal500
Copy link
Contributor

Tal500 commented Aug 23, 2022

Describe the bug

Background

While working on SvelteKit legacy feature (sveltejs/kit#12), I had encountered an issue on Svelte CSS resolving of relative URL parameters with the Vite legacy plugin.
After a lot of digging, it turned out that the problem I had wasn't related to Svelte, nor to Vite legacy plugin nor to even Babel - but to the internal Vite relative URL resolving in JS output code, when rendering SystemJS chunks.
I have gave this unrelated background so you folks will hopefully forgive me I'm not giving a complicated reproduction example(with too many forks), but rather prove to you by the code structure and notes what and why the problem exists.
I have modified Vite source code to fix the problem I'll describe next, and then my issues with SvelteKit legacy build with the relative URL were also fixed. Will send PR soon.

Problem

The function toOutputFilePathInString takes a path, and returns a JS code representation that computes the path (the computation is needed if the path is relative).
Following the default parameter, you'll see it uses internally relativeUrlMechanisms, and notice the following suspicious line:

// NOTE: make sure rollup generate `module` params
system: (relativePath) => getResolveUrl(`'${relativePath}', module.meta.url`),

Turns out that this code comment wasn't handled at all. It means is that:

Failing Case Story

When rendering chunks in SystemJS format, you might start with a code that looks like:

System.register([], function () {
    'use strict';
    // ...
}

but you may ends up using (internally) relativeUrlMechanisms, which may use the definition of the missing module parameter.
That means, the new code should include the module parameter and be in the form of:

System.register([], function (exports, module) {
    'use strict';
    // ...
}

Well, this "edge-case" was common in my use-case (introduced above).
This mean that I get in the legacy browser an error of "module" is not defined, since Vite internal code uses toOutputFilePathInString to transform relative URLs to JS code, which resolved to be in the following shape, which uses the missing parameter 'module':

system: (relativePath) => getResolveUrl(`'${relativePath}', module.meta.url`),

Solution

Need to introduce a property in toOutputFilePathInString, that tells whether the definition for the "module" is needed for the JS expression.
We need to propagate this property all over until we return to the rendering chunk function (in the internal plugins that use input paths in code). Then, on the final rendering step we need to make sure that the final code have the "module" definition (if not, replace the function deceleration to be in the signature function (exports, module)).

As said above, I'll post a PR that do exactly this.

Reproduction

no need to, I proved the problem by the code structure

System Info

N/A

Used Package Manager

pnpm

Logs

No response

Validations

Tal500 added a commit to Tal500/vite that referenced this issue Aug 23, 2022
@sapphi-red sapphi-red added plugin: legacy p3-minor-bug An edge case that only affects very specific usage (priority) labels Aug 24, 2022
@sapphi-red sapphi-red added p4-important Violate documented behavior or significantly improves performance (priority) and removed p3-minor-bug An edge case that only affects very specific usage (priority) labels Aug 24, 2022
Tal500 added a commit to Tal500/vite that referenced this issue Aug 24, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Sep 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority) plugin: legacy
Projects
None yet
2 participants