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

resolveId hook importer inconsistent in pre stage #5981

Closed
7 tasks done
DylanPiercey opened this issue Dec 6, 2021 · 2 comments · Fixed by #12911
Closed
7 tasks done

resolveId hook importer inconsistent in pre stage #5981

DylanPiercey opened this issue Dec 6, 2021 · 2 comments · Fixed by #12911
Labels
documentation Improvements or additions to documentation rollup plugin compat

Comments

@DylanPiercey
Copy link
Contributor

Describe the bug

If using the resolveId(importee, importer, opts) hook with enforce: "pre", the importer will be different in dev mode vs in build mode.

Specifically in dev mode the importer will always be the top level .html file.
For my specific use case this makes it impossible to determine what the top level scripts are without other work arounds.

I was thinking ideally I'd be able to do:

resolveId(importee, importer) {
  if (importer) {
    if (this.getModuleInfo(importer).isEntry) {
        // This tells me that the parent of importee` is the top level file, the `.html` file.
    }
  }
}

However using isEntry on getModuleInfo also only works in build mode.

Reproduction

See https://github.com/DylanPiercey/vite-issue-resolve-id-repro

System Info

System:
    OS: macOS 10.15.7
    CPU: (4) x64 Intel(R) Core(TM) i7-7567U CPU @ 3.50GHz
    Memory: 3.43 GB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 16.6.2 - ~/.nvm/versions/node/v16.6.2/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 7.20.3 - ~/.nvm/versions/node/v16.6.2/bin/npm
  Browsers:
    Chrome: 95.0.4638.69
    Edge: 96.0.1054.43
    Firefox: 91.0
    Safari: 15.1

Used Package Manager

npm

Logs

No response

Validations

@patak-dev
Copy link
Member

patak-dev commented Jun 18, 2022

I don't see a way for Vite to be able to provide the importer to this hook. A request for a module from the browser will not carry this information. If we start to encode it in import analysis &importedFrom=..., then these will be considered different modules. I think we should close this issue by clarifying the documentation. @DylanPiercey would you create specific feature requests for what you were trying to solve here? Maybe we could devise another way?
@bholmesdev since you pointed me to this issue, would you also provide more information on your use case?

@patak-dev patak-dev added documentation Improvements or additions to documentation and removed bug p3-minor-bug An edge case that only affects very specific usage (priority) labels Jun 18, 2022
@patak-dev
Copy link
Member

Currently, the importer is being set to a fake index root here

async resolveId(rawId, importer = join(root, 'index.html'), options) {

This seems arbitrary, and we could remove the default and pass undefined as the importer which may be better for plugins. I tried to do so, and there is only one failing test. I don't think we should change this at this point, looks like it hasn't been a big issue so far. But we may want to review this with more time in the next major.

@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation rollup plugin compat
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants