Applet code size reduction method and device, storage medium, and computer device

By automatically parsing and comparing global configuration files and project directories, redundant page directories and files are accurately identified and deleted, solving the problem of low efficiency in mini-program code optimization, achieving code size reduction and functional completeness, and improving the efficiency and quality of mini-program development.

CN121092182BActive Publication Date: 2026-02-13CHENGDU LUYI TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202511633020.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-11-10
Publication Date
2026-02-13
Estimated Expiration
2045-11-10

AI Technical Summary

Technical Problem

In the current process of optimizing mini-program code, relying on manual review is tedious, time-consuming, and prone to errors, resulting in low efficiency in code size optimization and difficulty in implementing rich functions and passing platform review within a limited space.

Method used

By automatically parsing the global configuration file, accurately identifying valid page paths, and comparing them with the project directory, redundant page directories and their files are deleted. Combined with intelligent comparison and fully automated processing, code quality and functional integrity are ensured.

Benefits of technology

Significantly reduce the size of the mini-program code, improve optimization efficiency, avoid human error, ensure code quality and functional integrity, and meet platform review requirements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121092182B_ABST
    Figure CN121092182B_ABST
Patent Text Reader

Abstract

The application relates to the technical field of program optimization, and particularly discloses a small program code volume reduction method and device, a storage medium and a computer device. The method comprises the following steps: in response to a small program code volume reduction instruction, determining a target small program to be subjected to volume reduction; acquiring a global configuration file of the target small program, and parsing declared valid page paths from the global configuration file; scanning an engineering directory corresponding to the target small program, and identifying a plurality of page directories contained in the engineering directory; comparing each page directory with the declared valid page paths respectively, so as to screen out redundant page directories not contained in the valid page paths from the plurality of page directories; and deleting the redundant page directories and all files under the redundant page directories, so as to obtain a target small program after code reduction.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of program optimization technology, and in particular to a method and apparatus for reducing the size of applet code, a storage medium, and a computer device. Background Technology

[0002] With the rapid development of the mobile internet, mini-programs, as an application format that can be used without downloading or installation, have penetrated various industries and become an important carrier connecting users and services due to their lightweight and easy-to-distribute characteristics. However, in order to ensure a smooth user experience, mini-program platforms generally impose strict limits on the size of the code package, such as a maximum size of 2MB. This limitation poses a serious challenge to developers in implementing rich functionality within a limited space, making code size optimization an indispensable key step before the mini-program is developed and launched.

[0003] Currently, developers typically rely on manual review and deletion when optimizing the code size of mini-programs. Specifically, this requires manually identifying and cleaning up redundant pages and useless resource files in each functional module. This process is not only tedious and time-consuming, but also highly dependent on the developer's experience and meticulousness. It is easy to accidentally delete valid code or fail to clean up thoroughly due to negligence, which can lead to online failures or failure to meet platform review requirements, seriously affecting the iteration efficiency and release quality of mini-programs. Summary of the Invention

[0004] In view of this, this application provides a method, apparatus, storage medium, and computer device for reducing the code size of a mini-program. The method automatically parses the global configuration file, accurately identifies declared valid page paths, and intelligently compares them with the project directory. This allows for the rapid and accurate location and deletion of all redundant page directories and their files. This approach not only significantly improves optimization efficiency but also effectively avoids human error through a fully automated processing flow. While ensuring code quality and functional integrity, it significantly and reliably reduces the code size of the mini-program.

[0005] According to one aspect of this application, a method for reducing the code size of a WeChat Mini Program is provided, comprising:

[0006] In response to the mini-program code size reduction instruction, determine the target mini-program to be reduced in size;

[0007] Obtain the global configuration file of the target mini-program, and parse out the declared valid page paths from the global configuration file;

[0008] Scan the project directory corresponding to the target mini-program and identify multiple page directories contained in the project directory;

[0009] compare each page directory with the declared valid page path respectively to filter out redundant page directories not contained in the valid page path from the plurality of page directories;

[0010] delete the redundant page directories and all files under the redundant page directories to obtain the target applet after code reduction.

[0011] According to another aspect of the present application, an applet code size reduction device is provided, comprising:

[0012] An instruction receiving module is configured to determine a target applet to be subjected to size reduction in response to an applet code size reduction instruction.

[0013] An analysis module is configured to obtain a global configuration file of the target applet and parse the declared valid page path from the global configuration file.

[0014] A scanning module is configured to scan an engineering directory corresponding to the target applet and identify a plurality of page directories contained in the engineering directory.

[0015] A comparison module is configured to compare each page directory with the declared valid page path respectively to filter out redundant page directories not contained in the valid page path from the plurality of page directories.

[0016] A deletion module is configured to delete the redundant page directories and all files under the redundant page directories to obtain the target applet after code reduction.

[0017] According to yet another aspect of the present application, a storage medium having a computer program stored thereon is provided, the program being executed by a processor to implement the above-mentioned applet code size reduction method.

[0018] According to still another aspect of the present application, a computer device is provided, comprising a storage medium, a processor, and a computer program stored on the storage medium and executable on the processor, the processor implementing the above-mentioned applet code size reduction method when executing the program.

