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:
Since we are using Webpack's magic comments, we need to make sure Babel is not removing those.
Add comments: true
to your Babel config, for example:
At this point all the code used by StudentSide.js
will be put into student.chunk.bundle
and
TeacherSide.js
into teacher.chunk.bundle
.
Before we can actually render out application, we need to add resolver using ScriptManager.shared.addResolver(...)
,
so it can resolve the chunks:
This code will allow Re.Pack's ScriptManager
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/output/<platform>/remote
by default,
for example: <projectRoot>/build/output/ios/student.chunk.bundle
.
You should upload files from this directory to a remote server or a CDN from where ScriptManager
will download them.
You can change this directory and/or mark chunks as local. Refer to dedicated Local vs Remote chunks guide for more information.