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

a script tag referencing node_modules does not get browserHash injected causing double-instantiation #9828

Open
7 tasks done
Rich-Harris opened this issue Aug 24, 2022 · 3 comments · Fixed by #9848
Open
7 tasks done
Labels
feat: deps optimizer Esbuild Dependencies Optimization feat: html has workaround p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@Rich-Harris
Copy link
Contributor

Describe the bug

I'm not sure if this is a bug or expected behaviour, but it's left me scratching my head and I'd love to know if there's a workaround at the very least.

When a module is imported via a project-relative URL (/node_modules/foo/index.js) and via a package name (foo), Vite resolves the latter to /node_modules/foo/index.js?v=xyz123, which causes the module to be instantiated twice. (In this example, optimizeDeps.exclude = ['foo'].)

This is causing headaches for SvelteKit users, as it causes failed instanceof checks. It doesn't happen when the package in question is installed outside the directory (e.g. in a workspace root), because in that case both references are resolved to /@fs/path/to/project/node_modules/foo/index.js.

Reproduction

https://github.com/Rich-Harris/vite-browserhash-repro

System Info

System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 178.69 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.15.1/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
  Browsers:
    Chrome: 104.0.5112.101
    Chrome Canary: 107.0.5258.0
    Firefox: 103.0.2
    Safari: 15.6.1
  npmPackages:
    vite: ^3.0.9 => 3.0.9

Used Package Manager

pnpm

Logs

No response

Validations

@benmccann
Copy link
Collaborator

Related issue, we may have a regression here #9661. We need to hash files (with ?v= to be able to reload them after they have been re-optimized.

For the example that Rich added, if 'foo' is in optimizeDeps exclude, it may still import other dependencies (that have been optimized) so we still need to have a ?v= on these excluded files.

@patak-dev
Copy link
Member

I'm not sure if this is a bug or expected behaviour

It is a bug, for reference, I added an explanation of why we need version queries in direct node_modules imports in the description of:

@patak-dev patak-dev reopened this Sep 6, 2022
@sapphi-red sapphi-red changed the title Unnecessary (?) browserHash causes double-instantiation a script tag referencing node_modules does not get browserHash injected causing double-instantiation Mar 5, 2024
@sapphi-red sapphi-red added feat: html p2-edge-case Bug, but has workaround or limited in scope (priority) feat: deps optimizer Esbuild Dependencies Optimization has workaround and removed p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) labels Mar 5, 2024
@sapphi-red
Copy link
Member

Sveltekit was fixed by #9848 but the repro in the OP is not fixed. This is happening because we are not injecting browserHash to script tags referencing a file in node_modules. (https://discord.com/channels/804011606160703521/804439875226173480/1172385782007353344)

A workaround is to use <script type="module">import 'path/to/node_module/file'</script> instead of <script type="module" src="path/to/node_module/file"></script>.

I guess this would also be fixed if we make bare specifiers work in HTML in dev (#12260). Though I'm not sure if we are going to support that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: deps optimizer Esbuild Dependencies Optimization feat: html has workaround p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants