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] Dynamic imports in modules fail under certain conditions. #3094

Open WilsontheWolf opened 3 years ago

WilsontheWolf commented 3 years ago

Bug Report Quick Checklist

Describe the bug

When a module dynamically imports a file relative to itself it fails. This is due to the fact that all modules are stored in one dir. I'm using a module that lazy loads its components. As such it dynamically imports them as a relative import. The script importing is @skyra/discord-components-react/dist/index.js. Snowpack loads this as @skyra.discord-components-react.dist.index.es.v2.0.3-alpha.0.js. However, when it imports the module it does ./module which normally is @skyra/discord-components-react/dist/module. However, due to the way snowpack loads, it is not in its sub-dir. As such ./module resolves to snowpack pkg dir instead of the @skyra/discord-components-react/dist/. The solution to this would be to allow snowpack to load it in the traditional module sense. Would this be an option to load my module with the dir structure it will?

To Reproduce

We can't fix bugs that we can't see for ourselves. Issues often need to be closed if this section is skipped.

I set up a minimal repo if you just want to clone that https://github.com/WilsontheWolf/import-bug

  1. Create a project (I used) yarn create snowpack-app import-bug --template @snowpack/app-template-react --use-yarn
  2. Add the modules giving you issues (in my case @skyra/discord-components-core @skyra/discord-components-react Alpha builds) yarn add @skyra/discord-components-core@alpha @skyra/discord-components-react@alpha
  3. Edit the app.js to add the import and use it
    
    import React from 'react';
    import {
    DiscordMessage,
    DiscordMessages
    } from '@skyra/discord-components-react';

function App() { return (

Test
);

}

export default App;


4. See error!

### Expected behaviour

It should import properly

### Anything else?

Related to my discussion #2927.

The repo for the module is https://github.com/skyra-project/discord-components if you need it.
vivianeasley commented 3 years ago

I believe I'm having the same issue. Recently upgraded to snowpack 3.3.0 and use-sound stopped working. The use-sound module tries to lazy load howler and I get this issue when I yarn start: Cannot find module ''howler'' from /frontend/app/node_modules/use-sound/dist/use-sound.esm.js

This was working fine until the upgrade from 3.1.0, which I had to move away from because I was having issues with this bug: https://github.com/snowpackjs/snowpack/issues/2948

mklasinc commented 3 years ago

I believe I'm having the same issue. Recently upgraded to snowpack 3.3.0 and use-sound stopped working. The use-sound module tries to lazy load howler and I get this issue when I yarn start: Cannot find module ''howler'' from /frontend/app/node_modules/use-sound/dist/use-sound.esm.js

This was working fine until the upgrade from 3.1.0, which I had to move away from because I was having issues with this bug:

2948

I'm having the same issue, I believe this is quite an urgent bug since it completely breaks dev/prod builds.

eudinnou commented 3 years ago

Think this bug is similar: https://github.com/snowpackjs/snowpack/issues/3370