Closed wvanrensselaer closed 3 years ago
Hey @wvanrensselaer :wave:, Thank you for opening an issue. We'll get back to you as soon as we can. Please, consider supporting us on Open Collective. We give a special attention to issues opened by backers. If you use Loadable at work, you can also ask your company to sponsor us :heart:.
Actually impacts regular loadable components too: https://github.com/wvanrensselaer/loadable-components-bug/compare/test-regular-loadable
Actually a very interesting problem. So "sometimes" webpack 5 does not obey the chunk name it was given.
The simplest and most error-resilient way would be to stop relaying on chunk names/generating chunk names, and do a reverse search from the stats for the given file name - there is a piece of extra information on how files are connected to chunks as well as how chunks are connected to each other... and it was just removed - https://github.com/gregberge/loadable-components/pull/735
The issue requires a little more investigation before moving forward, as it might cause a major architecture change.
That could be good. Might be tricky too though since Webpack does its best to hide the generated filenames, only accessible by calling __webpack_require__.u
with the chunk ID. So still need the chunk ID first unless there's some plugin magic we can do 🤔 .
Both webpack
and babel
plugins know everything about everyone.
The problem is right now babel
"thinks" that it controls webpack, while (by fact) it is not, and in order to fix it we have to "postprocess" result of babel transformation and correct chunks names.
There are at least two ways to do it::
Had some time to dig into this some more. I think we can patch the Babel plugin as a quick fix, need to rework chunkNameProperty
slightly. Can't base webpackChunkName
on the transformed template literal as it has replaced the directory separator and [request]
refers to the whole file name. So when the template literal expression is only a partial file name, loadable breaks:
const Translations = loadable.lib((props) => import(`./translations.${props.locale}.json`));
Transforms to:
const Translations = loadable.lib({
chunkName(props) {
return `translations-${props.locale}-json`.replace(/[^a-zA-Z0-9_!§$()=\-^°]+/g, "-");
},
importAsync: props => import(
- /* webpackChunkName: "translations-[request]" */
+ /* webpackChunkName: "[request]" */
`./translations/messages.${props.locale}.json`
)
});
I'll try to put a PR together this week. Can work around this for now by using the full file name in the template literal expression:
const Translations = loadable.lib((props) => import(`./${props.file}.json`));
<Translations file={`translations.${locale}`}>
Not sure this is the right way to move forward. We can or 100% control chunk names, or 100% not. Something in between is not an option, and will for example, not work for wp4, or some future wp5 version, as in this very internal-ish moment.
This would still be 100% control of chunk names, just fixing the bug with [request]
and partial file names. Verified that it affects Webpack 4 too. But I can hold off and use the workaround above for now.
So the problem also actual for wp4 and the fix is valid for wp4 as well? That removes all my concerns.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
🐛 Bug Report
Using a dynamic import with
loadable.lib
and Webpack 5 leads toChunkExtractor
error in SSR. Can reproduce in this repo.Relevant code in
Application.js
:Chunk names in
loadable-stats.json
come out as:Error during SSR is:
Trying to get
translations-en-US-json
, but the chunk name istranslations-translations-en-US-json
. I think it's an issue generating the chunk name in the Babel plugin here but not quite sure how best to fix yet. Compiled code in the bundle comes out to:But that should be
translations-translations-${props.locale}-json
in this case.To Reproduce
Clone the demo repo here and follow instructions in README to run the code.
Expected behavior
Resolves the correct chunk name in SSR.
Link to repl or repo (highly encouraged)
https://github.com/wvanrensselaer/loadable-components-bug
Run
npx envinfo --system --binaries --npmPackages @loadable/component,@loadable/server,@loadable/webpack-plugin,@loadable/babel-plugin --markdown --clipboard
Paste the results here: