# Small game independent subcontracting

WeChat client 7.0.13, basic library {% version('2.12.1') ​​%} and above versions are now supported. Developer tools need to use the latest nightly version, which can be click here to download.

Independent subcontracting is a special type of subcontracting in mini games, which can run independently of the main package and other subcontracts. When starting a mini game with an independent sub-package path, the client will only download the independent sub-package and start the game, instead of downloading the main package, so as to achieve the need for quick start of the game in some scenarios. Developers can configure certain functionally independent pages into independent subpackages as needed, which can greatly increase the page opening speed.

For example, when a game needs to realize the function of sharing and opening leaderboards, it can use the ability of independent subcontracting:

  1. Configure an independent subcontracting path that only contains leaderboard logic in game.json
  2. The wx.shareAppMessage parameter or wx.onShareAppMessage return parameter is passed into path, and the value is the independent subcontracting path of the leaderboard module configured in game.json
  3. Share to the group chat through the above two interfaces. When other users in the group enter the game through the message card, the client only downloads the leaderboard and starts it independently, instead of downloading the complete code package, saving startup time
  4. After the independent subpackage is started, the developer can use the wx.loadSubpackage interface to pre-download the main package or other subpackages as needed, and execute the downloaded code package logic at an appropriate time (for example, when the user clicks to start the game)

# Sample code

Suppose the directory structure of the mini game is as follows:

.
├── game.js
├── game.json
├── moduleA
│ └── game.js
├── moduleB
│ └── game.js
└── utils

The developer declares the corresponding subpackage as an independent subpackage by defining the independent field in the corresponding subpackage configuration item in the subPackages field of game.json.

{
  "subPackages": [
    {
      "name": "moduleA",
      "root": "/moduleA/", // ordinary subpackage
    },
    {
     "independent": true,
      "name": "moduleB",
      "root": "/moduleB/", // Independent sub-package, specify a directory, game.js in the root directory of the directory will be the entry file, and all resources in the directory will be packaged uniformly
    }
  ]
}

Share the designated independent subcontracting page in the game:

// Share to friends, groups
wx.shareAppMessage({
  title:'Share title',
  imageUrl:'xx.jpg',
  query:'a=1&b=2',
  path:'/moduleB/' // path is the root of the independent subpackage in the game.json configuration
})
// Share to Moments
wx.onShareTimeline({
  title:'Share title',
  imageUrl:'xx.jpg',
  query:'a=1&b=2',
  path:'/moduleB/' // path is the root of the independent subpackage in the game.json configuration
})
// add to favorites
wx.addToFavorites({
  title:'Collection title',
  imageUrl:'xx.jpg',
  query:'a=1&b=2',
  path:'/moduleB/' // path is the root of the independent subpackage in the game.json configuration
})

In an independent subcontract, download the main package or other subcontracts

const loadTask = wx.loadSubpackage({
  name:'__GAME__', // Download the main package
  success(res) {
    console.log('load __GAME__ success', res)
  },
  fail(err) {
    console.error('load __GAME__ fail', err)
  }
})
const loadTask = wx.loadSubpackage({
  name:'/moduleA/', // download other subpackages
  success(res) {
    console.log('load moduleA success', res)
  },
  fail(err) {
    console.error('load moduleA fail', err)
  }
})

# scenes to be used

Entrances that currently support independent subcontracting start include:

-Message cards shared by mini games to chat -Share the mini game to the preview page of Moments -Mini game link card in WeChat favorite list

# Precautions

-There can be multiple independent sub-packages in a mini game -The independent sub-packaged start of the mini game is only valid for cold start -Independent subcontracting is a type of subcontracting. All restrictions of ordinary subcontracting are valid for independent subcontracting. -Independent sub-packages cannot rely on the content of the main package and other sub-packages, including js files and resource files. -The path in the wx.shareAppMessage parameter or wx.onShareAppMessage return parameter must be the root field of the independent subpackage defined in game.json.

# Development tool debugging

The independent subcontracting mode startup can be simulated in the custom compilation conditions of the developer tools, as shown in the figure below

# Low version compatible

The WeChat client version lower than 7.0.13 does not support the independent subcontracting feature of mini games, and will start with the main package by default.

点击咨询小助手