# Independent subcontracting

WeChat Client 6.7.2, base library {% version ('2.3.0')%} and above are supported.Developer Tools 1.02.1808300 and above, Download here.

A standalone subcontract is a special type of subcontract in Weixin Mini Program that can operate independently of the main package and other sub-packages.When you enter a Mini Program from a page in a separate subpackage, you do not need to download the main package. The main package is only downloaded when the user enters the normal subpackage or the page within the main package.

Developers can configure some pages with some functional independence to separate subpackages on demand.When Weixin Mini Program is started from a normal subcontract page, the main package needs to be downloaded first;A standalone subpackage does not rely on the main package to run, and can greatly improve the startup speed of the subpackage page.

A Weixin Mini Program can have multiple independent sub-packages.

MiniGame Support for independent sub-packageing started in the base library v2.12.2, as detailed in the Small Mini game Independent Subcontracting Guide .

# Configuration method

Suppose the Weixin Mini Program directory structure is as follows:

├── app.js
├── app.json
├── app.wxss
├── moduleA
│   └── pages
│       ├── rabbit
│       └── squirrel
├── moduleB
│   └── pages
│       ├── pear
│       └── pineapple
├── pages
│   ├── index
│   └── logs
└── utils

Developers by using theapagejsonThesubpackagesfield defines theindependentfield declares the corresponding subpackage to be independent.

{
  "pages": [
    "pages/index",
    "pages/logs"
  ],
  "subpackages": [
    {
      "root": "moduleA",
      "pages": [
        "pages/rabbit",
        "pages/squirrel"
      ]
    }, {
      "root": "moduleB",
      "pages": [
        "pages/pear",
        "pages/pineapple"
      ],
      "independent": true
    }
  ]
}

# limit

Independent sub-packageing is one type of sub-packageing. All restrictions on ordinary sub-packageing apply to independent sub-packageing. Plugins and custom components are treated in separate subpackages in the same way as ordinary subpackages.

In addition, when using independent sub-packageing, be aware of:

  • Independent sub-packages cannot depend on what is in the master package and other sub-packages. includes js files, template, wss, custom components, plug-ins, etc. (JS files, custom components and plug-ins are not restricted by this rule when using subcontract asynchronization of )
  • apagewxssin a master package is invalid for a standalone subpackage, and you should avoid using the styles inаpagewxsdin the standalone subpackaging page;
  • Appcan only be defined within the main package, not within the independent subpackageApp, which may cause unexpected behavior;
  • The use of plug-ins is temporarily not supported in the standalone sub-packages.

# Note

# (1) AboutgetApp ()

Unlike normal sub-packageing, independent sub-packageing runtime,Appis not necessarily registered, sogetApp (]]is also not necessarily available forApp]]objects:

  • When the user launches Weixin Mini Program from a standalone sub-package page, the main package does not exist andAppdoes not. CallgetApp (and get`` undefined。The main package will be downloaded and theApp`will be registered when the user enters the general package or main package page.
  • When a user jumps from a normal subpackage or an intra-subpackage page to a standalone subpackage page, the main package already exists, and invokegetApp (]]to get the real`` App`。

Because of this limitation, developers cannot achieve independent sub-contracting and global variable sharing through theAppobject.

In order to meet this requirement in stand-alone sub-packageing, the base library version {% version ('2.2.4')%} startedgetAppsupport forallowDefault] parameter, returns a default implementation whenAppis not defined.When the master package is loaded andAppis registered, the properties defined in the default implementation are covered and merged into the real`App]].

Example code:

  • In process of independent subcontracting.
const app = getApp({allowDefault: true}) // {}
app.data = 456
app.global = {}
  • In apagejs
App({
  data: 123,
  other: 'hello'
})

console.log(getApp()) // {global: {}, data: 456, other: 'hello'}

# (2) AboutAppLife Cycle

When starting Weixin Mini Program from a standalone subpackage, theApp in the master package'sonLaunchand the firstonShoware called when they first enter the main package or other regular subpackage page from a standalone subpackage page.

Since theAppcannot be defined in the independent subcontract, Weixin Mini Program Life Cycle Monitoring can be done using wx.onAppShow , wx.onAppHide completed.Other events on the Appcan be accessed using wx.onError , wx.onPageNotFound Listen in.

# Low version compatible

When running in WeChat below version 6.7.2, independent sub-packageing is treated as normal sub-packageing and does not have the characteristics of independent running.

Note: In compatibility mode,apagewxssin a master package may affect pages in separate subpackages, so you should avoid using the styles inаpagewxsdin independent subpackage pages.