-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Doesn't work with "multiple files" import #4433
Comments
There seems to be an existing solution: https://github.com/faceyspacey/react-universal-component together with https://github.com/faceyspacey/webpack-flush-chunks. I'll see if I can get them working with Next.js. If they work alright would it be a viable option to replace next/dynamic with react-universal-component in Next.js? Or is it in the domain of Next.js plugins? |
I managed to make everything work. I'm very excited, because for so many days I was hoping that "the next solution will finally work" and there was always something lacking. But to get to the final solution, I had to change Next.js in a few ways - I'd like to get those changes into the package, as I think they'll benefit not only me, but also Next.js in the long run. TL;DR:
Below is a comprehensive description of whats and whys. The
|
@timneutkens I hope I'm not overstepping my bounds if I ask you to give me any feedback on this? Right now I could prepare PRs for changes that I suggested - I just want to make sure that the problems I highlighted are something that you'd also like to see fixed. |
@fatfisz let me read up on this soon! it's quite a lot of research 😄, I've been thinking about making some changes to next/dynamic anyway 👍 |
Hi, could I just create said PRs? They'll be very small and easy to merge - I promise 🙂 I'd like to move forward with my project and while I can patch webpack to do what I need, Next.js is much harder to adjust (I'd need to release my own version, monkey patching is impossible). I'm sure that those changes won't affect Next.js negatively regardless of what direction next/dynamic will take. |
Sure go ahead with creating a PR, I can review them and we have extensive tests for the feature anyway, so if you break something you'll know 😄 |
Just a heads up that both branches seem to be passing. I just updated them with the latest canary. |
Fixes one of the problems described in #4433. The old regexp was removing everything after a hyphen, so with a chunk name like so: ``` chunks/path-to-a-file-[hash].js ``` the saved chunk name was ``` chunks/path ``` This caused problems, because webpack by default changes `/` to `-` in chunk names generated e.g. by ``import(`foo/${bar}`)``. After this change the chunk name will be ``` chunks/path-to-a-file ```
This fixes a missed bug introduced in #4510. Because the regexp was `/-[^-]*/` and not `/-[^-]*$/`, a wrong part of the filename was being removed: ``` bad: 'foo-bar-0123456789abcdef-0123456789abcdef.js' -> 'foo-0123456789abcdef-0123456789abcdef.js' good: 'foo-bar-0123456789abcdef-0123456789abcdef.js' -> 'foo-bar-0123456789abcdef' ``` By a stroke of luck this didn't affect the existing dynamically generated chunks. To prevent regression I've added unit tests for the function that generates the name. Btw. in the original issue (#4433) I used the right regexp, I just used the wrong regexp in #4510. cc @timneutkens
…el#4510) Fixes one of the problems described in vercel#4433. The old regexp was removing everything after a hyphen, so with a chunk name like so: ``` chunks/path-to-a-file-[hash].js ``` the saved chunk name was ``` chunks/path ``` This caused problems, because webpack by default changes `/` to `-` in chunk names generated e.g. by ``import(`foo/${bar}`)``. After this change the chunk name will be ``` chunks/path-to-a-file ```
This fixes a missed bug introduced in vercel#4510. Because the regexp was `/-[^-]*/` and not `/-[^-]*$/`, a wrong part of the filename was being removed: ``` bad: 'foo-bar-0123456789abcdef-0123456789abcdef.js' -> 'foo-0123456789abcdef-0123456789abcdef.js' good: 'foo-bar-0123456789abcdef-0123456789abcdef.js' -> 'foo-bar-0123456789abcdef' ``` By a stroke of luck this didn't affect the existing dynamically generated chunks. To prevent regression I've added unit tests for the function that generates the name. Btw. in the original issue (vercel#4433) I used the right regexp, I just used the wrong regexp in vercel#4510. cc @timneutkens
Looks like some of the changes that @fatfisz suggested have been merged. But dynamic imports (using dynamic paths) still don't work with NextJS v6.1.1 |
Will be fixed with #4639 |
This sounds awesome - so you'll be dropping |
@fatfisz yep that's what I'm working on, works great so far 👍 |
Replace the line with require(variable/template string), what is the consequence of doing this? |
Describe the bug
This code causes the "handle-import" babel plugin to break:
To Reproduce
Module build failed: TypeError: Cannot read property '0' of undefined
error fromnode_modules\next\dist\server\build\babel\plugins\handle-import.js:35:30
Expected behavior
I'd expect Next.js to be able to handle this kind of
import
usage, as it's supported by webpack. The old way to do the same, namelyrequire.context
, is not breaking like this (though it's breaking in other ways, as I'll bescribe in "additional context").System information
Additional context
Related issue: #2690
This problem is directly caused by the "handle-import" plugin expecting the first argument to be a string literal (which has the
value
property). Since in my case it's a template literal, it doesn't have avalue
, onlyexpressions
andquasis
.For a while now I've been trying to create a wrapper around Next.js that's able to work with dynamically imported files (a tool for documentation which includes all
*.docs.js
files in the project).First of all, I can't use
pages/
- I want documentation files to be close to the scripts they document. I want the original structure of the source to be reflected by the structure of documentation, so replicating the directory structure in two places (original and pages) seems like an unmaintainable idea.That's how I arrived at a solution that uses import-all.macro. The problem with that is, it's powered by babel. So when, as a user, I'm adding a new documentation file, I'll need to stop the tool, purge the cache (since all imports are inlined in a file), and restart the tool. I'm losing the file-listening capabilities of webpack-dev-server, and I need to go around the limitations of babel caching.
That's how I arrived at a solution that uses
require.context
. It almost works! I can't get the SSR story correctly though - while the server renders the appropriate script, the chunk doesn't seem to be ready whendynamic
is rendering the component. Actually, the chunk doesn't seem to be generated at all! That's why I switched toimport
, to try and see if I could get the chunk-creating machine going at least with the other way of importing. As it is now, I've spent several days on this, I'm spinning in place, and I'm determined to fix the problem.The text was updated successfully, but these errors were encountered: