A JS file generation method, device, equipment and storage medium

By inserting a method collector into the Flutter framework to collect and remove unused methods, the problem of excessively large Flutter web compilation artifacts is solved, improving website access speed and reducing manual labor costs.

CN116339747BActive Publication Date: 2025-12-12政采云股份有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310467586.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-04-25
Publication Date
2025-12-12
Estimated Expiration
2043-04-25

AI Technical Summary

Technical Problem

Currently, the Flutter web compilation output is too large, resulting in slow website access speed.

Method used

By inserting a pre-defined method collector into the Flutter framework, the methods and functions used during the execution of the initial JS file are collected, and unused methods and functions are removed based on the list of methods and functions to generate the target dill file, and finally the target JS file.

Benefits of technology

It effectively reduced the size of JS files, improved website access speed, and reduced labor costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116339747B_ABST
    Figure CN116339747B_ABST
Patent Text Reader

Abstract

The application discloses a JS file generation method and device, equipment and storage medium, relate to computer technical field, including: through the Flutter framework, the source code is compiled to obtain the initial dill file, the preset method collector is inserted in the initial dill file by using preset abstract syntax tree compiler to obtain modified dill file;After the modified dill file is compiled into initial JS file by the Flutter framework, the preset browser is started and the initial JS file is run, after the preset browser runs, the method function to be removed in the initial dill file is determined based on the preset method function list and is removed to obtain target dill file;The target dill file is compiled by the Flutter framework to obtain target JS file. In this way, the size of the obtained target JS file is greatly reduced.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer, in particular to a JS file generation method and device, equipment and storage medium. BACKGROUND

[0002] The Flutter framework can compile the code into JS code products allowed by the browser. However, at present, the Flutter web compilation product is too large, and even in the simplest demo case, the compilation product is nearly 1.4M. Therefore, how to reduce the size of the Flutter web compilation product is a problem to be solved. SUMMARY

[0003] Therefore, the purpose of the present application is to provide a JS file generation method and device, equipment and storage medium, which reduces the size of the JS file product and improves the website access speed. The specific scheme is as follows:

[0004] In a first aspect, the present application discloses a JS file generation method, comprising:

[0005] Obtaining an initial dill file obtained by compiling source code through a Flutter framework, and inserting a preset method collector into the initial dill file by using a preset abstract syntax tree compiler to obtain a modified dill file;

[0006] After the modified dill file is compiled into an initial JS file through the Flutter framework, a preset browser is started and the initial JS file is run through a preset automation script, so that the preset method collector collects the method functions used in the running process of the initial JS file and stores them in a preset method function list;

[0007] After the preset browser is run, the initial dill file is removed based on the preset method function list to obtain a target dill file;

[0008] The target dill file is compiled through the Flutter framework to obtain a target JS file.

[0009] Optionally, before the initial dill file is inserted into the preset method collector by using the preset abstract syntax tree compiler to obtain the modified dill file, it further comprises:

[0010] Defining a global container list and a method collection function to obtain the preset method collector.

[0011] Optionally, the inserting of the preset method collector into the initial dill file by the preset abstract syntax tree compiler to obtain a modified dill file comprises:

[0012] The preset method collector is obtained by the preset abstract syntax tree compiler, and the preset method collector is injected into all method nodes of the initial dill file respectively to obtain a modified dill file.

[0013] Optionally, after the initial JS file is compiled from the modified dill file by the Flutter framework, a preset browser is started and the initial JS file is run by a preset automated script, comprising:

[0014] After the initial JS file is compiled from the modified dill file by the preset dart2js compiler in the Flutter framework, a preset browser is started and the initial JS file is run by a UI automated script.

[0015] Optionally, after the preset browser is run, the method functions to be removed in the initial dill file determined based on the preset method function list are removed to obtain a target dill file, comprising:

[0016] After the preset browser is run, the initial dill file is obtained by the preset abstract syntax tree compiler, and the method functions in the initial dill file that are not included in the method function list are removed based on the method function list to obtain a target dill file.

[0017] In a second aspect, the present application discloses a JS file generation device, comprising:

[0018] The method function insertion module is configured to obtain an initial dill file obtained by compiling source code by a Flutter framework, and insert a preset method collector into the initial dill file by a preset abstract syntax tree compiler to obtain a modified dill file.

[0019] The file running module is configured to start a preset browser and run the initial JS file by a preset automated script after the initial JS file is compiled from the modified dill file by the Flutter framework, so that the preset method collector collects method functions used in the running process of the initial JS file and stores the method functions into a preset method function list.

[0020] The function pruning module is configured to, after the preset browser is ended, prune the method functions to be pruned in the initial dill file based on the preset method function list to obtain a target dill file.

[0021] The file compiling module is configured to compile the target dill file by using the Flutter framework to obtain a target JS file.

[0022] Optionally, the file running module comprises:

[0023] The file compiling running unit is configured to, after the initial JS file is compiled by using the preset dart2js compiler in the Flutter framework, start a preset browser and run the initial JS file by using a UI automation script.

[0024] Optionally, the function pruning module comprises:

[0025] The function pruning unit is configured to, after the preset browser is ended, obtain the initial dill file by using the preset abstract syntax tree compiler, and prune the method functions in the initial dill file that are not included in the method function list based on the method function list to obtain a target dill file.

[0026] In a third aspect, the present application discloses an electronic device, comprising:

[0027] A memory is configured to save a computer program.

[0028] A processor is configured to execute the computer program to implement the JS file generation method.

[0029] In a fourth aspect, the present application discloses a computer readable storage medium configured to save a computer program, and the computer program is executed by a processor to implement the JS file generation method.

[0030] It can be seen that, in the present application, firstly, an initial dill file obtained by compiling source code through a Flutter framework is acquired, and a preset method collector is inserted into the initial dill file by using a preset abstract syntax tree compiler to obtain a modified dill file; after the modified dill file is compiled into an initial JS file through the Flutter framework, a preset browser is started and the initial JS file is run through a preset automation script, so that the preset method collector collects method functions used in the running process of the initial JS file and stores them into a preset method function list; after the preset browser is run, the initial dill file is processed to remove the method functions to be removed based on the preset method function list to obtain a target dill file; and the target dill file is compiled through the Flutter framework to obtain a target JS file. It can be seen that, through the inserted preset method collector, the method functions used in the running process of the initial JS file are collected and stored into the preset method function list, and then the method functions in the initial dill file that are not used are removed according to the preset method function list to obtain the target dill file, and the target JS file can be obtained according to the target dill file. In this way, the code that is not used in the running process of the Flutter web compilation product can be removed according to the actual running scene, the size of the JS file can be greatly reduced, and manual code deletion is not required in the whole process. Only after the initial dill file is obtained, the initial dill file is run by using the preset browser, the method functions that are not used can be automatically collected, and then the method functions are removed, thereby reducing the labor cost. BRIEF DESCRIPTION OF DRAWINGS

[0031] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed to be used in the embodiments or the prior art description will be briefly introduced. Obviously, the drawings in the following description are only embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative effort on the basis of the provided drawings.

[0032] Figure 1 A JS file generation method flow chart disclosed by the present application is provided.

[0033] Figure 2 A specific JS file generation method flow chart disclosed by the present application is provided.

[0034] Figure 3 A JS file generation device structure schematic diagram disclosed by the present application is provided.

[0035] Figure 4A structure diagram of an electronic device disclosed in the present application. DETAILED DESCRIPTION

[0036] The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, but not all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by a person of ordinary skill in the art without creative work are within the scope of protection of the present application.

[0037] At present, the Flutter web compilation product is too large, and even in the simplest demo case, the compilation product is nearly 1.4M. When the compiler performs static compilation, only dead code can be deleted. The present application introduces a method that can remove invalid code according to the scene.

[0038] Referring to Figure 1 The embodiments of the present application disclose a JS file generation method, which comprises the following steps:

[0039] Step S11: obtaining an initial dill file obtained by compiling source code through a Flutter framework, and inserting a preset method collector into the initial dill file by using a preset abstract syntax tree compiler to obtain a modified dill file.

[0040] In the present embodiment, before the initial dill file is inserted into the preset method collector by using the preset abstract syntax tree compiler to obtain the modified dill file, a global container list and a method collection function are defined to obtain the preset method collector. That is, a global container can be defined, and the container in the present application is named Container. There is also a method for inserting a method name into the container list, and the method defined in the present application is named methodCollector. The initial dill file is inserted into the preset method collector by using the preset abstract syntax tree compiler to obtain the modified dill file, which comprises: obtaining the preset method collector by using the preset abstract syntax tree compiler, and injecting the preset method collector into all method nodes of the initial dill file to obtain the modified dill file. After obtaining the initial dill file obtained by compiling the source code through the Flutter framework, the methodCollector node is obtained by using the preset abstract syntax tree (AST) compiler, and then the methodCollector node is injected into each method node in the initial dill file to obtain the modified dill file.

