Fundamentals of Cross-Platform Development (III)
Learn about another challenge that we face while cross-platform development: module swapping.
We'll cover the following
Most of the time, we already know at build time what code has to be included in the client bundle and what shouldnāt be included. This means that we can take this decision upfront and instruct the bundler to replace the implementation of an entire module at build time. This often results in a leaner bundle because weāre excluding unnecessary modules, and produces a more readable code because we donāt have all the if...else
statements required by runtime and build-time branching.
Letās find out how to adopt module swapping with webpack by updating our example.
The main idea of mudule swapping is that we want to have two separate implementations of our sayHello
functionality: one optimized for the server (say-hello.js
) and one optimized for the browser (say-hello-browser.js
). Weāll then tell webpack to replace any import of say-hello.js
with the say-hello-browser.js
module. Letās see what our new implementation looks like now.
Get hands-on with 1400+ tech skills courses.