# Use of sub-packageing

# Configuration method

Suppose that the sub-packageing Mini Programs directory structure is as follows:

 app.js
 app.json
 app.wxss
 packageA
    pages
        cat
        dog
 packageB
    pages
        apple
        Banana
 pages
    index
    logs
 utils

Developers through the app.json subPackages Field Declares Project Subcontract Structure:

Written subpackages Also supported.

{
  "pages":[
    "pages/index",
    "pages/logs"
  ],
  "subPackages": [
    {
      "root": "packageA",
      "pages": [
        "pages/cat",
        "pages/dog"
      ],
      "entry": "index.js"
    }, {
      "root": "packageB",
      "name": "pack2",
      "pages": [
        "pages/apple",
        "pages/banana"
      ]
    }
  ]
}

subPackages Each subcontract is configured with the following:

field type Introductions
root String Subpackage root directory
Name String Subcontract Alias,Subcontract PredownloadCan be used when
pages StringArray Sublot page path, as opposed to sublot root directory
independent Boolean Whether the subcontract isIndependent subcontract
entry String Subcontract entry documents

# Packing principle

  • statement subPackages After that, will press the subPackages Configure the path for packaging,subPackages Directories outside the configuration path will be packaged into the main package
  • The main package can also have its own Pages, that is, the outermost pages Fields.
  • subPackages Cannot be the root directory of another subPackages Subdirectory within
  • tabBar Pages must be inside the main package

# Citation principle

  • packageA unable Require packageB JS File, but can be Require Primary Package,packageA Inner JS fileuse Subcontract asynchrony Not subject to this restriction
  • packageA unable import packageB of Template, but can be Require Primary Package,packageA Inner template
  • packageA Unable to use packageB Resources, but you can use the main package,packageA Resources within

# Low Version Compatible

Compiled by the WeChat background to deal with the compatibility of the old version of the client, the background will compile two code packages, one is the subcontracted code, the other is the whole package of compatible code. The new client uses sub-packageing, the old client still uses the whole package, and the complete package will put each subPackages The path inside is placed pages In.

# Subcontract entry documents

In the configuration of each subcontract,entry Field can specify any one of the sub-packages JS File as the entry file, which will be executed first when sub-packageing injection.

Specified JS The file should fill in the path relative to the subpackage root directory, for example by specifying /path/to/subPackage/src/index.js As a subcontract /path/to/subPackage Of the entry documents, should be filled in src/index.js

Debugging this feature requires 1.06.2406242 Or above version of WeChat developer tools, the formal environment has no version requirements.

{% Minicode ('c5PkiKmv74S9') %}

# Sample Project

to download Mini Program example (subcontract loaded version) source code