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

Dynamically inserted importmap doesn't work in development mode #15192

Open
sorin-davidoi opened this issue Nov 30, 2023 · 2 comments
Open

Dynamically inserted importmap doesn't work in development mode #15192

sorin-davidoi opened this issue Nov 30, 2023 · 2 comments
Labels
feat: html has workaround p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@sorin-davidoi
Copy link

Describe the bug

Inserting an importmap at runtime via a script statement in head does not work in development mode because the script that is inserting it is placed after the module import of @vite/client. This results in the following error:

An import map is added after module script load was triggered.

This issue only affects the development mode because it it caused by the presence of the @vite/client import.

Related issues:

Workaround: trick Vite into thinking that the script that is inserting the importmap is an importmap itself - this will make sure that it gets sorted before the module import of @vite/client. This can be achieved by adding an attribute to it that matches this regular expression (e.g. data-vite-workaround="type=importmap").

Reproduction

https://stackblitz.com/edit/vitejs-vite-yde4az?file=index.html

Steps to reproduce

Run npm install && npm run dev.

Expected result:

  • The page shows the text "Hello World!"

Actual result:

  • The page shows the text "Waiting for script..."

Note that running npm install && npm run build && npm run preview works as expected.

System Info

Output from running the command in the linked StackBlitz:


System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.10.5 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^5.0.2 => 5.0.4


### Used Package Manager

npm

### Logs

_No response_

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitejs/vite/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitejs.dev/guide).
- [X] Check that there isn't [already an issue](https://github.com/vitejs/vite/issues) that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to [vuejs/core](https://github.com/vuejs/core) instead.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitejs/vite/discussions) or join our [Discord Chat Server](https://chat.vitejs.dev/).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
Copy link

stackblitz bot commented Nov 30, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@HexaField
Copy link
Contributor

@sorin-davidoi Would you be able to provide some more information about how to work around this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: html has workaround p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

3 participants