[0019] By means of the above technical solutions, the present application provides an applet code size reduction method and device, a storage medium, and a computer device. The program automatically parses a global configuration file, accurately identifies a declared valid page path, and intelligently compares the engineering directory, thereby quickly and accurately locating and deleting all redundant page directories and files. This approach not only greatly improves the optimization efficiency, but also effectively avoids human errors through a fully automated processing flow, significantly and reliably reduces the code size of the applet while ensuring code quality and functional integrity.

[0020] The above description is only a summary of the technical solutions of the present application. In order to enable the technical means of the present application to be more clearly understood, and to be implemented according to the content of the description, and in order to enable the above and other purposes, characteristics and advantages of the present application to be more apparent and easy to understand, the following specific embodiments of the present application are described. BRIEF DESCRIPTION OF DRAWINGS

[0021] The accompanying drawings described herein are used to provide further understanding of the present application, and form a part of the present application. The schematic embodiments of the present application and the description thereof are used to explain the present application, and do not constitute an improper limitation on the present application. In the drawings:

[0022] Figure 1 A flowchart of a small program code size reduction method provided by an embodiment of the present application is shown;

[0023] Figure 2 A structural schematic diagram of a small program code size reduction device provided by an embodiment of the present application is shown;

[0024] Figure 3 A device structure schematic diagram of a computer device provided by an embodiment of the present application is shown. DETAILED DESCRIPTION

[0025] In the following, the present application will be described in detail with reference to the drawings and in combination with the embodiments. It should be noted that the embodiments in the present application and the features in the embodiments can be combined with each other without conflict.

[0026] In the present embodiment, a small program code size reduction method is provided, as shown in Figure 1 The method comprises the following steps:

[0027] Step 101, in response to a small program code size reduction instruction, a target small program to be subjected to size reduction is determined.

[0028] Step 102, a global configuration file of the target small program is acquired, and declared valid page paths are parsed from the global configuration file.

[0029] Step 103, an engineering directory corresponding to the target small program is scanned, and a plurality of page directories contained in the engineering directory are identified.

[0030] Step 104, each page directory is compared with the declared valid page paths respectively, so as to filter out redundant page directories not contained in the valid page paths from the plurality of page directories.

[0031] Step 105, the redundant page directories and all files under the redundant page directories are deleted, and a target small program after code reduction is obtained.

[0032] The application embodiment provides a mini-program code volume reduction method, which can significantly and reliably reduce the code volume of the mini-program under the premise of ensuring the code quality and functional integrity.

[0033] Firstly, after receiving the mini-program code volume reduction instruction, the target mini-program to be processed can be determined in response to the instruction, so that subsequent operations are performed on the correct mini-program project. Then, the global configuration file of the target mini-program is obtained. The global configuration file can contain the core configuration information of the target mini-program, such as page path, window style, etc. By parsing the file, the declared valid page paths can be extracted. These valid page paths represent the pages actually needed to be loaded and run by the target mini-program, and are the key basis for judging whether the page is redundant. In a specific embodiment, the global configuration file can be app.json, and the valid page path can be declared in array form (such as the pages field). Then, the project directory corresponding to the target mini-program is scanned. The project directory is the actual storage location of the target mini-program code, which contains a plurality of page directories and other resource files. Through the scanning operation, the structure and content of the project directory can be comprehensively understood, providing basic data for subsequent comparison and screening. After obtaining the valid page path and the project directory, each page directory contained in the project directory can be compared with the declared valid page path, and it is identified which page directories in the project directory are redundant, i.e. not belonging to the pages corresponding to the declared valid page path in the global configuration file. Through the comparison one by one, the page directories that are actually not used by the target mini-program although they exist in the project directory can be accurately screened out. Finally, the redundant page directories and all the files under these directories are deleted, so that the redundant page directories and their related files no longer occupy the storage space, thereby reducing the code volume of the target mini-program. After the deletion operation, the target mini-program after code reduction is obtained, and the entire volume reduction process is completed.

[0034] By applying the technical solution of the embodiment, the global configuration file is automatically parsed by the program, the declared valid page path is accurately identified, and intelligent comparison is performed with the project directory, so that all redundant page directories and their files are quickly and accurately located and deleted. This method not only greatly improves the optimization efficiency, but also effectively avoids human errors through the fully automated processing process, significantly and reliably reduces the code volume of the mini-program under the premise of ensuring the code quality and functional integrity.

[0035] In the embodiments of the present application, after the step of deleting the redundant page directory and all files under the redundant page directory in step 105, the method further comprises: determining the WXSS files and WXML files under the remaining page directories; parsing all defined style selectors from the WXSS files and all referenced class names from the WXML files; comparing the style selectors with the referenced class names to screen out target style selectors that are not referenced by any class name; and deleting the target style selectors and their corresponding style declarations from the WXSS files to obtain the target applet after code reduction.

