vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
67.28k stars 6.05k forks source link

Supress Vite import() warnings when https:// modules are used #3378

Closed lubomirblazekcz closed 2 years ago

lubomirblazekcz commented 3 years ago

Describe the bug

7  |      const lang = await import(`https://cdn.esm.sh/v41/vanillajs-datepicker/locales/${document.documentElement.lang === "en" ? "cs" : document.documentElement.lang}.js`);
   |                                ^
8  |  
9  |      console.log(lang.default);
The above dynamic import cannot be analyzed by vite.
See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.

Reproduction

(async() => {
    const lang = await import(`https://cdn.esm.sh/v41/vanillajs-datepicker/locales/${document.documentElement.lang === "en" ? "cs" : document.documentElement.lang}.js`);

    console.log(lang.default);
})()

Before submitting the issue, please make sure you do the following

lubomirblazekcz commented 3 years ago

Also sort of related to https://github.com/vitejs/vite/issues/2483 when importmaps or es-module-shims are used __vite__injectQuery causes problems here.

Vite should ignore https:// and imports that are handled by importmaps and don't do any modifications to it.

bluwy commented 2 years ago

You can suppress the warning with:

const lang = await import(/* @vite-ignore */ `https://cdn.esm.sh/v41/vanillajs-datepicker/locales/${document.documentElement.lang === "en" ? "cs" : document.documentElement.lang}.js`);

as suggested in the warning message.

Vite does a simple regex pass to try to capture the import string, so it can't know that the import string starts with http in the first place to be skipping it. The warning message is emitted when the regex fails.