[0041] Step S12: after the modified dill file is compiled into an initial JS file by the Flutter framework, a preset browser is started and the initial JS file is run by a preset automation script, so that the preset method collector collects the method functions used in the running of the initial JS file and stores them into a preset method function list.

[0042] In this embodiment, after the modified dill file is compiled into an initial JS file by the Flutter framework, a preset browser is started and the initial JS file is run by a preset automation script, which includes: after the modified dill file is compiled into an initial JS file by a preset dart2js compiler in the Flutter framework, a preset browser is started and the initial JS file is run by a UI automation script. After obtaining the initial JS file, the modified dill file is compiled by the preset dart2js compiler in the Flutter framework to obtain the initial JS file. Then the browser is started and the JS file is run by the UI automation script, so that the preset method collector collects the method functions used in the running of the initial JS file and stores them into a preset method function list.

[0043] Step S13: after the running of the preset browser ends, the method functions to be removed in the initial dill file determined based on the preset method function list are removed to obtain a target dill file.

[0044] In this embodiment, after the running of the preset browser ends, the method functions to be removed in the initial dill file determined based on the preset method function list are removed to obtain a target dill file, which includes: after the running of the preset browser ends, the initial dill file is obtained by the preset abstract syntax tree compiler, and the method functions in the initial dill file that are not included in the method function list are removed based on the method function list to obtain a target dill file. That is, after the preset browser is run, the preset method function list is returned. Then the initial dill file is obtained by the preset abstract syntax tree compiler, and the method functions in the initial dill file that are not included in the method function list are removed to obtain a target dill file.

[0045] Step S14: the target dill file is compiled by the Flutter framework to obtain a target JS file.

[0046] In this embodiment, the target dill file is compiled by a preset dart2js compiler in the Flutter framework to obtain a target JS file.

[0047] It can be seen that, in this embodiment, firstly, an initial dill file obtained by compiling source code by the Flutter framework is acquired, and a preset method collector is inserted into the initial dill file by using a preset abstract syntax tree compiler to obtain a modified dill file; after the modified dill file is compiled into an initial JS file by the Flutter framework, a preset browser is started and the initial JS file is run by a preset automation script, so that the preset method collector collects method functions used in the running process of the initial JS file and stores the method functions into a preset method function list; after the preset browser is run, a method function to be removed in the initial dill file which is determined based on the preset method function list is removed to obtain a target dill file; and the target dill file is compiled by the Flutter framework to obtain a target JS file. It can be seen that, by using the inserted preset method collector, method functions used in the running process of the initial JS file are collected and stored into the preset method function list, and then method functions in the initial dill file which are not used are removed according to the preset method function list to obtain the target dill file, and the target JS file can be obtained according to the target dill file. In this way, code which is not used in the running process of the Flutter web compilation product can be removed according to an actual running scene, the size of the JS file can be greatly reduced, and manual code deletion is not required in the whole process, only after the initial dill file is obtained, the initial dill file is run by using the preset browser, method functions which are not used can be automatically collected, and then the method functions are removed, thereby reducing labor costs.

[0048] Referring to Figure 2As shown, the JS file generation method disclosed in the application specifically includes: creating a method collector: defining a global container named Container, and a method for inserting method names into the container list, and defining the method name as: methodCollector. At the same time, call this methodCollector in the main method of the business code. Then get the dill file obtained by compiling the source code, and through a dill file to AST script, get the methodCollector node, and inject this methodCollector node into all method nodes in the dill file, and generate a new dill file. Through the dart2js compiler, compile the dill file into a JS file, and then start the browser, and run the JS file through the UI automation script. When the browser is run, return the method list collected by the method collector that has been run, and through the dill file to AST script, reprocess the dill file, and remove the methods not included in the valid method list, and generate a new dill file. Finally, compile the new dill file into a JS file as the final product.

[0049] Reference Figure 3 As described above, the embodiments of the application also correspondingly disclose a JS file generation device, which comprises:

[0050] The method function insertion module 11 is configured to obtain an initial dill file obtained by compiling source code through a Flutter framework, and insert a preset method collector into the initial dill file by using a preset abstract syntax tree compiler to obtain a modified dill file;

[0051] The file running module 12 is configured to compile the modified dill file into an initial JS file through the Flutter framework, start a preset browser, and run the initial JS file through a preset automation script, so that the preset method collector collects method functions used in the running process of the initial JS file and stores the method functions in a preset method function list;

[0052] The function removal module 13 is configured to, after the preset browser is run, remove method functions to be removed in the initial dill file based on the preset method function list to obtain a target dill file;

[0053] The file compiling module 14 is configured to compile the target dill file through the Flutter framework to obtain a target JS file.