[0036] In this embodiment, after deleting the redundant page directory and all files thereunder, the remaining page directories can be further processed. Specifically, for the remaining page directories, first determine the WXSS files and WXML files under these page directories. The WXSS files are used to define the styles of the target applet, and the WXML files are used to describe the structure of the page. Then, parse all defined style selectors from the WXSS files. The style selector is a key identifier for determining which elements the style is applied to, and by parsing, all selector sets defined in the WXSS file can be obtained. At the same time, parse all referenced class names from the WXML files. The class name is a way of referencing styles in WXML, and parsing the referenced class names helps to understand which styles are used in the actual page. Then, compare the parsed style selectors with the referenced class names to find those style selectors that are defined in the WXSS file but are not referenced by the class names in the WXML file, i.e., target style selectors that are not referenced by any class name. That is, through the comparison operation, the useless style selectors can be accurately located. Finally, delete the screened target style selectors and their corresponding style declarations from the WXSS files. This operation further optimizes the code size of the target applet, because the unused styles no longer occupy storage space. After this series of processing, the target applet after code reduction is obtained again.

[0037] In the embodiments of the present application, after the step of deleting the redundant page directory and all files under the redundant page directory in step 105, the method further comprises: for each remaining page directory, determining a JS file under the page directory and a WXML file with the same name as the JS file; parsing all bound event handling function names from the WXML file with the same name, and determining the calling relationship between all functions in the JS file, and constructing a function call relationship graph corresponding to the JS file based on the calling relationship; identifying an isolated function from the JS file according to the event handling function name and the function call relationship graph, which is not referenced by the WXML file with the same name and is not called by any other function inside the JS file; and deleting the isolated function to obtain a target applet after code reduction.

[0038] In this embodiment, after deleting the redundant page directory and all files thereof, the remaining page directories can be further optimized. Specifically, for each remaining page directory, first determine the JS file under the page directory and the WXML file with the same name as the JS file. The JS file contains the logic code of the page, and the WXML file with the same name defines the structure of the page. Then, parse all bound event handling function names from the WXML file with the same name. In the WXML file, an event (such as a click event) can be bound to a corresponding function through a specific syntax, and parsing these binding relationships can obtain a set of event handling function names actually used by the page. At the same time, determine the calling relationship between all functions in the JS file, and construct a function call relationship graph corresponding to the JS file based on these calling relationships. The function call relationship graph can intuitively show the dependencies and calling paths between functions, which helps to identify unused functions subsequently. Then, according to the parsed event handling function names and the constructed function call relationship graph, identify an isolated function from the JS file, which is not referenced by the WXML file with the same name and is not called by any other function inside the JS file. By comparing the event handling function names and the function call relationship graph, those functions that are neither bound in the page structure nor called in the logic code can be found, and these functions are isolated functions. Finally, delete the identified isolated functions. This operation can reduce the code size of the JS file, because these isolated functions will not be actually executed, and deleting them will not affect the normal operation of the target applet. After this series of processing, the code structure of the target applet is further optimized.

[0039] In the embodiments of the present application, after step 101, the method further comprises: obtaining a plurality of resource files corresponding to the target applet; determining the reference information of each resource file respectively, regarding a resource file with reference information indicating null as a redundant resource file, and removing the redundant resource file from the plurality of resource files.

[0040] In this embodiment, after determining the target applet to be reduced in volume, the volume reduction of the target applet can also be performed by the following method. First, a plurality of resource files corresponding to the target applet are obtained. These resource files can include pictures, audios, videos, etc., which play a role of auxiliary display or functional support in the target applet. Then, the reference information of each resource file is determined respectively. The reference information can indicate the position and times of the resource file used in the program code of the target applet. By analyzing the reference path, function call, etc. in the code, the reference information of each resource file can be obtained. Then, the resource file with empty reference information is taken as a redundant resource file. That is, if the reference information of a resource file is empty, it means that it has no place to be used in the applet code, i.e. the resource file is redundant. Finally, the redundant resource file is removed from the plurality of resource files. This operation directly reduces the overall volume of the target applet, because it no longer contains those unused resource files. After this step, only the resource files actually needed in the target applet are retained, and the optimization of resource files is realized.

[0041] In the embodiments of the present application, optionally, after step 101, the method further includes: obtaining a JS file corresponding to the target applet; deleting a console log output statement from the JS file, and / or deleting comment information of source code from the JS file, and / or deleting a function definition in a function body which does not contain any executable statement from the JS file.

[0042] In this embodiment, after determining the target applet to be reduced in volume, the volume reduction of the target applet can also be performed by the following method. First, a JS file corresponding to the target applet is obtained. The JS file is the core part of the logic implementation of the target applet, containing various function definitions, event processing, etc. Optimizing it can effectively reduce the code volume.

[0043] Then, a console log output statement is deleted from the JS file. The console log output statement is usually used in the development and debugging stage to help developers view the information of the program running time, but after the applet is officially released, these statements no longer have any actual effect, and deleting them can reduce unnecessary code amount.

[0044] In addition, comment information of source code can also be deleted from the JS file. The comment information is added to facilitate the developer to understand the code, and has no any influence on the running of the applet, and deleting the comment can directly reduce the volume of the JS file.

[0045] In addition, function definitions without any executable statements in the function body can also be deleted from the JS file. Although such function definitions exist, they have no actual executable code in the function body and will not have any effect when the program is running. Deleting them can further reduce the size of the JS file and achieve code size reduction.

[0046] Through the series of processing on the JS file, the code structure of the target mini-program can be effectively optimized.

