Problem with code splitting
See original GitHub issueIn my code I have something that looks like this:
function retriveLocaleData(language, callback) {
if (language === 'es') {
require('bundle?name=spanish!../locales/es.js')(callback);
} else {
require('bundle?name=english!../locales/en.js')(callback);
}
}
In other projects that would successfully create a separate bundle for my I18n data for that locale, but using this library I am constantly getting this error:
ReferenceError: window is not defined
at evalmachine.<anonymous>:3:37
at evalmachine.<anonymous>:94:10
at ContextifyScript.Script.runInNewContext (vm.js:38:15)
at module.exports ([snip]/node_modules/static-site-generator-webpack-plugin/node_modules/eval/eval.js:62:12)
at Compiler.<anonymous> ([snip]/node_modules/static-site-generator-webpack-plugin/index.js:29:20)
I think I finally understand why it’s happening. Webpack is adding code to the bundle that appends the second chunk to the dom under certain conditions. The code it generates is using window, which blows up while the static-site-generator-webpack-plugin is generating the HTML.
Has anyone encountered this before / can think of any solutions? It would be a real shame to lose the ability to code split while generating a static site.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:8
- Comments:12
Top Results From Across the Web
Code-Splitting - React
Code -Splitting is a feature supported by bundlers like Webpack, Rollup and Browserify (via factor-bundle) which can create multiple bundles that can be ......
Read more >Why code splitting is hard in react server side rendering?
It is basically a way to split code from a large file into smaller code bundles which can be requested on demand or...
Read more >Dealing with Code Splitting Network Failures - In Plain English
Reloading code split chunks to attempt to recover from network failures.
Read more >Code-Splitting in ReactJs with Its Best Practices - XenonStack
Code splitting is the splitting of code into components or numerous bundles which can be loaded when there is a demand or in...
Read more >The problem of code splitting #4010 - reduxjs/redux - GitHub
The summary of this issue is that code splitting in Redux (at least in React) is currently cumbersome and hard to get right,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I believe the OP got it right, here’s my understanding of why this fails:
You want to include something on demand. E.g. your index.js can look as simple as this:
See https://webpack.github.io/docs/code-splitting.html BTW.
The
static-site-generator-webpack-pluginmodule will evaluate yourindex.jsin it’s compilation stage.You’ll try to compile your app, but you’ll get the
window is not definedissue but it will appear vague.This is because it comes from the evaluated file that is created in step 2 above (
main.js). If you enable the log statement in step 2 you can see the generatedmain.js.At line 12 of
main.jsyou’ll see this on-demand requiring magic that webpack adds:And this is where the error actually comes from.
Please somebody correct me if I’m wrong though!
As for solving this, I am not sure. @markdalgleish don’t suppose you have any ideas how to solve this? I am happy to provide a simple repo to reproduce this if it helps.
hey, try #55