[0054] It can be seen that in the embodiment, firstly, the initial dill file obtained by compiling the source code through the Flutter framework is acquired, and a preset method collector is inserted into the initial dill file by using a preset abstract syntax tree compiler to obtain a modified dill file; after the modified dill file is compiled into an initial JS file through the Flutter framework, a preset browser is started and the initial JS file is run through a preset automation script, so that the preset method collector collects the method functions used in the running process of the initial JS file and stores them in a preset method function list; after the preset browser is run, the initial dill file is processed to remove the method functions to be removed based on the preset method function list to obtain a target dill file; and the target dill file is compiled through the Flutter framework to obtain a target JS file. It can be seen that the method functions used in the running process of the initial JS file are collected by the inserted preset method collector and stored in the preset method function list, and then the method functions in the initial dill file that are not used are removed according to the preset method function list to obtain the target dill file, and the target JS file can be obtained according to the target dill file. In this way, the code that is not used in the running process of the Flutter web compilation product can be removed according to the actual running scene, the size of the JS file can be greatly reduced, and manual code deletion is not required in the entire process. Only after obtaining the initial dill file, the initial dill file is run by using the preset browser, the method functions that are not used can be automatically collected, and then the method functions are removed, thereby reducing the labor cost.

[0055] In some specific embodiments, the JS file generation apparatus can further include:

[0056] The method collector generation module is configured to define a global container list and a method collection function to obtain the preset method collector.

[0057] In some specific embodiments, the method function insertion module 11 can be configured to acquire the preset method collector by using a preset abstract syntax tree compiler, and inject the preset method collector into all method nodes of the initial dill file to obtain a modified dill file.

[0058] In some specific embodiments, the file running module 12 can include:

[0059] The file compiling and running unit is configured to, after the modified dill file is compiled into an initial JS file by a preset dart2js compiler in the Flutter framework, start a preset browser and run the initial JS file by using a UI automation script.

[0060] In some specific embodiments, the function pruning module 13 can specifically include:

[0061] The function pruning unit is configured to, after the running of the preset browser ends, obtain the initial dill file by using the preset abstract syntax tree compiler, and prune the method functions in the initial dill file that are not included in the method function list based on the method function list to obtain a target dill file.

[0062] Further, the embodiment of the present application further discloses an electronic device, Figure 4 is a structural diagram of an electronic device 20 according to an exemplary embodiment, and the content in the figure cannot be considered as any limitation on the use range of the present application.

[0063] Figure 4 A structural schematic diagram of an electronic device 20 provided by the embodiment of the present application. The electronic device 20 can specifically include at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25 and a communication bus 26. The memory 22 is configured to store a computer program, and the processor 21 is configured to load and execute the computer program to implement the related steps in the JS file generation method disclosed in any of the preceding embodiments. In addition, the electronic device 20 in the embodiment can be an electronic computer.

[0064] In the embodiment, the power supply 23 is configured to provide working voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol followed by the communication interface 24 can be any communication protocol applicable to the technical solution of the present application, which is not limited specifically herein; the input / output interface 25 is configured to obtain external input data or output data to the outside, and the specific interface type can be selected according to the specific application needs, which is not limited specifically herein.

[0065] In addition, the memory 22 as a carrier for resource storage can be a read-only memory, a random access memory, a magnetic disk or an optical disk, etc., and the resources stored thereon can include an operating system 221, a computer program 222, etc., and the storage mode can be temporary storage or permanent storage.

[0066] The operating system 221 is used to manage and control each hardware device on the electronic device 20 and the computer program 222, which can be Windows Server, Netware, Unix, Linux, etc. The computer program 222 can further include computer programs capable of completing other specific work in addition to the computer programs capable of completing the JS file generation method disclosed by the electronic device 20 in any of the foregoing embodiments.

[0067] Further, the present application also discloses a computer readable storage medium for storing a computer program; wherein the computer program is executed by a processor to implement the JS file generation method disclosed above. For the specific steps of the method, please refer to the corresponding content disclosed in the foregoing embodiments, which will not be repeated here.

[0068] The embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts of each embodiment can be referred to each other. For the device disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple, and the relevant parts can be referred to the method part.

[0069] The skilled person can further realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be realized by electronic hardware, computer software or a combination of both. In order to clearly show the interchangeability of hardware and software, the components and steps of each example have been described in the above description. Whether the functions are realized in hardware or software depends on the specific application and design constraints of the technical solution. The skilled person can use different methods to realize the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.

