Applet base load capacity - Subcontracting

(1) sub-Load

Micro-channel client 6.6.0, the underlying library  1.7.3  and later began to support. Please use the developer tools 1.01.1712150 and above, you can click here to download .

    In some cases, an applet developers need to be divided into different sub-packets, sub-packaged into different when building, loading a user on demand during use.

    When building a small sub-projects program, will build the output of one or more subcontractors. Each applet must contain subcontractors using a main package . The so-called main package, which placed the default start page / TabBar page, as well as some all subcontractors are required to public resources / JS script; and subcontracting is divided according to the configuration of the developer.

    When the applet is started, the default will download the package and start the main page in the main pack, when the user enters a page within subcontracting, the client will download the corresponding sub-contractors, on display after the download is complete.

    At present small size of the sub-program has the following limitations:

  • All subcontractors throughout the applet size of not more than 8M
  • Single sub / main package size can not exceed 2M

    Program for small subcontractors, we can optimize applet first start of download time, and in a multi-team to develop better coordination of decoupling.

    Please refer to the specific use:

 

(2) sub-

Configuration

Assume support of small subcontracting program directory structure is as follows:

├── app.js
├── app.json
├── app.wxss
├── packageA
│   └── pages
│       ├── cat
│       └── dog
├── packageB
│   └── pages
│       ├── apple
│       └── banana
├── pages
│   ├── index
│   └── logs
└── utils

Developers by app.json  subpackages field declarations subcontract structure:

Written subPackages also supported.

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

subpackages Each subcontractor have the following configuration:

Field Types of Explanation
root String Subcontracting root directory
name String Alias subcontracting, subcontracting pre-download can be used
pages StringArray Subcontracting page path relative to the root directory of subcontracting
independent Boolean Whether subcontracting is independent subcontractors

Package principles

  • Statement  subpackages After will  subpackages configure the packing path, subpackages directory path is disposed outside the package to the app (the main package) in
  • app (the primary packages) may have their own pages (i.e., pages outermost field)
  • subpackage The root directory can not be another  subpackage subdirectories within
  • tabBar Page must (main package) within the app

Reference principles

  • packageA Unable to The require  packageB JS files, but may The require  app, JS files in their own package
  • packageA You can not import  packageB the template, but The require  app, within their own package Template
  • packageA You can not use the  packageB resources, but you can use  appresources within their own package

Low-compatible version

By the micro-channel coding background to handle compatible with older versions of the client, the background will compile the code package two, one for the sub-codes, the other one is the whole package compatible code. The new client with subcontractors, or old client with the whole package, each package will complete  subpackage the inside path into the pages of.

 

(3) independent subcontractors

Micro-channel client 6.7.2, the underlying library  2.3.0  and later began to support. Please use the developer tools 1.02.1808300 and above, you can click here to download .

Independent sub applet is a special type of subcontract, the packet may be independent of the primary run and the other subcontractors. When sub-pages into the applet from independent, no need to download the main package. When a user enters the general subcontractor or main package page, the main package will be downloaded.

Developers can demand some pages with a certain functional independence configuration to independent subcontracting. When the program starts from the ordinary small sub-page, you need to first download the main package; and independent does not rely on subcontractors to run the main package, you can largely improve the boot speed sub-pages.

A small program can have multiple independent subcontractors.

Small game does not support independent subcontractors.

Configuration

Suppose a small directory structure as follows:

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

Developer by app.jsonthe subpackagessub-field configuration item defined in the corresponding independentfield declaration corresponding to an independent sub-sub.

{
  "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-contractors belonging to subcontracting. All restrictions are independent of general subcontracting subcontracting effective. Independent sub plugin, since treatment with the general definition of the sub-assembly.

In addition, the use of independent subcontractors to pay attention to:

  • Independent sub-packages and can not rely on the main contents of the other subcontractors , including js file, template, wxss, custom components, plug-ins. The main package app.wxssindependent the subcontractor should be avoided in a separate sub-page using  app.wxss the style;
  • App Can only be defined in the main pack, independent subcontractor can not be defined  App, it can cause unpredictable behavior;
  • Independent subcontracting does not support the use of plug-ins.

Precautions

① About getApp()

Unlike ordinary subcontracting, subcontracting independent operation, App not necessarily to be registered, and therefore  getApp() can not necessarily get  App the object:

  • When a user from a separate sub-page startup applet, the main package does not exist, Appdoes not exist, then call  getApp() get to that  undefined. When a user enters the general subcontractor or main package page, the main package will be downloaded, App will be registered.
  • When the user is within the ordinary jump from page subcontractors or independent subcontractor to the main package page, the main package already exists, then calls  getApp() can get to the real  App.

Because of this limitation, the developer can not pass through  App independence and subcontracting applet object sharing the rest of the global variables.

In order to meet this demand in a separate sub-contractors, the underlying library  2.2.4  version began to  getAppsupport [ allowDefault] parameters, in  App return undefined when a default implementation. When the primary package is loaded, App when it is registered, the default properties defined by the implementation will be merged into the real covered  App in.

Sample code:

  • Independent subcontracting
const app = getApp({allowDefault: true}) // {}
app.data = 456
app.global = {}
app.js 中
App({
  data: 123,
  other: 'hello'
})

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

② About  App Life Cycle

When the program started small independent sub-contractors from the main pack  App in  onLaunch and for the first time  onShow will be called when the sub-page for the first time into the main pack or other common sub-pages from independence.

Because independent subcontractor can not define  App, monitor applet lifecycle can use  wx.onAppShow , wx.onAppHide  complete. App Other events on the use  wx.onError , wx.onPageNotFound  monitor.

Low-compatible version

6.7.2 runs below the micro-channel version of the time, regarded as separate sub-sub-normal process, does not have the characteristics of independent operation.

Note: In compatibility mode, the primary package  app.wxss may have separate sub-pages impact, and therefore should be avoided in a separate sub-page  app.wxss in style.

 

(4) Subcontractor pre-download

Infrastructure Library 2.3.0 started to support low version to be made compatible with the process . Please use the developer tools 1.02.1808300 and above, you can click here to download .

Developers can configure the applet when entering a page, automatically download pre subcontracting may be required by the framework to improve the startup speed when entering a subsequent sub-page. For independent subcontractors , the main package may be pre-downloaded.

Subcontracting pre-download is currently only supported through the use of configuration, not support by calling API.

There vConsole preloadSubpackageslog information beginning, can be used to verify the situation pre-downloaded.

Configuration

Pre-download subcontracting behavior when entering a page is triggered by the  app.json increase  preloadRule to control configuration.

{
  "pages": ["pages/index"],
  "subpackages": [
    {
      "root": "important",
      "pages": ["index"],
    },
    {
      "root": "sub1",
      "pages": ["index"],
    },
    {
      "name": "hello",
      "root": "path/to",
      "pages": ["index"]
    },
    {
      "root": "sub3",
      "pages": ["index"]
    },
    {
      "root": "indep",
      "pages": ["index"],
      "independent": true
    }
  ],
  "preloadRule": {
    "pages/index": {
      "network": "all",
      "packages": ["important"]
    },
    "sub1/index": {
      "packages": ["hello", "sub3"]
    },
    "sub3/index": {
      "packages": ["path/to"]
    },
    "indep/index": {
      "packages": ["__APP__"]
    }
  }
}

preloadRule In key a Page path, value entering this pre-download the configuration page, each equipped with the following:

Field Types of Mandatory Defaults Explanation
packages StringArray Yes no Pre-download subcontracting enter the page  root or  name. __APP__It represents the main pack.
network String no wifi Pre-downloaded at a given network, optional value:
all: Any network 
wifi: only the pre-downloaded wifi

limit

The same sub-pages share a common pre-download size limit 2M, the limit will be packaged when the check in the tool.

E.g., page A and B are in the same sub, A is the total size of the pre-downloaded 0.5M sub A, B can only be pre-downloaded sub-total size up to 1.5M.

 

 

 

 

.

Guess you like

Origin www.cnblogs.com/jianxian/p/11109256.html