Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Sub Packages is a common concept in most Mini Program platforms to improve load performance and code
structure. It can be regarded as chunk split in Web development.
Here are some useful links to their official documents.
WeChat sub packages
QQ sub packages
Alipay sub packages
Baidu sub packages
Toutiao sub packages
Problems
The original design of sub packages based code splitting result in several limitations. A sub
package cannot
require
any module in the main package nor other sub packages.Developers have to place shared code in main package manually, otherwise they will get a compile
error from the Mini Program dev tool. In another hand, if a module was only required by a sub
package, developers should move it into the sub package to reduce size of main package.
Hoist and nohoist
In GojiJS, we use Webpack to analyze module dependencies and bundle the
code. With code splitting we are able to import
dependents in a sub package from anywhere we want, like the main package, node_modules or even other
sub packages. GojiJS can hoist the shared code into a common chunk file ( usually
_goji_commons.js
) in the main package.Here is an example that has 5 pages, one is in the main package and others are in 2 sub packages (
packageA
andpackageB
).GojiJS hoist modules to root common chunk. By doing this the dependencies can match Mini Program sub
packages' limitations.
nohoist.enable
boolean = true
if in production mode, otherwisefalse
.In above example,
redux
anddate-fns
are not shared code so they can be moved into sub packagesto reduce size of main package. We call that optimization nohoist.
You can change
nohoist.enable
option ingoji.config.js
to toggle this feature.nohoist.maxPackages
number = 1
Some modules, like
lodash
in the example, are shared only by sub packages. It is possible toforked them into sub packages. Although the size of total packages increases, it does reduce the
size of main package.
To enable this feature, you can set
nohoist.maxPackages
to a number N above 1. A module sharedless than or equal N will be forked into
packageName/_goji_nohoist_[contenthash].js
.Although the code are duplicated, the runtime closure is still a singleton. For more details see
Independent packages.
nohoist.test
function (module, chunks) => boolean
RegRex
string
You can use this options to nohoist specific modules.