[0047] In the embodiments of the present application, optionally, the method further comprises: after performing the deleting operation, calculating a first code size of the target mini-program after deletion, and determining the relationship between the first code size and a first preset size threshold; if the first code size is greater than the first preset size threshold, obtaining all resource files corresponding to the target mini-program, and identifying a target resource file with a file size greater than a second preset size threshold from the all resource files; uploading the target resource file to a server and obtaining a corresponding network access address; replacing a local reference path of the target resource file in the code of the target mini-program with the network access address of the target resource file obtained from the server, and eliminating the target resource file from the code of the target mini-program.

[0048] In this embodiment, after each deleting operation, a series of subsequent processing can be performed to further optimize the code size of the target mini-program.

[0049] Specifically, after performing the deleting operation (such as deleting the redundant page directory and all files thereof, deleting the target style selector and the corresponding style declaration, deleting the isolated function, deleting the redundant resource file, deleting the console log output statement, deleting the comment information of the source code, deleting the function definition without any executable statement in the function body, etc.), the first code size of the target mini-program after deletion can be calculated first. This step is to accurately understand the code size of the current target mini-program, and to provide data support for subsequent judgment of whether the expected size is reached. Then, the relationship between the first code size and a first preset size threshold is determined. The first preset size threshold is a standard set in advance, which is used to measure whether the code size of the mini-program is too large. By comparison, it can be determined whether the current code size is within a reasonable range. For example, during the platform audit process, the code size of the mini-program is required to be less than 2MB, and therefore the first preset size threshold can be set to 2MB.

[0050] If the first code volume is greater than the first preset volume threshold, it indicates that the code volume of the target applet is still large after the previous deletion operation, and further optimization is needed. At this time, all resource files corresponding to the target applet are obtained. The resource files can include pictures, audios, etc., which occupy a certain space in the target applet and are potential objects for subsequent optimization. Then, the target resource files with a file volume greater than a second preset volume threshold are identified from all the resource files. The second preset volume threshold is used to screen out resource files with large volumes, which have a greater impact on the overall volume of the target applet and need to be processed.

[0051] Next, the target resource files are uploaded to the server. Uploading to the server can realize centralized management of the resource files, and these resource files can be referenced through a network access address, avoiding storing large-volume resource files directly in the local target applet. After uploading is completed, the corresponding network access address is obtained, which is used to replace the local reference path in the subsequent process.

[0052] Finally, in the code of the target applet, the local reference path of the target resource file is replaced by the network access address for obtaining the target resource file from the server. In this way, the target applet can obtain these resource files from the server through the network during runtime, instead of loading them from the local. At the same time, the target resource files are removed from the code of the target applet, further reducing the local code volume of the target applet. Through this series of operations, the overall volume of the target applet is effectively reduced, and the performance and loading speed of the target applet are improved.

[0053] It should be noted that the above-mentioned methods of deleting redundant page directories and all files thereof, deleting target style selectors and corresponding style declarations, deleting isolated functions, deleting redundant resource files, deleting console log output statements, deleting comment information of source code, deleting function definitions in function bodies that do not contain any executable statements, and deleting target resource files can be used in any combination or individually, and are not limited herein.

[0054] In the embodiments of the present application, optionally, after the target resource file is removed from the code of the target applet, the method further comprises: calculating a second code volume of the target applet after the removal, and determining the relationship between the second code volume and the first preset volume threshold; if the second code volume is greater than the first preset volume threshold, determining a program entry page corresponding to the target applet, and global resources and self-owned resources of a main package, constructing the main package based on the program entry page, the global resources and the self-owned resources of the main package, and ensuring that the volume of the main package does not exceed the first preset volume threshold; based on the first preset volume threshold, dividing a remaining page directory and corresponding sub-package exclusive resource files into one or more sub-packages, wherein the volume of each sub-package does not exceed the first preset volume threshold; configuring package splitting information in a global configuration file of the target applet, and declaring the root directory path of each sub-package and the page path list contained therein in the package splitting information, wherein the sub-package exclusive resource files located under the root directory of each sub-package are automatically included in the corresponding sub-package during sub-package construction; and updating the page jump logic in the target applet to adapt to the path structure after the package splitting.

[0055] In this embodiment, after the target resource file is removed from the code of the target applet, the optimization and adjustment of the code volume can be continued.

[0056] First, the second code volume of the target applet after the removal is calculated. This step is to accurately grasp the actual code amount of the target applet after the resource file is uploaded to the server and the removal operation. Then, the relationship between the second code volume and the first preset volume threshold is determined. The first preset volume threshold serves as a standard for measuring whether the code volume of the applet meets the requirements, and through comparison, it can be determined whether the current code volume meets the expectations.

[0057] If the second code volume is greater than the first preset volume threshold, it means that even after uploading to the server and removing part of the resource files, the program code volume of the target applet is still too large, and more in-depth structural optimization is needed. At this time, the program entry page corresponding to the target applet, and the global resources and the self-owned resources of the main package are determined. The program entry page is the first page loaded when the target applet starts, the global resources are resources that can be used by the entire target applet, and the self-owned resources of the main package are resources closely related to the functions of the main package. Based on these key elements, the main package can be constructed, and the volume of the main package is ensured not to exceed the first preset volume threshold during construction. By reasonably dividing and selecting resources, it is ensured that the main package can contain necessary core content and meet the code volume requirement of the first preset volume threshold.

[0058] Then, based on the first preset volume threshold, the remaining page directories and corresponding subpackage exclusivity resource files are divided into one or more subpackages. Subpackage exclusivity resources refer to resources used only in a specific subpackage, which are divided into subpackages together with the corresponding page directories, helping to further disperse code volume. During the division process, it is strictly ensured that the volume of each subpackage does not exceed the first preset volume threshold, to ensure that each subpackage can be efficiently loaded and meet the code volume requirement of the first preset volume threshold.

[0059] Next, the split package information is configured in the global configuration file of the target small program. In the split package information, the root directory path of each subpackage and the page path list contained therein are declared, so that the target small program framework can correctly identify and load each subpackage at runtime. At the same time, the subpackage exclusivity resource files located under the root directory of each subpackage are automatically included in the corresponding subpackage during subpackage construction, without the need for additional manual configuration, simplifying the operation process.

[0060] Finally, the page jump logic in the target small program is updated to adapt to the path structure after splitting. Since the target small program has been split, the jump path between pages may have changed. By updating the jump logic, it is ensured that during the running of the target small program, the page jump can correctly point to the page path after splitting, ensuring that each function of the target small program operates normally. After this series of operations, the structure of the target small program is effectively optimized, so that each package meets the code volume requirement of the first preset volume threshold, thereby passing the audit.

[0061] In the embodiments of the present application, optionally, after step 105, the method further includes: constructing a program relationship graph of the target small program, wherein the nodes in the program relationship graph include functions, variables, components, pages, WXML tags, WXSS class names, and resource files, and the edges between the nodes are established based on calling relationships, inheritance relationships, reference relationships, and event binding relationships; inputting the program relationship graph into a pre-trained graph neural network model, generating an embedding representation containing global context information for each node based on the graph neural network model through a message passing mechanism, and predicting the probability that each node is a redundant node based on the embedding representation; identifying nodes with a probability exceeding a preset threshold as target redundant nodes, and deleting files or codes corresponding to the target redundant nodes.

[0062] In this embodiment, first, the source code of the target applet can be parsed to extract the key nodes and relationships therein. The node types include functions, variables, components, pages, WXML tags, WXSS class names, and resource files. By scanning the JavaScript / TypeScript code using static analysis tools (such as ESLint, Babel, etc.), function definitions, variable declarations, component imports, and other nodes can be identified; by parsing the WXML file, the tag structure and event bindings (such as bindtap) can be extracted; by the WXSS file, the class name selectors can be extracted; and the resource files (such as images, JSON configurations) can be included in the graph through file path reference relationships. The edges are established based on the following relationships: call relationships (functions calling other functions or component methods), inheritance relationships (components inheriting base classes), reference relationships (variables being used by functions, pages referencing components, WXSS class names being applied in WXML), and event binding relationships (WXML tag binding event handling functions). Finally, a directed graph is generated, with the nodes being the above entities and the edges being the relationship types and directions.

[0063] Next, the constructed program relationship graph is converted into a format that can be processed by a graph neural network (GNN). Specifically, the graph structure can be represented using an adjacency matrix or an edge list, and the node features can be initialized using one-hot encoding or pre-trained embeddings (such as Word2Vec encoding for variable names). Pre-trained GNN models (such as GCN, GAT, or GraphSAGE) have been trained on large-scale code corpora and can understand code semantics and structural patterns. The model iteratively updates the node embeddings through a message passing mechanism: each node aggregates the information of its neighbor nodes (such as through weighted averaging or attention mechanisms) and generates a new embedding representation by combining its own features. This process is repeated multiple times (such as 2-5 layers), allowing the node embeddings to gradually include global context information (such as the role of a function in the call chain, the reuse of a component).

[0064] In the last layer of the GNN, the embedding representation of each node is input into a classifier (such as a fully connected layer + Softmax) to predict the probability of it being a redundant node. The training of the classifier requires labeled data: redundant code in the applet (such as unused variables, uncalled functions, duplicate WXSS class names, etc.) is marked manually or using heuristic rules to construct a positive and negative sample set. During model training, the cross-entropy loss is minimized to make the probability of redundant nodes close to 1 and the probability of non-redundant nodes close to 0. In the inference phase, the model outputs probability values for all nodes in the graph, allowing it to generalize to new code without labeled data.

[0065] A probability threshold (e.g., 0.8) is set, and nodes with a probability exceeding the threshold are selected as target redundant nodes. It should be noted that the deletion of target redundant nodes needs to meet the security requirements: for example, the function needs to be confirmed to have no dynamic calls (such as through string concatenation calls) before deletion; the WXSS class name needs to be ensured to have no other style dependencies; and the component needs to be checked for page rendering logic. The reference count of the node can be verified through static analysis, or the function can be observed through dynamic analysis (such as mini-program test running) to ensure that the function is normal after deletion. Finally, the code file (such as an unused component file) or code segment (such as an unreferenced function or an unapplied WXSS class name) corresponding to the target redundant node is directly deleted, and the target mini-program is reconstructed to ensure that there is no compilation error.

[0066] After deleting the target redundant node, the program relationship graph can be reconstructed and input into the model to verify whether a new target redundant node is introduced (e.g., the caller of the deleted function becomes redundant). This process can be iteratively performed until a preset condition is met. Finally, the mini-program code after simplification is output.

[0067] In the embodiments of the present application, optionally, after step 105, the method further includes: obtaining the source code of the target mini-program after code reduction; inputting the source code into a pre-trained large language model, and performing deep semantic analysis on the source code by the large language model to generate a structured business specification corresponding to the source code, wherein the structured business specification is used to indicate the function modules included in the target mini-program, and the business logic description and the first code segment corresponding to each function module; identifying a matching second code segment from a preset mini-program code knowledge base based on the business logic description corresponding to each function module in the structured business specification, and comparing the code size of the second code segment with the first code segment; if there is a target second code segment with a code size smaller than the first code segment, reconstructing the first code segment based on the target second code segment with the smallest code size to obtain the reconstructed target mini-program.

[0068] In this embodiment, the source code of the target mini-program is first obtained. This can be achieved by exporting from a mini-program development tool, pulling from a version control system, or decompiling a published mini-program package. Next, the source code is input into a pre-trained large language model (such as CodeBERT or GPT). The large language model uses natural language processing techniques to parse the syntactic structure and semantic relationships of the source code, identifying elements such as functions, classes, and variables, and their interaction logic. Further, based on the semantic analysis results, the large language model maps the source code into a structured business specification. The structured business specification may include functional module divisions (such as login, payment, and data display), business logic descriptions for each module (such as "user clicks a button to trigger an API request"), and extracts key code snippets (the first code snippet) as examples. Then, based on the functional modules and business logic descriptions in the structured business specification, a pre-defined mini-program code knowledge base (containing open-source code, best practices, or historical optimization cases) can be queried. Second code snippets with similar functions but different implementations are found through semantic matching (such as vector similarity calculation). The volume of the matched second code snippet is compared with the original first code snippet. Code size can be measured by the number of lines, characters, or compressed bytes. Smaller code segments are prioritized to reduce redundancy. If a target second code segment exists with a significantly smaller size (e.g., a reduction of more than 20%), the refactoring process is triggered; otherwise, the original code (i.e., the first code segment) is retained. If the original first code segment is replaced with the target second code segment, interface compatibility can be further checked (e.g., consistent function parameters and return values). Specifically, after refactoring, overall performance and stability can be verified using static analysis tools (such as ESLint) and unit tests to ensure that the optimization has not introduced new defects.

[0069] This application embodiment decouples business logic from code implementation through semantic analysis and achieves intelligent code optimization by combining a preset mini-program code knowledge base. This can effectively reduce the size of the target mini-program code while reducing the cost of manual refactoring.

[0070] Furthermore, as Figure 1 In terms of specific implementation, this application provides a device for reducing the size of mini-program code, such as... Figure 2 As shown, the device includes:

[0071] The instruction receiving module is used to respond to the mini-program code size reduction instruction and determine the target mini-program to be reduced in size;

[0072] The parsing module is used to obtain the global configuration file of the target mini-program and parse out the declared valid page paths from the global configuration file;

[0073] The scanning module is configured to scan an engineering directory corresponding to the target applet, and identify a plurality of page directories contained in the engineering directory;

[0074] The comparison module is configured to compare each page directory with a declared valid page path respectively, so as to filter out redundant page directories not contained in the valid page path from the plurality of page directories;

[0075] The deleting module is configured to delete the redundant page directories and all files under the redundant page directories, so as to obtain the target applet after code reduction.

[0076] Optionally, the deleting module is further configured to:

[0077] After the redundant page directories and all files under the redundant page directories are deleted, for the remaining page directories, determine WXSS files and WXML files under the remaining page directories;

[0078] Parse all defined style selectors from the WXSS files, and parse all referenced class names from the WXML files;

[0079] Compare the style selectors with the referenced class names, and filter out target style selectors not referenced by any class name;

[0080] Delete the target style selectors and corresponding style declarations from the WXSS files, so as to obtain the target applet after code reduction.

[0081] Optionally, the deleting module is further configured to:

[0082] After the redundant page directories and all files under the redundant page directories are deleted, for each of the remaining page directories, determine a JS file under the page directory, and a WXML file with the same name as the JS file;

[0083] Parse all bound event handling function names from the WXML file with the same name, and determine a calling relationship between all functions in the JS file, and construct a function call relationship graph corresponding to the JS file based on the calling relationship;

[0084] According to the event handling function names and the function call relationship graph, identify isolated functions in the JS file that are not referenced by the WXML file with the same name and are not called by any other function in the JS file;

[0085] Delete the isolated functions, so as to obtain the target applet after code reduction.

[0086] Optionally, the deleting module is further configured to:

[0087] After determining the target applet to be subjected to volume reduction, the deleting module is further configured to acquire a plurality of resource files corresponding to the target applet;

[0088] The deleting module is further configured to determine reference information of each resource file respectively, and remove a redundant resource file from the plurality of resource files, the redundant resource file being indicated as empty by the reference information.

[0089] Optionally, the deleting module is further configured to:

[0090] After determining the target applet to be subjected to volume reduction, the deleting module is further configured to acquire a JS file corresponding to the target applet;

