tmeasday / storybook-skeleton

3 stars 1 forks source link

Use a static require context #15

Closed tmeasday closed 3 years ago

tmeasday commented 3 years ago

Based on https://github.com/tmeasday/storybook-skeleton/pull/12

The idea here is to work around the (apparent) lack of support for lazy require contexts with lazy compilation.

So instead:

So in a sense we are implementing our own lazy require context via a non-lazy require context and a bunch of lazy "proxy" imports.

I am guessing there is some way to achieve this without manually creating the .stories.import.jsx files, but instead getting webpack to do it via some special loader? I was thinking something like

  1. Transform any file .stories.jsx to a simple import proxy (i.e. like what we have in Pages.stories.import.jsx).
  2. That file imports the same file (is that allowed?) but via a different loader (import('./Pages.stories.jsx!no-proxy-loader'))
  3. That different "loader" just doesn't do step 1 (so emits the file as normal).

I got this idea from browsing through NextJS's source to see how they do the equivalent thing. I don't pretend to understand the nuance but I did see this:

https://github.com/vercel/next.js/blob/0af3b526408bae26d6b3f8cab75c4229998bf7cb/packages/next/build/webpack/loaders/next-client-pages-loader.ts#L24-L30

bebraw commented 3 years ago

I pinged Tobias about this as he'll know the right way to do this.

bebraw commented 3 years ago

Can you try https://github.com/tmeasday/storybook-skeleton/issues/19#issuecomment-855867404 with this one? I.e. port to import() and make sure it's using version of webpack 5 with the fix with Tobias I made to support lazy compilation.

tmeasday commented 3 years ago

@bebraw in https://github.com/tmeasday/storybook-skeleton/issues/19#issuecomment-856718818 I confirmed it works with import() thanks to your changes. Did you still want me to try here also?

bebraw commented 3 years ago

@tmeasday Nope, that's enough. Now we know import() works but require.context doesn't. Let me comment to your comment.

tmeasday commented 3 years ago

Let's close this then, I think this approach is no longer relevant.