Let's assume, we are building an E-Learning application with specific functionalities for a student
and for a teacher. Both student and a teacher will get different UIs and different features, so it
would make sense to isolate the student's specific code from the teacher's. That's were Code
Splitting comes into play - we can use dynamic import(...) function together with React.lazy and
React.Suspense to conditionally render the student and the teacher sides based on the user's role.
The code for the student and the teacher will be put into a remote async chunk, so that the initial
download size will be smaller.
It's recommended to read:
first, to understand Code Splitting, usage on a high-level and get the necessary context.
Before you begin, make sure the Re.Pack's native module is linked into your application:
Let's use the following code for the student's side:
And a code for the teacher's side:
Now in our parent component, which will be common for both the student and the teacher:
At this point all the code used by StudentSide.js will be put into student.chunk.bundle and
TeacherSide.js into teacher.chunk.bundle.
If you try to render Home component in your application, it should work in development
(the development server must be running). For production however, there's an additional step
necessary - to configure ChunkManager:
This code will allow Re.Pack's ChunkManager to
actually locate your chunks for the student and the teacher, and download them.
When bundling for production/release, all remote chunks, including student.chunk.bundle and
teacher.chunk.bundle will be copied to <projectRoot>/build/<platform>/remote by default.
You should upload files from this directory to a remote server or a CDN from where ChunkManager
will download them.
You can change remote chunks output directory using
remoteChunksOutput
in OutputPlugin configuration.
By default all async chunks are remote chunks, meaning they are hosted on a remote server (e.g: CDN) and downloaded on demand.
Local chunks, however, are always stored on a filesystem and bundled together with main bundle into
the final .ipa or .apk file, meaning they increase initial download size the user has to
download when installing your application.
Local chunks should only be used if you know that the majority of users will need them or if you want to have pre-built features/modules.
Local chunks will not be copied into <projectRoot>/build/<platform>/remote (or directory specified
in remoteChunksOutput).
They will be automatically copied to appropriate locations by
OutputPlugin.
To mark a chunk as a local chunk, you need to add it's name or a RegExp matching the chunk's name to
OutputPlugin's localChunks option in
your Webpack config.
For example, if we know they majority of the users will be students, it would make sense to make
student chunk a local chunk. To mark the student chunk as a local one, apply this diff to your
Webpack configuration: