Disclosure of Invention
The invention mainly aims to provide an applet generating method, device, equipment and computer readable storage medium, and aims to solve the problem that codes of third party applets are difficult to maintain when the third party applets are accessed in the prior art.
In order to achieve the above object, the present invention provides an applet generation method comprising:
Acquiring a third-party global configuration file, and storing the third-party global configuration file into a preset directory;
Reading first target field information of the third-party global configuration file, obtaining a target page, and copying a page file corresponding to the target page from a source code catalog to the preset catalog;
Analyzing dependent resources of the page file based on the page file and a preset rule, and acquiring paths of the dependent resources;
and acquiring the source codes of the dependent resources from the source code catalog according to the paths of the dependent resources, and copying the source codes to the preset catalog to generate a third-party applet.
Optionally, the page file includes a configuration file, a logic file, a template file and a style file, and the step of analyzing the dependent resource of the page file based on the page file and a preset rule and acquiring the path of the dependent resource includes:
Acquiring a path of the component from the configuration file according to a preset target parameter;
Acquiring path information of the dependent resources contained in the logic file, the template file and the style file;
and obtaining a resource-dependent path corresponding to the path information according to the path information.
Optionally, the step of obtaining the path information of the dependent resource contained in the logic file, the template file and the style file includes:
Respectively converting source codes of the logic file, the template file and the style file into abstract syntax trees through a preset tool;
traversing each node of the abstract syntax tree by adopting a depth-first search algorithm, and reading metadata of each node, wherein the metadata comprises path information.
Optionally, the step of obtaining the path information of the dependent resource contained in the logic file, the template file and the style file includes:
And respectively matching the source codes of the logic file, the template file and the style file by adopting a preset regular expression, and obtaining path information according to a matching result.
Optionally, the step of obtaining the resource-dependent path corresponding to the path information according to the path information includes:
detecting whether the path information meets a preset condition or not;
If the path information meets the preset condition, determining the path which is dependent on the resource and corresponds to the path information as a preset path;
If the path information does not meet the preset condition, judging whether the path type corresponding to the path information is an absolute path or a relative path;
If the path type corresponding to the path information is an absolute path, determining the path dependent on the resource corresponding to the path information as the path corresponding to the path information;
And if the path type corresponding to the path information is a relative path, acquiring a relative path corresponding to the path information, and obtaining a corresponding path depending on resources according to the relative path and the path information.
Optionally, before the step of obtaining the source code of the dependent resource from the source code directory according to the path of the dependent resource and copying the source code to the preset directory to generate the third party applet, the method further includes:
Reading second target field information of the third-party global configuration file, and reading a target field value in the second target field information to obtain a path of the icon picture;
And acquiring a corresponding icon picture according to the path of the icon picture, and copying the icon picture to the preset directory.
Optionally, the step of copying the page file corresponding to the target page from the source code directory to the preset directory includes:
Acquiring a page file corresponding to the target page from a source code catalog, compiling preset specified parameters in the page file into a third party identification number ID, and copying the compiled page file to the preset catalog;
the step of obtaining the source code of the dependent resource from the source code catalog according to the path of the dependent resource and copying the source code to the preset catalog comprises the following steps:
And acquiring the source codes of the dependent resources from the source code catalog according to the paths of the dependent resources, compiling the preset specified parameters in the source codes of the dependent resources into the third party ID, and copying the compiled source codes of the dependent resources to the preset catalog.
In addition, in order to achieve the above object, the present invention also provides an applet generating apparatus comprising:
the file acquisition module is used for acquiring a third-party global configuration file and storing the third-party global configuration file into a preset directory;
The first copying module is used for reading first target field information of the third-party global configuration file, obtaining a target page, and copying a page file corresponding to the target page from a source code catalog to the preset catalog;
The path acquisition module is used for analyzing the dependent resources of the page file based on the page file and a preset rule and acquiring paths of the dependent resources;
The second copying module is used for acquiring the source codes of the dependent resources from the source code catalog according to the paths of the dependent resources and copying the source codes to the preset catalog to generate a third-party applet.
In addition, in order to achieve the above object, the present invention also provides an applet generating apparatus comprising a memory, a processor and an applet generating program stored on the memory and executable on the processor, the applet generating program implementing the steps of the applet generating method as described above when being executed by the processor.
In addition, in order to achieve the above object, the present invention also provides a computer-readable storage medium having stored thereon an applet generating program which, when executed by a processor, implements the steps of the applet generating method as described above.
The invention provides a method, a device, equipment and a computer readable storage medium for generating an applet, which are used for acquiring a third-party global configuration file and storing the third-party global configuration file under a preset directory; the method comprises the steps of reading first target field information of a third-party global configuration file, obtaining a target page, copying a page file corresponding to the target page to a preset directory from a source code directory, analyzing dependent resources of the page file based on the page file and a preset rule, obtaining a path of the dependent resources, obtaining source codes of the dependent resources from the source code directory according to the path of the dependent resources, and copying the source codes to the preset directory to generate a third-party applet. By the method, the performance and experience of the third-party applet can be ensured by acquiring the global configuration file (namely the third-party global configuration file) of the third-party applet and generating the application of the minimum code amount according to the third-party global configuration file and meeting the applet corresponding to the third party. Meanwhile, by means of configuration, the third-party applet can be updated only by modifying the third-party global configuration file, so that maintenance of the third-party applet codes can be facilitated.
Detailed Description
It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the invention.
Referring to fig. 1, fig. 1 is a schematic device structure diagram of a hardware running environment according to an embodiment of the present invention.
The applet generating device in the embodiment of the invention can be a smart phone or terminal devices such as a PC (Personal Computer ), a tablet personal computer, a portable computer and the like.
As shown in fig. 1, the applet generating device may comprise a processor 1001, e.g. a CPU, a communication bus 1002, a user interface 1003, a network interface 1004, a memory 1005. Wherein the communication bus 1002 is used to enable connected communication between these components. The user interface 1003 may include a Display, an input unit such as a Keyboard (Keyboard), and the optional user interface 1003 may further include a standard wired interface, a wireless interface. The network interface 1004 may optionally include a standard wired interface, a wireless interface (e.g., wi-Fi interface). The memory 1005 may be a high-speed RAM memory or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 may also optionally be a storage device separate from the processor 1001 described above.
It will be appreciated by those skilled in the art that the applet generating device structure shown in fig. 1 does not constitute a limitation of the applet generating device, and may comprise more or fewer components than shown, or may combine certain components, or may be arranged in different components.
As shown in fig. 1, an operating system, a network communication module, a user interface module, and an applet generation program may be included in a memory 1005 as one type of computer storage medium.
In the terminal shown in fig. 1, the network interface 1004 is mainly used for connecting to a background server and performing data communication with the background server, the user interface 1003 is mainly used for connecting to a client and performing data communication with the client, and the processor 1001 may be used for calling an applet generating program stored in the memory 1005 and performing the steps of the applet generating method below.
Based on the above hardware structure, various embodiments of the applet generation method of the present invention are presented.
The invention provides an applet generation method.
Referring to fig. 2, fig. 2 is a flowchart illustrating a first embodiment of an applet generation method according to the present invention.
In this embodiment, the applet generation method includes:
step S10, a third-party global configuration file is obtained, and the third-party global configuration file is stored under a preset directory;
The applet generating method of the present embodiment is implemented by an applet generating apparatus, which is described as an example of a server. In this embodiment, when a third party wants to access, a global configuration file may be maintained in a code base for the accessed third party according to the requirement of the third party, and is recorded as the third party global configuration file, specifically, the server obtains the third party global configuration file, and stores the third party global configuration file under a preset directory, taking a WeChat applet as an example, the preset directory may be a dist directory (i.e. a code directory of the WeChat server), and for the command of the third party global configuration file, the command needs to be named app. Json to be identified by WeChat. In addition, a plurality of third party accesses exist, a plurality of corresponding running code packages can be correspondingly set under the dist directory, and the configuration file, the subsequent page file and the source code depending on the resource are copied into the corresponding running code packages.
Step S20, reading first target field information of the third-party global configuration file, obtaining a target page, and copying a page file corresponding to the target page from a source code catalog to the preset catalog;
and reading first target field information of the third-party global configuration file to obtain a target page, wherein the first target field information is information corresponding to a page field, and in the WeChat applet, the page field is used for designating which pages the applet consists of, and each item corresponds to path (including file name) information of one page, so that the target page (namely the page of the third-party applet) can be obtained by reading the first target field information. And then copying the page file corresponding to the target page from the source code directory to a preset directory, namely, a dist directory. The page files comprise configuration files, logic files, template files and style files.
Step S30, analyzing the dependent resources of the page file based on the page file and a preset rule, and acquiring paths of the dependent resources;
Then, the dependent resources of the page file are analyzed based on the page file and preset rules, and paths of the dependent resources are obtained. The dependent resources of the page file include code dependence and static resource dependence, the code dependence includes an applet custom component, locally extracted and multiplexed code, npm (node PACKAGE MANAGER, node packet manager) installed dependent packets, and the static resource dependence includes pictures, pdf (file format) files, audio and video files, and the like.
In this embodiment, the obtaining mode of the dependent resource path includes two types, specifically, the first type is to obtain the path of the component from the configuration file according to the preset target parameter, and the second type is to obtain the path of the dependent resource corresponding to the path information by locally extracting the multiplexed code in the dependent resource, the dependent package installed by npm and the path of the picture, and the second type is to obtain the path information by 1) converting the source codes of the logic file, the template file and the pattern file into abstract syntax tree through a preset tool, then traversing each node of the abstract syntax tree through a depth-first search algorithm, reading metadata of each node, including the path information, further obtaining the path of the dependent resource corresponding to the path information according to the path information, and 2) obtaining the path information through a regular expression matching method, namely, matching the preset regular expression with the source codes of the logic file, the template file and the pattern file respectively, obtaining the path information according to the matching result, and obtaining the path of the dependent resource corresponding to the path information according to the path information. The specific acquisition process may refer to the following embodiments, and will not be described herein.
And S40, acquiring the source codes of the dependent resources from the source code catalog according to the paths of the dependent resources, and copying the source codes to the preset catalog to generate a third-party applet.
And finally, acquiring the source codes of the dependent resources from the source code catalog according to the paths of the dependent resources, and copying the source codes to a preset catalog to generate the third-party applet.
Further, before the step S40, the applet generating method further includes:
Reading second target field information of the third-party global configuration file, and reading a target field value in the second target field information to obtain a path of the icon picture;
And acquiring a corresponding icon picture according to the path of the icon picture, and copying the icon picture to the preset directory.
In this embodiment, since the tabBar fields under the third-party global configuration file may also have pictures, corresponding second target field information of the third-party global configuration file may be read, and a target field value in the second target field information may be read to obtain a path of the icon picture, where the second target field information is tabBar (tab) field information, and if the applet is a multi-tab application (a tab (tab) column is located at the bottom or top of the client window and can be switched to a page), the representation of the tab column may be specified by the tabBar configuration item, and the corresponding page displayed during tab switching, and the target field value is a field value of the icon path, that is iconpath (icon path). Then, after the path of the icon picture is obtained, the corresponding icon picture is obtained according to the path of the icon picture, and is copied to a preset directory, namely, a dist directory.
The embodiment of the invention provides an applet generation method, which comprises the steps of obtaining a third-party global configuration file, storing the third-party global configuration file under a preset catalog, reading first target field information of the third-party global configuration file, obtaining a target page, copying a page file corresponding to the target page under the preset catalog from a source code catalog, analyzing dependent resources of the page file based on the page file and preset rules, obtaining a path of the dependent resources, obtaining source codes of the dependent resources from the source code catalog according to the path of the dependent resources, and copying the source codes of the dependent resources under the preset catalog to generate the applet of the third-party. By the method, the performance and experience of the third-party applet can be ensured by acquiring the global configuration file (namely the third-party global configuration file) of the third-party applet and generating the application of the minimum code amount according to the third-party global configuration file and meeting the applet corresponding to the third party. Meanwhile, by means of configuration, the third-party applet can be updated only by modifying the third-party global configuration file, so that maintenance of the third-party applet codes can be facilitated.
Specifically, in the first embodiment, the page file includes a configuration file, a logic file, a template file, and a style file, and step S40 includes:
Step a1, acquiring a path of a component from the configuration file according to preset target parameters;
For obtaining the component path, the path of the component can be directly obtained from the configuration file according to the preset target parameter, wherein the preset target parameter is using Components, and the using Components field of the configuration file is directly read, so that the path of the component can be obtained.
Or the step S40 includes:
Step a2, obtaining path information of the dependent resources contained in the logic file, the template file and the style file;
Then, path information of the dependent resource contained in the logical file, the template file, and the style file is acquired. The dependent resources contained in these three types of files have locally extracted multiplexed code, npm installed dependent packets and pictures.
As one embodiment, step a2 may include:
Step a21, converting source codes of the logic file, the template file and the style file into abstract syntax trees through a preset tool respectively;
Step a22, traversing each node of the abstract syntax tree by adopting a depth-first search algorithm, and reading metadata of each node, wherein the metadata comprises path information.
In this embodiment, the source codes of the logic file, the template file and the style file may be respectively converted into abstract syntax trees (Abstract Syntax Tree, AST) by a preset tool, where the preset tool may be babel (a JavaScript compiler), and of course, in a specific embodiment, other manners may be used to convert the source codes of the logic file, the template file and the style file into abstract syntax trees.
And traversing each node of the abstract syntax tree by adopting a depth-first search algorithm, and reading metadata of each node, wherein the metadata comprises path information. Specifically, when the preset tool is babel, the AST tree may be traversed by using babel-reverse, and the corresponding metadata may be read through the mounted metadataVisitor. For example, a tool library, i.e. "import { add } from"/util. Js "", is introduced into the page code by means of the es6 module, and after being converted into an AST tree by means of the babylon interpreter, the statement corresponds to a node of type ImportDeclaration (import declaration) on the AST tree, and the node contains all metadata of the statement. And the metadata field source in the node represents the path information of the tool library file, and the path information is extracted to be used as the basis for searching the corresponding code file.
It should be noted that the depth-first search (DEPTH FIRST SEARCH, DFS) algorithm belongs to one of the graph algorithms, and is a traversal algorithm for graphs and trees, and the process is briefly described as going deep enough for each possible branch path to no further deep, and each node can only access once. When the abstract syntax tree has more nodes, memory overflow is easy to occur by other methods, and the depth-first search algorithm is adopted to traverse all the nodes of the abstract syntax tree, so that the memory consumption can be reduced, and the memory overflow is avoided.
As another embodiment, step a2 may further include:
And a step a23 of respectively matching the source codes of the logic file, the template file and the style file by adopting a preset regular expression, and obtaining path information according to a matching result.
In this embodiment, path information may also be obtained by a regular expression matching method, specifically, a preset regular expression may be adopted to match source codes of a logic file, a template file and a style file respectively, and path information may be obtained according to a matching result. For example, for the above example, a tool library is introduced in the page code by means of an es6 module introduction, i.e. "import { add } from"./ util.js ";" regular expressions can be used to extract the es6 module introduction statement from the source code: ". The regular expression corresponds to all modes of the statement grammar introduced by the es6 module, so that the statement can be identified from the source code no matter what standard grammar a developer writes. After the es6 sentence in the code is taken, the path information behind the from keyword can be obtained through simple string operation.
And a step a3, obtaining a resource-dependent path corresponding to the path information according to the path information.
After the path information is acquired, a path which depends on resources and corresponds to the path information is acquired according to the path information. Specifically, step a3 includes:
Step a31, detecting whether the path information meets a preset condition;
step a32, if the path information meets the preset condition, determining the path which is dependent on the resource and corresponds to the path information as a preset path;
in this embodiment, whether the path information satisfies a preset condition is detected, where the preset condition is that the path information does not include/, and if the path information does not include/, that is, the preset condition is satisfied, it is determined that the path dependent on the resource corresponding to the path information is a preset path, for example, for the following two statements:
import{Page}from'wxa';
import{Page}from'./wxa';
The from of the above two sentences corresponds to path information, the first path information is none/is indicated by a dependent tool installed through npm package management tools, and the path installed by the tool defaults to a preset path, for example, a node_ modules directory under the project root directory. And the second path information is/is provided, which indicates that the tool is a self-writing tool, and the preset condition is not met, and the path is further determined according to the path information.
Step a33, judging whether the path type corresponding to the path information is an absolute path or a relative path if the path information does not meet the preset condition;
Step a34, if the path type corresponding to the path information is an absolute path, determining the path dependent on the resource corresponding to the path information as the path corresponding to the path information;
And a step a35 of acquiring a relative path corresponding to the path information if the path type corresponding to the path information is a relative path, and acquiring a corresponding path depending on resources according to the relative path and the path information.
If the path information does not meet the preset condition, further judging whether the path type corresponding to the path information is an absolute path or a relative path. Specifically, whether the path information is an absolute path or a relative path can be determined according to the format of the path information,/represents a root directory and is an absolute path,/represents a current path,/represents a higher-layer directory, and belongs to a relative path.
If the path type corresponding to the path information is an absolute path, determining that the path of the dependent resource corresponding to the path information is the path corresponding to the path information, for example, the obtained path information is/uteil.js, and if the path type corresponding to the path information is the absolute path, the corresponding dependent resource can be directly obtained according to the path.
And if the path type corresponding to the path information is a relative path, acquiring the relative path corresponding to the path information, and acquiring a corresponding path depending on the resource according to the relative path and the path information. For example, if the path information is./ util. Js, a corresponding relative path, e.g.,/pages/index, is acquired, and then the two paths are combined to obtain a corresponding resource-dependent path, e.g.,/pages/index/util. Js.
Further, based on the first embodiment shown in fig. 2, a second embodiment of the applet generating method of the present invention is proposed.
In this embodiment, the step of copying the page file corresponding to the target page from the source code directory to the preset directory includes:
Acquiring a page file corresponding to the target page from a source code catalog, compiling preset specified parameters in the page file into a third party identification number ID, and copying the compiled page file to the preset catalog;
In this embodiment, when different third parties access, a third party ID (identification number) may be allocated to each merchant registration according to third party information (such as a name) or a preset rule, and in the copying process, the preset specified parameters are compiled into the third party ID, and then the compiled page file or source code is copied to a preset directory, so as to be used for distinguishing the third parties to which each generated third party applet belongs.
Specifically, after reading first target field information (pages) of a third-party global configuration file and acquiring a target page, the server acquires a page file corresponding to the target page from a source code catalog, compiles preset specified parameters in the page file into a third-party identification number ID, and copies the compiled page file to a preset catalog. The preset specified parameter is set according to the source code of the applet, for example, a protocol needs to be signed in the code, paths corresponding to the code are http:// bank.com/bank_appid/xieii.pdf respectively, and at this time, the preset specified parameter can be set as bank_appid. At compile time, bank_appid may be replaced with a third party ID.
After the compiled page file is copied to the preset directory, the dependent resource of the page file is analyzed based on the page file and the preset rule, and the path of the dependent resource is obtained, and the specific path obtaining method may refer to the above embodiment and will not be described herein.
At this time, the step of "obtaining the source code of the dependent resource from the source code directory according to the path of the dependent resource and copying the source code to the preset directory" includes:
And acquiring the source codes of the dependent resources from the source code catalog according to the paths of the dependent resources, compiling the preset specified parameters in the source codes of the dependent resources into the third party ID, and copying the compiled source codes of the dependent resources to the preset catalog.
After the path of the dependent resource is obtained, the source code of the dependent resource is obtained from the source code catalog according to the path of the dependent resource, then preset appointed parameters in the source code of the dependent resource are compiled into the third party ID, and the compiled source code of the dependent resource is copied to the preset catalog to generate the third party applet.
By means of the method, in the copying process, the preset specified parameters are compiled into the third party ID, and further the compiled page file or source code is copied to the preset directory, so that the generated third party applets can be distinguished from the third parties.
The invention also provides an applet generating device.
Referring to fig. 3, fig. 3 is a schematic functional block diagram of a first embodiment of the applet generating apparatus according to the invention.
As shown in fig. 3, the applet generating apparatus includes:
The file acquisition module 10 is configured to acquire a third-party global configuration file, and store the third-party global configuration file under a preset directory;
The first copying module 20 is configured to read first target field information of the third party global configuration file, obtain a target page, and copy a page file corresponding to the target page from a source code directory to the preset directory;
A path acquisition module 30, configured to analyze a dependent resource of the page file based on the page file and a preset rule, and acquire a path of the dependent resource;
The second copying module 40 is configured to obtain the source code of the dependent resource from the source code directory according to the path of the dependent resource, and copy the source code to the preset directory to generate a third party applet.
Further, the path acquisition module 30 includes:
The first acquisition unit is used for acquiring the path of the component from the configuration file according to preset target parameters;
A second obtaining unit, configured to obtain path information of the dependent resource included in the logic file, the template file, and the style file;
and a third acquisition unit, configured to obtain a resource-dependent path corresponding to the path information according to the path information.
Further, the second obtaining unit is specifically configured to convert source codes of the logic file, the template file and the style file into abstract syntax trees through a preset tool, traverse each node of the abstract syntax trees by using a depth-first search algorithm, and read metadata of each node, where the metadata includes path information.
Further, the second obtaining unit is further specifically configured to match the source codes of the logic file, the template file and the style file with a preset regular expression, and obtain path information according to a matching result.
The third obtaining unit is specifically configured to detect whether the path information meets a preset condition, determine that a path of a dependent resource corresponding to the path information is a preset path if the path information meets the preset condition, determine whether a path type corresponding to the path information is an absolute path or a relative path if the path information does not meet the preset condition, determine that the path of the dependent resource corresponding to the path information is the path corresponding to the path information if the path type corresponding to the path information is the absolute path, and obtain a relative path corresponding to the path information and obtain a path of the dependent resource according to the relative path and the path information if the path type corresponding to the path information is the relative path.
Further, the applet generating apparatus further includes:
the reading module is used for reading second target field information of the third-party global configuration file and reading a target field value in the second target field information to obtain a path of the icon picture;
And the third copying module is used for acquiring the corresponding icon picture according to the path of the icon picture and copying the icon picture to the preset directory.
Further, the first replication module 20 is specifically configured to:
Acquiring a page file corresponding to the target page from a source code catalog, compiling preset specified parameters in the page file into a third party identification number ID, and copying the compiled page file to the preset catalog;
further, the second replication module 40 is specifically configured to:
And acquiring the source codes of the dependent resources from the source code catalog according to the paths of the dependent resources, compiling the preset specified parameters in the source codes of the dependent resources into the third party ID, and copying the compiled source codes of the dependent resources to the preset catalog.
The function implementation of each module in the applet generating device corresponds to each step in the applet generating method embodiment, and the function and implementation process thereof are not described in detail herein.
The present invention also provides a computer-readable storage medium having stored thereon an applet generating program which, when executed by a processor, implements the steps of the applet generating method according to any one of the embodiments above.
The specific embodiments of the computer readable storage medium of the present invention are substantially the same as the embodiments of the applet generating method described above, and will not be described herein.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or system. Without further limitation, an element defined by the phrase "comprising one does not exclude the presence of other like elements in a process, method, article, or system that comprises the element.
The foregoing embodiment numbers of the present invention are merely for the purpose of description, and do not represent the advantages or disadvantages of the embodiments.
From the above description of the embodiments, it will be clear to those skilled in the art that the above-described embodiment method may be implemented by means of software plus a necessary general hardware platform, but of course may also be implemented by means of hardware, but in many cases the former is a preferred embodiment. Based on such understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art in the form of a software product stored in a storage medium (e.g. ROM/RAM, magnetic disk, optical disk) as described above, comprising instructions for causing a terminal device (which may be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to perform the method according to the embodiments of the present invention.
The foregoing description is only of the preferred embodiments of the present invention, and is not intended to limit the scope of the invention, but rather is intended to cover any equivalents of the structures or equivalent processes disclosed herein or in the alternative, which may be employed directly or indirectly in other related arts.