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

Dynamic imports with variables in a dependency are not processed at all #6179

Open
7 tasks done
balthild opened this issue Dec 19, 2021 · 3 comments
Open
7 tasks done
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@balthild
Copy link

balthild commented Dec 19, 2021

Describe the bug

Variable dynamic imports such as import(`./inc/${name}.js`) that appears in a dependency are not transpiled at all.

Maybe related to #1903.

Reproduction

https://github.com/balthild/vrt-esmdi

  • /testlib2 - The dependency that contains variable dynamic imports
  • /app - A vite app
  • /app/es - A copy of /testlib2/es

Step

  1. Clone the repo
  2. cd vrt-esmdi/app
  3. yarn && yarn build
  4. Check the build output in dist and you can find that there's nothing corresponding to /testlib2/es/inc/*.js which is dynamically imported.
  5. Edit /app/main.js and change the first line to import { testImport } from './es';
  6. yarn build
  7. Check the build output and you can find that the dynamically imported scripts are there.

Since the contents of /testlib2/es and /app/es are identical, so I think the import expressions are treated differently when they're in the app or in a dependency.

System Info

System:
    OS: Linux 4.19 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (4) x64 Intel(R) Xeon(R) CPU E5-2696 v2 @ 2.50GHz
    Memory: 3.38 GB / 7.79 GB
    Container: Yes
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 16.13.1 - /usr/bin/node
    Yarn: 1.22.17 - /usr/bin/yarn
    npm: 8.1.2 - /usr/bin/npm

Used Package Manager

yarn

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Apr 1, 2022

I can reproduce this in Vite 2.9.1. Looks like this is because dynamic import is disable in node_modules here. It's not clear to me why that is there, but a workaround is to set:

export default defineConfig({
    build: {
        dynamicImportVarsOptions: {
            exclude: []
        }
    }
})

Sending a PR to remove it.

@bluwy bluwy added p2-edge-case Bug, but has workaround or limited in scope (priority) and removed pending triage labels Apr 1, 2022
@poyoho
Copy link
Member

poyoho commented Apr 2, 2022

I found that 'importAnalysisBuild' also ignores node_module.

I think dynamic import from the node module, it is possible to repackage public variables within the package once.
And using several files from a node module will also make the dependencies of those files repackage a chunk.

@bluwy
Copy link
Member

bluwy commented Apr 3, 2022

@poyoho Are you proposing that importAnalysisBuild should run for node_modules too, so that it gets the preload optimization also? I'm not very familiar with that part of the codebase, but if so that makes sense to me too and we could probably test that in the PR. Not sure if there's any gotchas with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
Status: Approved
Development

Successfully merging a pull request may close this issue.

3 participants