game.json
without using the framework. Details are described below.├── game.js├── game.json├── images│ ├── a.png│ ├── b.png├── stage1│ └── game.js│ └── images│ ├── 1.png│ ├── 2.png└── stage2.js└── utils
Configuration in game.json
{..."subpackages": [{"name": "stage1","root": "stage1/" // Specify a directory; the game.js in the root directory will be the entry file, and all resources in the directory will be packaged together}, {"name": "stage2","root": "stage2.js" // You can also specify a JS file}]...}
Each subpackage
configuration in subpackages includes the following fields:Field | Type | Description |
root | String | Root directory of the subpackage. |
name | String | |
independent | Boolean |
subpackages
, the paths specified in subpackages
will be packaged accordingly. Directories outside the subpackages
configuration paths will be packaged into the main package. The root directory of a subpackage
cannot be a subpackage
of another subpackage.packageA
cannot require JS files from packageB
, but can require JS files from the main package or within packageA
.packageA
cannot use resources from packageB
, but can use resources from the main package or within packageA
.wx.loadSubpackage
to pre-download the main package or other subpackages and execute the downloaded code package logic at an appropriate time (e.g. when the user taps "Start Game")..├── game.js├── game.json├── moduleA│ └── game.js├── moduleB│ └── game.js└── utils
game.json
.{"subPackages": [{"name": "moduleA","root": "/moduleA/", // Regular subpackage},{"independent": true, // Independent subpackage, specify a directory; the game.js in the root directory will be the entry file, and all resources in the directory will be packaged together"name": "moduleB","root": "/moduleB/",}]}
// Share with friends or groupswx.shareAppMessage({title: 'Share the title',imageUrl: 'xx.jpg',query: 'a=1&b=2',path: '/moduleB/' // path corresponds to the root of the independent subpackage as configured in game.json})Share on Momentswx.onShareTimeline({title: 'Share the title',imageUrl: 'xx.jpg',query: 'a=1&b=2',path: '/moduleB/' // path corresponds to the root of the independent subpackage as configured in game.json})// Add to Favoriteswx.addToFavorites({title: 'Favorite title',imageUrl: 'xx.jpg',query: 'a=1&b=2',path: '/moduleB/' // path corresponds to the root of the independent subpackage as configured in game.json})
const loadTask = wx.loadSubpackage({name: '/moduleA/', // Download another subpackagesuccess(res) {console.log('load moduleA success', res)},fail(err) {console.error('load moduleA fail', err)}})
The path parameter in wx.shareAppMessage
or the return parameter in wx.onShareAppMessage
must be the root field of the independent subpackage as defined in game.json
.const loadTask = wx.loadSubpackage({name: 'stage1', // name can be either the name or the rootsuccess: function(res) {// Callback for successful subpackage loading},fail: function(res) {// Callback for failed subpackage loading}})loadTask.onProgressUpdate(res => {console.log('Download progress', res.progress)console.log('Downloaded data length', res.totalBytesWritten)console.log('Expected total data length', res.totalBytesExpectedToWrite)})
// Preloadconst loadTask = wx.preDownloadSubpackage({name: 'stage1',success: function(res) {// Callback for successful subpackage preloading},fail: function(res) {// Callback for failed subpackage preloading}})loadTask.onProgressUpdate(res => {console.log('Download progress', res.progress)console.log('Downloaded data length', res.totalBytesWritten)console.log('Expected total data length', res.totalBytesExpectedToWrite)})// Execute subpackage code logic at an appropriate timewx.loadSubpackage({name: 'stage1'})