[0070] The steps of the method or algorithm described in combination with the embodiments disclosed herein can be directly implemented by hardware, software modules executed by a processor, or a combination of both. The software modules can be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

[0071] Finally, it needs to be pointed out that in this document, relational terms such as first and second and the like can only be intended to distinguish one entity or operation from another entity or operation without necessarily requiring or implying any actual such relationship or order between such entities or operations. Moreover, the terms "comprising", "including", or any other variant thereof are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can also include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without more limitations, an element defined by the statement "comprising a" does not exclude the existence of additional identical elements in the process, method, article, or apparatus including the stated element.

[0072] The above detailed description of the technical solutions provided by the present application has been provided, and the principles and implementation manners of the present application have been described by applying specific examples. The above description of the examples is only for the purpose of helping to understand the method of the present application and its core idea; at the same time, for those skilled in the art, according to the idea of the present application, the specific implementation manner and application range will be changed, and the above description of the content of the specification should not be understood as a limitation of the present application.

Claims

1. A method of generating a JS file, characterized by, The method comprises the following steps: obtaining an initial dill file obtained by compiling source code through a Flutter framework, and inserting a preset method collector into the initial dill file by using a preset abstract syntax tree compiler to obtain a modified dill file; after the modified dill file is compiled into an initial JS file through the Flutter framework, starting a preset browser and running the initial JS file through a preset automation script, so that the preset method collector collects method functions used in the running process of the initial JS file and stores the method functions in a preset method function list; after the preset browser is run, the initial dill file is processed to remove method functions determined based on the preset method function list to obtain a target dill file; after the preset browser is run, the initial dill file is processed to remove method functions determined based on the preset method function list to obtain a target dill file, which comprises the following steps: after the preset browser is run, the initial dill file is obtained by using the preset abstract syntax tree compiler, and the method functions in the initial dill file that are not included in the method function list are removed based on the method function list to obtain a target dill file; the target dill file is compiled through the Flutter framework to obtain a target JS file.

2. The JS file generation method according to claim 1, characterized by, Before the preset method collector is inserted into the initial dill file by using the preset abstract syntax tree compiler to obtain the modified dill file, the method further comprises the following steps: defining a global container list and a method collection function to obtain the preset method collector.

3. The JS file generation method according to claim 2, characterized by, The method of inserting the preset method collector into the initial dill file by using the preset abstract syntax tree compiler to obtain the modified dill file comprises the following steps: obtaining the preset method collector by using the preset abstract syntax tree compiler, and injecting the preset method collector into all method nodes of the initial dill file to obtain the modified dill file.

4. The JS file generation method according to claim 1, characterized by, After the modified dill file is compiled into an initial JS file through the Flutter framework, a preset browser is started and the initial JS file is run through a preset automation script, which comprises the following steps: after the modified dill file is compiled into an initial JS file through the preset dart2js compiler in the Flutter framework, a preset browser is started and the initial JS file is run by using a UI automation script.

5. A JS file generation apparatus characterized by comprising: The method comprises the following steps: a method function insertion module is configured to obtain an initial dill file obtained by compiling source code through a Flutter framework, and insert a preset method collector into the initial dill file by using a preset abstract syntax tree compiler to obtain a modified dill file; The file running module is configured to, after the modified dill file is compiled into an initial JS file by the Flutter framework, start a preset browser and run the initial JS file by a preset automation script, so that the preset method collector collects method functions used in the running of the initial JS file and stores the method functions into a preset method function list; The function pruning module is configured to, after the preset browser is run, prune method functions to be pruned in the initial dill file based on the preset method function list to obtain a target dill file; The function pruning module includes a function pruning unit configured to, after the preset browser is run, acquire the initial dill file by using the preset abstract syntax tree compiler, and prune method functions in the initial dill file that are not included in the method function list based on the method function list to obtain a target dill file; The file compiling module is configured to compile the target dill file by the Flutter framework to obtain a target JS file.

6. The JS file generation apparatus according to claim 5, wherein The file running module includes: The file compiling running unit is configured to, after the modified dill file is compiled into an initial JS file by a preset dart2js compiler in the Flutter framework, start a preset browser and run the initial JS file by a UI automation script.

7. An electronic device, comprising: It includes: A memory configured to save a computer program; A processor configured to execute the computer program to implement the JS file generation method according to any one of claims 1 to 4.

8. A computer-readable storage medium, characterized in that, A memory configured to save a computer program, which is executed by a processor to implement the JS file generation method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • Page generation method and device, electronic equipment and computer readable medium

    CN112506602A

  • Project development method and device, electronic equipment and storage medium

    CN113031932A