# Using Subpackage
# Configuration
Assume that the directory structure of a Mini Program supporting subpackages is as follows:
├── app.js
├── app.json
├── app.wxss
├── packageA
│ └── pages
│ ├── cat
│ └── dog
├── packageB
│ └── pages
│ ├── apple
│ └── banana
├── pages
│ ├── index
│ └── logs
└── utils
The developer declares the subpackage structure of a project in the subpackages
field via app.json:
The field can also be written as subPackages.
{
"pages":[
"pages/index",
"pages/logs"
],
"subpackages": [
{
"root": "packageA",
"pages": [
"pages/cat",
"pages/dog"
]
}, {
"root": "packageB",
"name": "pack2",
"pages": [
"pages/apple",
"pages/banana"
]
}
]
}
In subpackages
, each subpackage are configured with the following fields:
Field | Type | Description |
---|---|---|
root | String | Root directory of subpackage |
name | String | Alias of subpackage, which can be used in Subpackage Preload |
pages | StringArray | Subpackage page path, which is relative to the root directory of the subpackage |
independent | Boolean | Indicates whether a subpackage is an Independent Subpackage |
# Packaging Rules
- After
subpackages
is declared, files are packaged based on the paths configured insubpackages
, and files in the directories outside the paths insubpackages
are packaged into the app (main package). - The app (main package) can also have its own pages (i.e. the outermost pages field).
- The root directory of
subpackage
cannot be a subdirectory configured in anothersubpackage
. ThetabBar
page must be in the app (main package).
# Referencing Rules
packageA
cannot require JS files inpackageB
, but can require JS files inapp
andpackageA
.packageA
cannot import templates inpackageB
, but can import templates inapp
andpackageA
.packageA
cannot use resources inpackageB
, but can use resources inapp
andpackageA
.
# Compatibility with Lower Versions
The compatibility with earlier versions of Weixin is handled by compiling two code packages on the Weixin backend: one is code obtained after pages are subpackaged, and the other is the compatible code of the whole package.
The subpackages are used in new versions, and the whole package is still used in earlier versions. In the whole package, the path in each subpackage
is put into pages
.
# Sample Project
Download the source code of a Mini Program sample (for subpackage preload)