[0091] The deleting module is further configured to delete a console log output statement from the JS file, and / or delete comment information of source code from the JS file, and / or delete a function definition not containing any executable statement in a function body from the JS file.

[0092] Optionally, the deleting module is further configured to:

[0093] After performing the deleting operation, the deleting module is further configured to calculate a first code volume of the target applet after deletion, and determine a relationship between the first code volume and a first preset volume threshold;

[0094] If the first code volume is greater than the first preset volume threshold, the deleting module is further configured to acquire all resource files corresponding to the target applet, and identify a target resource file with a file volume greater than a second preset volume threshold from the all resource files;

[0095] The deleting module is further configured to upload the target resource file to a server, and obtain a network access address corresponding to the target resource file;

[0096] The deleting module is further configured to replace, in code of the target applet, a local reference path of the target resource file with the network access address of the target resource file obtained from the server, and remove the target resource file from the code of the target applet.

[0097] Optionally, the apparatus further comprises a packaging module, and the packaging module is configured to:

[0098] After removing the target resource file from the code of the target applet, the packaging module is further configured to calculate a second code volume of the target applet after removal, and determine a relationship between the second code volume and the first preset volume threshold;

[0099] If the second code volume is greater than a first preset volume threshold, a program entry page corresponding to the target applet is determined, and global resources and self-owned resources of a main package are determined, a main package is constructed based on the program entry page, the global resources and the self-owned resources of the main package, and it is ensured that a volume of the main package does not exceed the first preset volume threshold;

[0100] Based on the first preset volume threshold, a remaining page directory and corresponding sub-package exclusive resource files are divided into one or more sub-packages, wherein a volume of each sub-package does not exceed the first preset volume threshold;

[0101] In a global configuration file of the target applet, package information is configured, and in the package information, a root directory path of each sub-package and a page path list contained by the sub-package are declared, wherein sub-package exclusive resource files located under the root directory of each sub-package are automatically contained in the corresponding sub-package during sub-package construction;

[0102] Page jump logic in the target applet is updated to adapt to a path structure after package.

[0103] It should be noted that other corresponding descriptions of the functional units involved in the applet code volume reduction device provided in the embodiments of the present application can be referred to the corresponding descriptions in the methods, which will not be described here. Figure 1 The method, which will not be described here.

[0104] The embodiments of the present application also provide a computer device, which can be a personal computer, a server, a network device, etc., as shown in the Figure 3 The computer device includes a bus, a processor, a memory and a communication interface, and can also include an input / output interface and a display device. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operating system and the computer program in the non-volatile storage medium to run. The database of the computer device is used to store location information. The network interface of the computer device is used to communicate with external terminals through network connection. The computer program is executed by the processor to implement the steps in the method embodiments.

[0105] Those skilled in the art can understand, Figure 3 that the structure shown in the

[0106] In an embodiment, a computer readable storage medium, which can be nonvolatile or volatile, storing a computer program is provided, and the computer program, when executed by a processor, implements the steps in the above method embodiments.

[0107] In an embodiment, a computer program product is provided, comprising a computer program, which, when executed by a processor, implements the steps in the above method embodiments.

