FredKSchott / snowpack

ESM-powered frontend build tool. Instant, lightweight, unbundled development. ✌️
https://www.snowpack.dev
MIT License
19.48k stars 922 forks source link

[BUG] Resolve dynamic imports #3038

Open jaschaio opened 3 years ago

jaschaio commented 3 years ago

Bug Report Quick Checklist

Describe the bug

Snowpack doesn't resolve dynamic imports:

import { library, icon } from '@fortawesome/fontawesome-svg-core';

const getIcon = async ( { icon: iconToFetch } ) => {

    const { [ iconToFetch ]: importedIcon } = await import( `@fortawesome/free-brands-svg-icons/${ iconToFetch }.js` );

    library.add( importedIcon );

    return icon( importedIcon, { classes } ).html

}

( async () => {

    const html = await getIcon( { icon: 'faFacebook' } );

    document.body.innerHTML = html;

} )();

Throws Uncaught (in promise) TypeError: Failed to resolve module specifier '@fortawesome/free-brands-svg-icons/faFacebook.js'

To Reproduce

Reproducible example: https://gist.github.com/jaschaio/321e794e7f8fb15d4bf9dfa0da46656c

Expected behavior

I would expect snowpack to notice the dynamic import, and replace the module identifier with the build output directory.

amankkg commented 3 years ago

This is more likely to be related to esbuild rather than snowpack. Here is some discussion going on https://github.com/evanw/esbuild/issues/700

TL;DR dynamic imports isn't something esbuild gonna support out of the box

drwpow commented 3 years ago

This does seem to be a bug; thanks for filing! Will investigate.

PeterPanZH commented 3 years ago

It seems that snowpack won't resolve the path when dynamic import has a variable in it. Any update?

IanVS commented 3 years ago

This seems related to https://github.com/snowpackjs/snowpack/issues/2861, right?

PeterPanZH commented 3 years ago

This seems related to #2861, right?

Ah, right! This feature will solve the problem.

IanVS commented 3 years ago

@drwpow I think this can be closed as a duplicate.

aneta-kjeldsen commented 2 years ago

I'm expiriencing exactly the same thing. Any progress on this issue?