In a universal app there is a need to destinguish whether it is SSR or client bundling. So we can use webpack.DefinePlugin to define a variable according which we can identify client and "not client" or we can also check if the require object has ensure property. But if we create a function-helper and return the expression from it, it doesn't work:
// constant defined in webpack config
if (typeof __CLIENT__ === 'boolean') { // it works
// client code
} else { /* server code */ }
But if we do it this way:
const isClient = () => typeof __CLIENT__ === 'boolean'
if (!isClient()) { /* server code */ } // webpack is trying to bundle server code
So we need reliably identify server rendering and client bundling so as to webpack don't shove server code to the client bundle and the other way around. What approaches do you use to identify sever rendering / client bundling moment?
Sergei
Learning ...
Philip Davis
Software developer
I have all my client-specific code in /src/client (is basically just index.tsx and a polyfill); all my server-specific code in /src/server; all the shared code in /src/app.
The client webpack config excludes everything in /src/server The server webpack config excludes everything in /src/client