[0108] It should be noted that the user information (including but not limited to user equipment information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the present application are all information and data authorized by the user or authorized by all parties.

[0109] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer readable storage medium, and when the computer program is executed, the processes of the above-mentioned embodiments of the methods can be included. Any reference to memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical storage, high-density embedded non-volatile memory, resistive memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration but not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The database involved in the embodiments provided in the present application can include at least one of a relational database and a non-relational database. The non-relational database can include a distributed database based on a block chain, etc., without being limited thereto. The processor involved in the embodiments provided in the present application can be a general-purpose processor, a central processing unit, a graphics processing unit, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., without being limited thereto.

[0110] Any combination of the technical features of the above embodiments can be made. In order to make the description simple, all possible combinations of the technical features in the above embodiments are not described, however, as long as the combination of the technical features does not exist, it should be considered as the scope of the present application.

[0111] The above embodiments only express several implementation manners of the present application, and the description is more specific and detailed, but it should not be understood as a limitation on the scope of the patent of the present application. It should be pointed out that for ordinary skilled in the art, without departing from the concept of the present application, a number of modifications and improvements can be made, which are all within the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the appended claims.

Claims

1. A method for reducing the code size of a WeChat Mini Program, characterized in that, include: In response to the mini-program code size reduction instruction, determine the target mini-program to be reduced in size; Obtain the global configuration file of the target mini-program, and parse out the declared valid page paths from the global configuration file; Scan the project directory corresponding to the target mini-program and identify multiple page directories contained in the project directory; Each page directory is compared with the declared valid page paths to filter out redundant page directories that are not included in the valid page paths from the multiple page directories; Delete the redundant page directory and all files under the redundant page directory to obtain the target mini-program with reduced code. The method further includes: Construct a program relationship graph for the target mini-program, wherein the nodes in the program relationship graph include functions, variables, components, pages, WXML tags, WXSS class names and resource files, and the edges between each node are established based on calling relationships, inheritance relationships, reference relationships and event binding relationships; The program relationship graph is input into a pre-trained graph neural network model. Based on the graph neural network model, an embedding representation containing global context information is generated for each node through a message passing mechanism. Based on the embedding representation, the probability that each node is a redundant node is predicted. Nodes with a probability exceeding a preset threshold are identified as target redundant nodes, and the files or code corresponding to the target redundant nodes are deleted.

2. The method according to claim 1, characterized in that, After deleting the redundant page directory and all files under the redundant page directory, the method further includes: For the remaining page directories, identify the WXSS and WXML files within those directories; Parse all defined style selectors from the WXSS file and all referenced class names from the WXML file; The style selector is compared with the referenced class name to filter out the target style selector that is not referenced by any class name; The target style selector and its corresponding style declaration are removed from the WXSS file to obtain the target mini-program with reduced code.

3. The method according to claim 1, characterized in that, After deleting the redundant page directory and all files under the redundant page directory, the method further includes: For each remaining page directory, identify the JS file in that page directory, and the WXML file with the same name as the JS file; Parse all bound event handling function names from the WXML file with the same name, and determine the call relationship between all functions in the JS file. Construct a function call relationship graph corresponding to the JS file based on the call relationship. Based on the event handling function name and the function call relationship diagram, identify isolated functions from the JS file that are not referenced by the WXML file with the same name and are not called by any other function within the JS file; Delete the isolated function to obtain the target mini-program with reduced code.

4. The method according to claim 1, characterized in that, After determining the target mini-program to be reduced in size, the method further includes: Obtain multiple resource files corresponding to the target mini-program; The reference information of each resource file is determined, and resource files with empty reference information are designated as redundant resource files. These redundant resource files are then removed from the list of resource files.

5. The method according to claim 1, characterized in that, After determining the target mini-program to be reduced in size, the method further includes: Obtain the JS file corresponding to the target mini-program; Remove console log output statements from the JS file, and / or remove source code comments from the JS file, and / or remove function definitions from the JS file whose function bodies do not contain any executable statements.

6. The method according to any one of claims 1 to 5, characterized in that, The method further includes: After performing the deletion operation, calculate the first code volume of the target mini-program after deletion, and determine the relationship between the first code volume and the first preset volume threshold. If the size of the first code is greater than the first preset size threshold, then all resource files corresponding to the target mini-program are obtained, and the target resource file whose file size is greater than the second preset size threshold is identified from all the resource files. Upload the target resource file to the server and obtain the corresponding network access address; In the code of the target mini-program, the local reference path of the target resource file is replaced with the network access address of the target resource file obtained from the server, and the target resource file is removed from the code of the target mini-program.

7. The method according to claim 6, characterized in that, After removing the target resource file from the code of the target mini-program, the method further includes: Calculate the second code volume of the target mini-program after removal, and determine the relationship between the second code volume and the first preset volume threshold; If the size of the second code is greater than the first preset size threshold, then the program entry page corresponding to the target mini-program, as well as global resources and main package resources, are determined. The main package is constructed based on the program entry page, the global resources and the main package resources, and the size of the main package is ensured not to exceed the first preset size threshold. Based on the first preset volume threshold, the remaining page directory and the corresponding sub-package exclusive resource files are divided into one or more sub-packages, wherein the volume of each sub-package does not exceed the first preset volume threshold. Configure subpackage information in the global configuration file of the target mini-program. The subpackage information declares the root directory path of each subpackage and the list of page paths it contains. The exclusive resource files of each subpackage located in the root directory of each subpackage are automatically included in the corresponding subpackage when the subpackage is built. Update the page navigation logic in the target mini-program to adapt to the path structure after subpackaging.

8. A device for reducing the size of WeChat Mini Program code, characterized in that, include: The instruction receiving module is used to respond to the mini-program code size reduction instruction and determine the target mini-program to be reduced in size; The parsing module is used to obtain the global configuration file of the target mini-program and parse out the declared valid page paths from the global configuration file; The scanning module is used to scan the project directory corresponding to the target mini-program and identify multiple page directories contained in the project directory; The comparison module is used to compare each page directory with the declared valid page paths, so as to filter out redundant page directories that are not included in the valid page paths from the multiple page directories. The deletion module is used to delete the redundant page directory and all files under the redundant page directory to obtain the target mini-program with reduced code. The mini-program code size reduction device is also used for: Construct a program relationship graph for the target mini-program, wherein the nodes in the program relationship graph include functions, variables, components, pages, WXML tags, WXSS class names and resource files, and the edges between each node are established based on calling relationships, inheritance relationships, reference relationships and event binding relationships; The program relationship graph is input into a pre-trained graph neural network model. Based on the graph neural network model, an embedding representation containing global context information is generated for each node through a message passing mechanism. Based on the embedding representation, the probability that each node is a redundant node is predicted. Nodes with a probability exceeding a preset threshold are identified as target redundant nodes, and the files or code corresponding to the target redundant nodes are deleted.

9. A storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the method of any one of claims 1 to 7.

10. A computer device, comprising a storage medium, a processor, and a computer program stored on the storage medium and executable on the processor, characterized in that, When the processor executes the computer program, it implements the method of any one of claims 1 to 7.

Citation Information

Patent Citations

  • Method and device for optimizing applet style file

    CN117193838A

  • APP applet subpackage method and device and medium

    CN118093042A

  • Picture resource processing method and system for application installation package

    CN118484229A