-
Notifications
You must be signed in to change notification settings - Fork 508
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 of static files are not working as expected #533
Comments
I'm confused, is this for Storybook stories or for distribution as a library? Because those have different configs. On Storybook, there was a big change with fixes in #435. I'm not sure if that's related however. It still hasn't been released though... see #512 For Rollup images -- there is this HOWTO: #379 (comment) . I'm not totally sure how that interacts with dynamic imports but it wasn't clear if you'd tried that. |
Yeah, sorry for the confusion, I’ve mixed up everything in my post.
The thing is, I can’t see “flags” folder in my “dist” after compilation no matter what I try. I didn’t try to npm run build yet, was just building my component right in the Storybook. Going to try to build today and maybe just use boilerplate in the “example” folder to test it instead of Storybook.
UPD: And yes, I've tried that HOWTO, but it didn't work for me either.
… On 4 Mar 2020, at 07:13, Anton Gilgur ***@***.***> wrote:
I'm confused, is this for Storybook stories or for distribution as a library? Because those have different configs.
On Storybook, there was a big change with fixes at #435. I'm not sure if that's related however. It still hasn't been released though... see #512
For Rollup images -- there is this HOWTO: #379 (comment) . I'm not totally sure how that interacts with dynamic imports but it wasn't clear if you'd tried that.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Uhuh... well if you haven't used
Storybook doesn't produce a Sounds like you're not using things correctly and mixing two different tools up. |
Hmm, I've been thinking that |
Ah, ok gotcha. Seems like the docs might be wrong there as the way the stories are configured, they are imported from |
Oh, I see. I've also fixed my problem with When I
It shows the flag without any problem. But when I use a template string
It crashes with error: |
Dynamic imports are not statically analyzable (as in, the bundler doesn't know to include dynamically imported files). As far as I know no modern bundler can support them. You have to either include all of the flags, or just the flags you know will be used. |
Sorry, I don't mean to argue and stuff, I haven't been doing much configuration lately (just using |
@lpolito thanks for chiming in. I've dealt with this before but didn't really remember the caveats. Your statement could use some modification though: Dynamic imports are statically analyzable, that's how code-splitting works in both Rollup and Webpack after all. @randex that's an interesting example, I am curious how that simple config works. The Rollup issue points to virtual modules/custom resolvers as a potential solution but I'm not sure if there's a simple way to do it. |
@randex actually I think that example is far simpler than we think and has nothing to do with bundlers -- it just includes the entire It also has this issue that you'll end up bundling all flags: ekwonye-richard/react-flags-select#22 |
Thank you very much for your research and that useful link to Rollup issue. I've tried to use a virtual module here but I'm stuck again with a problem. import test from 'testmodule'
export default function () {
return test
} Now module.exports = {
rollup (config) {
config.plugins.unshift({
name: 'plugin-test-module',
resolveId (id) {
console.log('resolveId', id);
if (id === 'testmodule') {
return id;
}
return null;
},
load (id) {
if (id === 'testmodule') {
return 'export default "Test is successful"';
}
return null;
},
});
return config;
},
}; So what should happen is I just need to see "Test is successful" in the browser. |
Sorry this is a general Rollup support question and so is getting further and further out-of-scope. I think using I'm not sure why |
I understand. Thank you very much for all your help. I’ll post a solution here when I find it for the sake of completeness. |
@lpolito Just want to point out that Webpack supports partially dynamic imports, e.g. https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import |
Current Behavior
I have a folder full of country flags SVGs, and I need to display a specific one. I think that the best solution here is to just dynamically import it into an
img
.I ended up spending whole day trying to figure out how to make it work. Also tried ES6 imports and just setting src to
../flags/${value}.svg
(without using any imports), but the storybook keeps failing with an errorModule not found: Error: Can't resolve '../flags' in...
all the time.I've also tried to copy the whole
flags
folder intodist
usingrollup-plugin-copy
with this config:But the folder just won't appear in the
dist
.I'm not experienced with Rollup unfortunately, so maybe there's someone who can help me use static files like these in my package?
Expected behavior
Correct handling of dynamic imports of static files.
Suggested solution(s)
N/A
Additional context
Your environment
The text was updated successfully, but these errors were encountered: