Skip to content

Basic Library Introduction

Basic Library Introduction

The basic library is the middle layer responsible for loading the mini program. There are many pre-packaged capabilities of the app and the mobile phone (such as Bluetooth, NFC, etc.) in the mini program basic library. Therefore, developers can call related capabilities through standard components and standard API interfaces to develop mini programs.

The basic library can be compared to common libraries such as React and Lodash in web development; but it is more special because the capabilities of the mini program need to be supported by the SDK, so:

  • The basic library has its own version number like the host app;
  • The new capabilities of each version of the basic library need to run in a specific version;
  • Some capabilities of the higher version of the basic library are not compatible with the lower version of the SDK.
Commonly, the basic library provides two parts: components and APIs for mini program developers to use related capabilities.
  • After being compiled, the wxml files in the mini program can render real DOM nodes in the environment provided by the basic library;

  • The Page, Component and other interfaces used in the JS files in the mini program are also exposed to the world by the basic library;

  • The wx.xxxAPI used in the mini program actually calls the native capabilities (Bluetooth, wifi, etc.) and the basic library is the "bridge" between the mini program and the native container.

    In short, the basic library is mainly used to handle data binding, provide a series of framework logics such as component system, event system, and communication system. From the perspective of the dual-process model, it includes both the rendering problem of the Virtual DOM of the rendering layer and the built-in components and APIs of the logic layer. In addition, the basic library also provides some supplementary capabilities for mini programs, such as custom components and performance compatibility.

js
const { SDKVersion } = wx.getSystemInfoSync(); // fetch jsLib version

Basic library mechanism

When developing web pages, we often reference third-party JavaScript libraries, and before using the APIs provided in these libraries, we need to introduce these third-party libraries in the business code in advance.Similarly, when opening a mini program in the App, we also need to load the basic library before starting the mini program, and then load the business code. Since the rendering layer and logic layer of the mini program are managed by two threads, when we generally talk about the basic library, it usually includes the WebView basic library (rendering layer) and the AppService basic library (logic layer).

Before opening all mini programs based on Luffa Cloud, the same basic library needs to be injected, so we do not need to package the basic library in the code package of the mini program, but need to integrate the Luffa Cloud mini program SDK in the App and introduce the mini program basic library through the SDK. There are two benefits of introducing the basic library through the SDK:

It can reduce the size of the mini program code package;

You can add capabilities to the basic library alone, or fix bugs, without modifying the code of the mini program.

Mini program startup

When we open the mini program, we will see a loading page including the mini program icon and name. In this page, the download of the basic library and code package of the mini program will be completed first. After the download of the basic library and code package is completed, JSCode and WebView will be initialized respectively. After the basic library, the mini program code package and WebView are injected, the relevant code of the mini program will be initialized, and the final user can see the complete mini program code.