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

Trying to import a dependency using an ESM import() statement fails #18739

Closed
7 tasks done
hriverahdez opened this issue Nov 22, 2024 · 2 comments
Closed
7 tasks done

Comments

@hriverahdez
Copy link

Describe the bug

I am trying to dynamically import a dependency from node modules, based on a condition. In this case I want to use the package https://www.npmjs.com/package/@gtm-support/vue-gtm for google tag manager. This package is a Vue plugin but I don't want to import it and register the plugin based on some condition I receive from my infra during startup.

The code looks roughly like this

if (optionalTracking) {
  const { createGtm } = await import('@gtm-support/vue-gtm');
  app.use(createGtm({ id: 'GTM-1234567', defer: true }));
}

This works during dev, but not during build. I debugged the unminified prod build output and it seems like the __vitePreload function has a promise that silently fails, cannot be caught and simply halts execution. Then then clause is never executed, and neither is code that follows the import statement.

image

I've looked around multiple issues and the only one that seems to ring a bell is this closed one

#2009

where it's mentioned that this __vitePreload function could fail and never resolve. Though still could be unrelated, I'm not sure.

Is what I'm trying to achieve not possible/currently supported? Or am I missing something that might be in the docs?

Reproduction

https://stackblitz.com/edit/vitejs-vite-tdaeem?file=src%2Fmain.js

Steps to reproduce

You'll notice the app will start just fine for dev mode. I've added the serve package which allow you to run the prod build. So if you execute:

> npm run build
> serve -l 5173 dist/

You'll see that the app is no longer mounted.

System Info

System:
    OS: Linux 6.6 Manjaro Linux
    CPU: (16) x64 AMD Ryzen 7 PRO 4750U with Radeon Graphics
    Memory: 3.09 GB / 30.58 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 20.11.0 - ~/.volta/tools/image/node/20.11.0/bin/node
    npm: 10.2.4 - ~/.volta/tools/image/node/20.11.0/bin/npm
    pnpm: 9.1.4 - ~/.local/share/pnpm/pnpm
  npmPackages:
    @vitejs/plugin-vue: ^5.0.4 => 5.1.2 
    vite: ^5.1.6 => 5.4.2

Used Package Manager

npm

Logs

No response

Validations

@hi-ogawa
Copy link
Collaborator

It look like this is a top-level-await dead lock as @gtm-support/vue-gtm chunk is importing nextTick from vue in the entry chunk. This is a duplicate of #16520 (comment). See linked comment for the workaround.

@hriverahdez
Copy link
Author

hriverahdez commented Nov 25, 2024

It look like this is a top-level-await dead lock as @gtm-support/vue-gtm chunk is importing nextTick from vue in the entry chunk. This is a duplicate of #16520 (comment). See linked comment for the workaround.

hm, Interesting.... I have other top-level awaits on my main file though and those have been working fine so far. Only this new change seems to be causing it. Nevertheless I will test it out.

EDIT: ah wait, you probably meant that it's caused by top-level await and this particular lib, because of the nextTick import. Got it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants