Code optimization method, device, equipment, storage medium and computer program product
By automating analysis and replacing global imports with local references, the code size optimization problem is solved, resulting in reduced code package size and improved development efficiency. This is suitable for code optimization in uni-app mini-program projects.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- CHINA MOBILE M2M
- Filing Date
- 2026-03-02
- Publication Date
- 2026-06-30
AI Technical Summary
Existing code optimization solutions struggle to automatically and efficiently optimize code size without significant changes to project structure and build configuration, especially for existing projects of a certain size that use global imports, lacking lightweight optimization methods that do not require continuous intervention.
By automating the analysis of project file sets, identifying and deleting unused files, replacing global imports of UI component libraries with local references, generating local reference statements for the target UI component library, and completing code optimization.
It effectively reduces code package size, improves development and maintenance efficiency, reduces the risk of manual operation, and enhances project maintainability and team collaboration efficiency.
Smart Images

Figure CN122308838A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of software development technology, and in particular to a code optimization method, apparatus, device, storage medium, and computer program product. Background Technology
[0002] With the development of the mobile application ecosystem, mini-program development based on the uni-app framework has been widely adopted due to its cross-platform characteristics. During development, to improve efficiency, developers often import UI component libraries such as uView, typically using a global import approach. However, the mini-program platform has strict limitations on code package size, such as a maximum of 2MB; exceeding this limit will prevent uploading and publishing.
[0003] Currently, common optimization methods to control code size include changing global imports to local imports to remove unused components, or cleaning up redundant files in the project. However, these optimization solutions have the following problems: if manual modifications are made item by item, the processing efficiency is extremely low and errors such as accidental deletion are easy to occur; if existing automated tools are used to implement on-demand imports, specific configuration of the build process is usually required, which is less efficient when the project structure is complex or iterations are frequent.
[0004] Therefore, existing code optimization solutions struggle to simultaneously control code size and improve development and maintenance efficiency, especially for existing projects that have reached a certain size and use a global import approach, lacking a lightweight optimization method that requires no continuous intervention.
[0005] Therefore, how to automatically and efficiently optimize code size without significantly altering the project structure and build configuration has become a pressing technical problem in this field. Summary of the Invention
[0006] This application provides a code optimization method to address the problem that existing code optimization schemes cannot simultaneously achieve effective control of code size and improvement of development and maintenance efficiency, especially for existing projects of a certain size that use a global import approach, where there is a lack of a lightweight optimization method that does not require continuous intervention.
[0007] This application also provides a code optimization device to address the problem that existing code optimization schemes cannot simultaneously achieve effective control of code size and improvement of development and maintenance efficiency, especially for existing projects of a certain size that use a global import approach, where there is a lack of a lightweight optimization method that does not require continuous intervention.
[0008] This application also provides a code optimization device to address the problem that existing code optimization solutions cannot simultaneously achieve effective control of code size and improvement of development and maintenance efficiency, especially for existing projects of a certain size that use a global import approach, where there is a lack of a lightweight optimization method that does not require continuous intervention.
[0009] This application also provides a computer-readable storage medium to address the problem that existing code optimization schemes are unable to simultaneously achieve effective control of code size and improvement of development and maintenance efficiency, especially for existing projects of a certain size that use a global import approach, where there is a lack of a lightweight optimization method that does not require continuous intervention.
[0010] A computer program product designed to address the challenge that current code optimization solutions struggle to simultaneously control code size and improve development and maintenance efficiency, particularly for existing projects of a certain size that use a global import approach, where there is a lack of a lightweight optimization method that requires no continuous intervention.
[0011] The embodiments of this application adopt the following technical solutions: A code optimization method includes: determining a set of used files based on a set of project files corresponding to the project to be optimized, wherein the set of project files includes all files under the project directory; determining unused files based on the set of project files and the set of used files, and deleting the unused files from the set of project files to obtain a first set of files; identifying Vue files contained in the first set of files, and determining the target UI component corresponding to each Vue file; generating local reference statements for the target UI component library based on the target UI component, and replacing the global import statements for the target UI component library in the entry files contained in the first set of files with the local reference statements, thereby completing the code optimization of the project to be optimized.
[0012] A code optimization apparatus includes: a used file determination unit, configured to determine a used file set based on an acquired project file set corresponding to the project to be optimized, wherein the project file set includes all files under the project directory; an unused file deletion unit, configured to determine unused files based on the project file set and the used file set, and delete the unused files in the project file set to obtain a first file set; a UI component determination unit, configured to identify Vue files contained in the first file set, and determine the target UI component corresponding to each Vue file; and a replacement unit, configured to generate local reference statements for a target UI component library based on the target UI component, and replace the global import statements for the target UI component library in the entry files contained in the first file set with the local reference statements, thereby completing the code optimization of the project to be optimized.
[0013] A code optimization device, comprising: A processor; and a memory configured to store computer-executable instructions, which, when executed, cause the processor to perform the following operations: determining a set of used files based on a set of project files corresponding to the project to be optimized, wherein the set of project files includes all files in the project directory; determining unused files based on the set of project files and the set of used files, and deleting the unused files from the set of project files to obtain a first set of files; identifying Vue files contained in the first set of files, and determining the target UI component corresponding to each Vue file; generating local reference statements for the target UI component library based on the target UI component, and replacing the global import statements for the target UI component library in the entry files contained in the first set of files with the local reference statements, thereby completing the code optimization of the project to be optimized.
[0014] A computer-readable storage medium stores one or more programs that, when executed by an electronic device including multiple applications, cause the electronic device to perform the following operations: determining a set of used files based on an acquired set of project files corresponding to a project to be optimized, wherein the set of project files includes all files under the project directory; determining unused files based on the set of project files and the set of used files, and deleting the unused files from the set of project files to obtain a first set of files; identifying Vue files contained in the first set of files, and determining the target UI component corresponding to each Vue file; generating local reference statements for a target UI component library based on the target UI component, and replacing the global import statements for the target UI component library in the entry files contained in the first set of files with the local reference statements, thereby completing the code optimization of the project to be optimized.
[0015] A computer program product includes a computer program that, when executed by a processor, implements the following: determining a set of used files based on an acquired set of project files corresponding to a project to be optimized, wherein the set of project files includes all files under the project directory; determining unused files based on the set of project files and the set of used files, and deleting the unused files from the set of project files to obtain a first set of files; identifying Vue files contained in the first set of files, and determining the target UI component corresponding to each Vue file; generating local reference statements for a target UI component library based on the target UI component, and replacing the global import statements for the target UI component library in the entry files contained in the first set of files with the local reference statements, thereby completing the code optimization of the project to be optimized.
[0016] The above-described technical solutions adopted in the embodiments of this application can achieve the following beneficial effects: Using the code optimization method provided in this application, when code optimization of a development project is required, the first step is to determine the set of used files based on the project file set corresponding to the project to be optimized, and then determine the unused files based on the project file set and the set of used files. Unused files are then deleted from the project file set to obtain a first file set. By identifying the Vue files contained in the first file set, the target UI component corresponding to each Vue file is determined. Based on the target UI component, local reference statements for the target UI component library are generated. Based on the local reference statements, the global import statements for the target UI component library in the entry files contained in the first file set are replaced with local reference statements, thus completing the code optimization of the project to be optimized. The code optimization method provided in this application effectively reduces the final code package size from two dimensions: firstly, by automating the analysis and deletion of unused files in the project and intelligently replacing global imports of UI component libraries with on-demand local imports; secondly, the method transforms the optimization work that originally required manual identification and modification into a fully automated one-time script execution, eliminating the need for developers to manually search for redundant files or repeatedly modify component import code in each Vue file. This not only significantly saves development time and reduces the risk of omissions and errors due to manual operation, but also allows for convenient code size management even after long-term iterations, improving project maintainability and team collaboration efficiency. Attached Figure Description
[0017] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings: Figure 1 This application provides a schematic flowchart of a code optimization method according to an embodiment of the present application. Figure 2 This application provides a schematic flowchart illustrating a method for obtaining a set of project files in an embodiment of the present application. Figure 3 This application provides a schematic flowchart illustrating a method for obtaining a set of used files in an embodiment. Figure 4 This application provides a schematic diagram illustrating a specific process for filtering Vue files in an embodiment. Figure 5 This application provides a schematic diagram illustrating a specific process for generating local reference statements in an embodiment of the present application. Figure 6 This application provides a schematic flowchart of a code optimization method according to an embodiment of the present application. Figure 7A schematic diagram of the specific structure of a code optimization device provided in an embodiment of this application; Figure 8 This is a schematic diagram of the specific structure of a code optimization device provided in an embodiment of this application. Detailed Implementation
[0018] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below in conjunction with specific embodiments and corresponding drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0019] This application provides a code optimization method to address the problem that existing code optimization schemes cannot simultaneously achieve effective control of code size and improvement of development and maintenance efficiency, especially for existing projects of a certain size that use a global import approach, where there is a lack of a lightweight optimization method that does not require continuous intervention.
[0020] The execution subject of the code optimization method provided in this application embodiment may be, but is not limited to, at least one of a development server, a code optimization server, and a program packaging server; in addition, the execution subject of the method may also be the system or application (APP) itself running on these servers.
[0021] For ease of description, the following text uses a code optimization system as the execution subject of this method and a uni-app mini-program project as the example to introduce the implementation of this method. It should be understood that using a code optimization system as the execution subject is merely an illustrative example and should not be construed as a limitation of the method.
[0022] The specific implementation flowchart of the code optimization method provided in this application is shown in the figure below. Figure 1 As shown, the main steps include the following: Step 11: Determine the set of files already used based on the set of project files corresponding to the project to be optimized; The project file collection includes all files in the project directory.
[0023] In this embodiment of the application, the project file set of the uni-app project to be optimized can be obtained by the following method: traversing the root directory and subdirectories of the project to be optimized; and obtaining the paths of all files in the root directory and the subdirectories according to the preset matching rules to obtain the project file set.
[0024] Specifically, in one implementation, the code optimization system can retrieve project files using a custom file path retrieval function, such as the getAllFiles function. The input parameters of the getAllFiles function include the root directory path of the uni-app project to be optimized and a regular expression for matching file paths.
[0025] In one implementation, the regular expression used to match all files is designed to identify common file types used in project development. These may include, but are not limited to: Vue single-file components with the ".vue" suffix, script files with the ".js" or ".ts" suffix, stylesheet files with the ".css", ".scss", or ".less" suffixes, and static resource files such as images and fonts with suffixes such as ".png", ".jpg", and ".ttf". This regular expression ensures that when traversing the project root directory and all its subdirectories, the absolute or relative path of every file is comprehensively collected, ultimately forming a complete set of project files.
[0026] In this embodiment of the application, the specific process for obtaining the project file set is as follows: Figure 2 As shown, it mainly includes the following sub-steps: Sub-step 1101, process start: Call the getAllFiles function and pass in the following two key parameters: Parameter 1, projectRoot: The root directory path of the project to be optimized.
[0027] Parameter 2, regAll: A preset regular expression used to match the file types to be collected, such as ".vue", ".js", ".css", etc.
[0028] Sub-step 1102, reading and traversal: The `getAllFiles` function begins reading the directory specified by the `projectRoot` parameter and iterates through all entries in that directory, including files and subfolders.
[0029] Sub-step 1103, Path determination and processing: For each entry encountered, perform the following checks: When the entry is determined to be a folder, the program will recursively call the getAllFiles function itself, and pass the path of the current folder as the new projectRoot parameter to ensure that all levels of subdirectories are thoroughly scanned.
[0030] When the entry is determined to be a file, the program obtains the path of the file and checks whether it matches the preset regular expression regAll. If it matches, the path of the file is added to a temporary or global storage collection, such as an array or collection of allFiles.
[0031] Sub-step 1104, result returned: Once the initial call to the `getAllFiles` function and all its recursive calls have completed, the entire project directory tree has been fully traversed. At this point, the `getAllFiles` function returns the final collection of file paths, `allFiles`, as the result. This collection is the "project file collection," containing all file paths in the project that conform to the preset type rules.
[0032] After obtaining the project file set of the uni-app project to be optimized through the above process, the code optimization system can further filter out the files that are actually referenced in the project source code from the project file set to form a set of used files.
[0033] Specifically, the process begins by filtering source code files (such as .vue and .js files) from the project file collection. Then, the content of these source code files is scanned using a pre-defined regular expression (e.g., regImport) configured to recognize various referencing syntaxes such as "import", "require", "src=", and "url". When these statements are matched in the source code content, the specific file path being referenced is extracted. In this embodiment of the application, the specific process for obtaining the set of used files is as follows: Figure 3 As shown, it mainly includes the following sub-steps: Sub-step 11-a, initialize the matching rules: Specifically, you can first define and initialize a regular expression for matching import statements, such as "regImport". This regular expression is pre-designed to recognize various forms of dependency import statements in the source code, such as import in ES Modules, require() in CommonJS, resource links in HTML / templates (such as src="..." or href="..."), and url() in CSS, etc.
[0034] Sub-step 11-b: Read the file content: By calling a file reading function (e.g., the readFile function), the entire text content of the file is read into memory based on the passed file path, so that subsequent text analysis can be performed.
[0035] Sub-step 11-c, Content Analysis and Judgment: Using the regular expression regImport defined in substep 11-a, the read file content is scanned and matched. The specific judgment condition is: whether there is a string in the current file content that matches the quoted statement pattern.
[0036] Sub-step 11-d, path extraction and collection: When the result of substep 11-c is yes, meaning one or more reference statements are found in the content, the specific file paths referenced are parsed from these matched statements. These extracted file paths are then added to a central storage set, such as a set or array named `usedFiles`. This set is used to accumulate all discovered and used file paths.
[0037] If the result of the judgment obtained by executing sub-step 11-c is negative, that is, no reference statement is detected in the current file content, then no path collection operation is performed.
[0038] Sub-step 11-e, iteration and loop: Regardless of whether the current file contains references, after completing the above judgment and possible collection operations, the process moves to "read the next file". Traverse each file in the project file set and repeat sub-steps 11-b to 11-d until all files have been analyzed.
[0039] Once all files have been traversed, the resulting storage collection usedFiles contains the paths to all referenced files extracted from the entire project source code, thus forming a complete collection of used files.
[0040] Step 12: Based on the project file set and the used file set, determine the unused files, and delete the unused files from the project file set to obtain the first file set; In this embodiment, the code optimization system can compare the project file set with the used file set to identify files that exist in the project file set but not in the used file set as unused files. These unused files may be old pages that are not referenced, obsolete components, unused styles, or image resources, etc. Subsequently, the system calls the file system operation interface to physically delete these unused files from the project directory or move them to a backup location. After the deletion operation is completed, the remaining files constitute the optimized first file set.
[0041] Step 13: Identify the Vue files contained in the first file set and determine the target UI component corresponding to each Vue file; In this embodiment of the application, all Vue files can be filtered out from the first file set by matching regular expressions that match the ".vue" suffix, such as "regVue", to form a list of Vue files.
[0042] In this embodiment of the application, the specific process for filtering all Vue files from the first file set is as follows: Figure 4 As shown, it mainly includes the following sub-steps: Sub-step 1301, reading and traversing the directory: First, you can call the getAllFiles function to read the passed-in directory path, which is initially the project root directory. It will then retrieve all entries in that directory, including a list of files and subfolders, and begin iterating through them one by one.
[0043] Sub-step 1302, path type determination: For each entry encountered, determine whether it is a folder.
[0044] If the result is yes, the getAllFiles function itself is called recursively, and the path of the current subfolder is passed in as a new parameter.
[0045] If the result is negative, meaning it is a file but not a folder, then the file type determination step begins.
[0046] Sub-step 1303, file type filtering: By executing the file entries determined in sub-step 1302 above, it will be further checked whether their file paths meet the definition of a Vue file.
[0047] Specifically, the passed-in regular expression "regVue" is used to perform a match test on the full path or name of the file.
[0048] If the match result is yes, it means that the file is a Vue file (.vue file), and the path of the Vue file will be added to a storage collection specifically for collection, such as an array or collection named allVueFiles.
[0049] If the match result is negative, ignore this non-Vue file and continue processing the next entry.
[0050] Sub-step 1304, recursively summarize and return: The above process continues until the initial directory and all its subdirectories have been completely traversed. All recursive call branches will eventually complete and return. When the outermost getAllFiles function call finishes executing, it will finally return the fully populated collection of storage, allVueFiles.
[0051] The final output is an allVueFiles collection, which contains the complete paths of all Vue single-file components found by recursion, starting from the project root directory.
[0052] Next, the content of each Vue file in the allVueFiles collection is analyzed to determine which components from the target UI component library are used internally. In this embodiment, the specific method for determining the target UI component corresponding to each Vue file may include: searching for component identifiers with a specific naming prefix in each Vue file; and determining the target UI component based on the component corresponding to the component identifier.
[0053] In one implementation, the code optimization system can optimize the template of the Vue file ( <template>The `<component>` section searches for all tags with a specific component library prefix. For example, if all component tags in the target UI component library are prefixed with "u-" (e.g., ...), then... <u-button> 、 <u-icon>If the prefix is matched, then a search is performed using a regular expression that matches the prefix (such as "u-").
[0054] All unique prefix component tags found in a Vue file are identified as the "target UI components" that the file depends on. For example, if a file contains... <u-button>and <u-icon>The tag determines that its target UI components are Button and Icon.
[0055] Step 14: Generate local reference statements for the target UI component library based on the target UI component determined by executing Step 13; Specifically, for each Vue file, based on its unique list of target UI components determined through step 13, local import and registration code conforming to Vue.js syntax is automatically generated. For example, for a file using the Button and Icon components, the generated code logic is as follows: import the uButton and uIcon components from the target UI component library as needed, and register them locally in the components option of the current Vue component. Then, this generated code is inserted into the script section of the Vue file. <script>)的适当位置。
[0056] 在本申请实施例中,步骤14的具体流程如图5所示,主要包括以下子步骤:步骤1401,定义convertToLocalImport函数。
[0057] 步骤1402,读取文件内容:convertToLocalImport函数启动后,首先调用文件读取功能,根据传入的Vue文件路径,读取该文件的全部文本内容到内存中,为后续的文本分析做好准备。
[0058] 步骤1403,识别使用的UI组件:具体地,convertToLocalImport函数利用预设的、用于匹配特定UI组件库组件标签的正则表达式,并结合代表该组件库标签前缀的变量,对读取到的Vue文件内容进行扫描。
[0059] 在本申请实施例中,该正则表达式被设计为能够精准匹配该UI组件库特有的组件命名模式(例如所有以 u- 开头的标签)。
[0060] 通过执行此匹配,convertToLocalImport函数能够从文件的模板(<template>)部分,提取出所有被使用到的该UI组件库的组件标识符(例如 u-button, u-icon)。所有被识别出的唯一组件名称会被收集并存入一个临时的组件集合components中。
[0061] 子步骤1404,判断与代码生成决策:检查通过执行子步骤1403的组件集合components,判断该组件集合的长度是否大于0,即当前Vue文件中是否确实使用了至少一个来自目标UI组件库的组件。
[0062] 当判断结果为是时,则进入代码生成阶段。convertToLocalImport函数会根据集合中的组件列表,动态构建一段符合Vue.js语法规范的局部引入与注册代码。这段代码通常可以包括两部分:首先是从UI组件库中按需导入这些具体组件的import语句;其次是在当前Vue组件的components选项中注册这些组件的代码。
[0063] 当判断结果为否时,说明该Vue文件并未使用目标UI组件库,则无需进行任何修改,流程结束。
[0064] 子步骤1405,写入生成代码:对于需要修改的文件,函数最后会调用文件写入功能。它将新生成的局部引入代码,整合或替换到原Vue文件脚本部分(<script>)的适当位置,从而完成对该文件源代码的自动化重构。
[0065] 通过遍历项目中所有Vue文件并执行此流程,可以将整个项目对目标UI组件库的依赖,从单一的、包含所有组件的全局引入,转变为分散的、按需索取的局部引入,从而有效移除未使用组件的代码,减少最终打包体积。
[0066] 步骤15,根据通过执行步骤14确定的局部引用语句,将第一文件集合所包含的入口文件中对所述目标UI组件库的全局引入语句,替换为所述局部引用语句;具体地,代码优化系统可以在处理项目的入口文件(例如,"main.js”或"main.ts”中,定位到用于全局注册整个目标UI组件库的语句,然后,将此全局引入语句移除或注释掉。
[0067] 步骤16,封装得到代码优化函数,并通过该代码优化函数完成对所述待优化项目的代码优化。
[0068] 具体地,将上述步骤11~步骤15步骤封装在一个主函数中。该主函数接收项目根目录、UI组件库前缀等必要的配置参数。开发者在需要优化项目时,只需运行一次该脚本文件,脚本将自动且顺序地执行上述步骤11至步骤15的所有操作,如图6所述,主要包括以下流程:流程1,获取所有文件列表:首先调用getAllFiles函数。该函数遍历项目整个目录树,根据匹配所有类型文件的正则规则,收集项目中存在的每一个文件的路径,形成一个完整的项目文件全集allFiles。
[0069] 流程2,识别已使用的文件:接着,调用getUsedFiles函数。getUsedFiles函数基于预设的引用语句匹配规则,深入分析项目源代码,解析出所有被import、require或通过标签引用的文件路径,生成已使用文件列表usedFiles。
[0070] 流程3,删除未使用的冗余文件:调用removeUnusedFiles函数,该函数将前两步的结果进行比对:从allFiles全集中剔除usedFiles列表中的条目,剩余的即为"未使用文件”。系统会将这些未被引用的冗余文件从项目目录中物理删除。此举直接从源头减少了打包体积,得到清理后的项目文件集合。
[0071] 流程4,定位所有Vue组件文件:在清理后的项目基础上,再次调用getAllFiles函数,使用专门匹配.vue文件的正则规则。精准筛选出项目中所有的Vue单文件组件,获取它们的路径列表,为后续的组件级优化做好准备。
[0072] 流程5,转换每个Vue文件的组件引入方式:遍历上一步得到的所有Vue文件路径,对每一个文件调用convertToLocalImport函数。
[0073] 通过convertToLocalImport函数:读取单个Vue文件内容,识别其模板中使用的所有uView组件,并为这些用到的组件动态生成按需导入和局部注册的代码。
[0074] 将生成的局部引入代码写入该Vue文件的脚本部分。
[0075] 流程6、替换入口文件的全局引入。
[0076] 最后,调用replaceGlobalImport函数处理项目的入口文件,该replaceGlobalImport函数定位并移除或注释掉在该入口文件中初始化并全局注册整个uView组件库的语句。由于所有组件已在各自文件中被局部引入和注册,这条全局引入语句便不再需要,移除它可以避免重复引入和潜在的冲突。
[0077] 脚本运行完毕后,项目的冗余文件已被清除,UI组件的引入方式也已全部转换为局部按需引入。此次优化结果将直接保存在项目源代码中,在此后的常规开发构建流程中,无需再次运行本优化脚本,从而避免了持续的性能开销。项目代码的体积得到显著减小,且不影响后续的开发体验。
[0078] 采用本申请实施例提供的代码优化方法,当需要对开发项目进行代码优化时,首先可以根据获取到的待优化项目对应的项目文件集合,确定已使用文件集合,并根据项目文件集合与已使用文件集合,确定未使用文件,进而在项目文件集合删除未使用文件,得到第一文件集合,通过识别第一文件集合中包含的Vue文件,分别确定各Vue文件对应的目标UI组件,根据目标UI组件,生成针对目标UI组件库的局部引用语句,并根据局部引用语句,将第一文件集合所包含的入口文件中对目标UI组件库的全局引入语句,替换为局部引用语句,完成对所述待优化项目的代码优化。采用本申请实施例所提供的代码优化方法,一方面,通过自动化分析并删除项目中的未使用文件,以及将UI组件库的全局引入智能替换为按需局部引入,本方法能够从两个维度有效地减少最终代码包的体积;另外一方面,本申请实施例所提供的方法可以将原本需要人工逐一识别修改的优化工作,转变为全自动的一次性脚本执行,开发者无需再手动查找冗余文件,也无需在每个Vue文件中反复修改组件引入代码。这不仅极大地节省了开发时间,降低了因人工操作可能带来的遗漏和错误风险,也使得项目在长期迭代后依然能便捷地进行代码体积治理,提升了项目的可维护性和团队协作效率。
[0079] 在一种实施方式中,本申请实施例还提供了一种代码优化装置,用以解决现的代码优化方案难以同时实现代码体积的有效控制与开发维护效率的提升,尤其对于已有一定规模、采用全局引入方式的存量项目,缺乏一种轻量且无需持续干预的优化手段的问题。该代码优化装置的具体结构示意图如图7所示,包括:已使用文件确定单元71、未使用文件删除单元72、UI组件确定单元73以及替换单元74。
[0080] 其中,已使用文件确定单元71,用于根据获取到的待优化项目对应的项目文件集合,确定已使用文件集合,其中,所述项目文件集合包括所述项目目录下的所有文件;未使用文件删除单元72,用于根据所述项目文件集合与所述已使用文件集合,确定未使用文件,并在所述项目文件集合删除所述未使用文件,得到第一文件集合;UI组件确定单元73,用于识别所述第一文件集合中包含的Vue文件,分别确定各所述Vue文件对应的目标UI组件;替换单元74,用于根据所述目标UI组件,生成针对目标UI组件库的局部引用语句,并根据所述局部引用语句,将所述第一文件集合所包含的入口文件中对所述目标UI组件库的全局引入语句,替换为所述局部引用语句,完成对所述待优化项目的代码优化。
[0081] 在一种实施方式中,已使用文件确定单元71,具体用于:遍历所述待优化项目的根目录及子目录;根据预设的匹配规则,分别获取所述根目录与所述子目录目录下所有文件的路径,得到所述项目文件集合。
[0082] 在一种实施方式中,已使用文件确定单元71,具体用于:根据所述项目文件集合,确定所述待优化项目对应的源码文件;根据所述源码文件,确定引用语句,并根据所述引用语句,确定引用文件路径;根据所述引用文件路径,生成所述已使用文件集合。
[0083] 在一种实施方式中,UI组件确定单元73,具体用于:在各所述Vue文件中,查找具有特定命名前缀的组件标识符;根据所述组件标识符对应的组件,确定所述目标UI组件。
[0084] 在一种实施方式中,UI组件确定单元73,具体用于:根据各所述Vue文件对应的目标UI组件,生成所述目标UI组件对应的局部引入代码;将所述局部引入代码添加至对应Vue文件的脚本部分,得到针对目标UI组件库的局部引用语句。
[0085] 在一种实施方式中,替换单元74,具体用于:在所述入口文件中,确定用于全局引入所述目标UI组件库的全局引入语句;将所述全局引入语句替换为所述局部引用语句。
[0086] 采用本申请实施例提供的代码优化装置,当需要对开发项目进行代码优化时,首先可以根据获取到的待优化项目对应的项目文件集合,确定已使用文件集合,并根据项目文件集合与已使用文件集合,确定未使用文件,进而在项目文件集合删除未使用文件,得到第一文件集合,通过识别第一文件集合中包含的Vue文件,分别确定各Vue文件对应的目标UI组件,根据目标UI组件,生成针对目标UI组件库的局部引用语句,并根据局部引用语句,将第一文件集合所包含的入口文件中对目标UI组件库的全局引入语句,替换为局部引用语句,完成对所述待优化项目的代码优化。采用本申请实施例所提供的代码优化装置,一方面,通过自动化分析并删除项目中的未使用文件,以及将UI组件库的全局引入智能替换为按需局部引入,本方法能够从两个维度有效地减少最终代码包的体积;另外一方面,本申请实施例所提供的方法可以将原本需要人工逐一识别修改的优化工作,转变为全自动的一次性脚本执行,开发者无需再手动查找冗余文件,也无需在每个Vue文件中反复修改组件引入代码。这不仅极大地节省了开发时间,降低了因人工操作可能带来的遗漏和错误风险,也使得项目在长期迭代后依然能便捷地进行代码体积治理,提升了项目的可维护性和团队协作效率。
[0087] 图8是本申请的一个实施例电子设备的结构示意图。请参考图8,在硬件层面,该电子设备包括处理器,可选地还包括内部总线、网络接口、存储器。其中,存储器可能包含内存,例如高速随机存取存储器(Random-Access Memory,RAM),也可能还包括非易失性存储器(non-volatile memory),例如至少1个磁盘存储器等。当然,该电子设备还可能包括其他业务所需要的硬件。
[0088] 处理器、网络接口和存储器可以通过内部总线相互连接,该内部总线可以是ISA(Industry Standard Architecture,工业标准体系结构)总线、PCI(PeripheralComponent Interconnect,外设部件互连标准)总线或EISA(Extended Industry StandardArchitecture,扩展工业标准结构)总线等。所述总线可以分为地址总线、数据总线、控制总线等。为便于表示,图8中仅用一个双向箭头表示,但并不表示仅有一根总线或一种类型的总线。
[0089] 存储器,用于存放程序。具体地,程序可以包括程序代码,所述程序代码包括计算机操作指令。存储器可以包括内存和非易失性存储器,并向处理器提供指令和数据。
[0090] 处理器从非易失性存储器中读取对应的计算机程序到内存中然后运行,在逻辑层面上形成代码优化装置。处理器,执行存储器所存放的程序,并具体用于执行以下操作:根据获取到的待优化项目对应的项目文件集合,确定已使用文件集合,其中,所述项目文件集合包括所述项目目录下的所有文件;根据所述项目文件集合与所述已使用文件集合,确定未使用文件,并在所述项目文件集合删除所述未使用文件,得到第一文件集合;识别所述第一文件集合中包含的Vue文件,分别确定各所述Vue文件对应的目标UI组件;根据所述目标UI组件,生成针对目标UI组件库的局部引用语句,并根据所述局部引用语句,将所述第一文件集合所包含的入口文件中对所述目标UI组件库的全局引入语句,替换为所述局部引用语句,完成对所述待优化项目的代码优化。
[0091] 上述如本申请图8所示实施例揭示的代码优化电子设备执行的方法可以应用于处理器中,或者由处理器实现。处理器可能是一种集成电路芯片,具有信号的处理能力。在实现过程中,上述方法的各步骤可以通过处理器中的硬件的集成逻辑电路或者软件形式的指令完成。上述的处理器可以是通用处理器,包括中央处理器(Central Processing Unit,CPU)、网络处理器(Network Processor,NP)等;还可以是数字信号处理器(Digital SignalProcessor,DSP)、专用集成电路(Application Specific Integrated Circuit,ASIC)、现场可编程门阵列(Field-Programmable Gate Array,FPGA)或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件。可以实现或者执行本申请实施例中的公开的各方法、步骤及逻辑框图。通用处理器可以是微处理器或者该处理器也可以是任何常规的处理器等。结合本申请实施例所公开的方法的步骤可以直接体现为硬件译码处理器执行完成,或者用译码处理器中的硬件及软件模块组合执行完成。软件模块可以位于随机存储器,闪存、只读存储器,可编程只读存储器或者电可擦写可编程存储器、寄存器等本领域成熟的存储介质中。该存储介质位于存储器,处理器读取存储器中的信息,结合其硬件完成上述方法的步骤。
[0092] 当然,除了软件实现方式之外,本申请的电子设备并不排除其他实现方式,比如逻辑器件抑或软硬件结合的方式等等,也就是说以下处理流程的执行主体并不限定于各个逻辑单元,也可以是硬件或逻辑器件。
[0093] 本申请实施例还提出了一种计算机可读存储介质,该计算机可读存储介质存储一个或多个程序,该一个或多个程序包括指令,该指令当被包括多个应用程序的便携式电子设备执行时,能够使该便携式电子设备执行图1所示实施例的视频剪辑方法,并具体用于执行以下操作:根据获取到的待优化项目对应的项目文件集合,确定已使用文件集合,其中,所述项目文件集合包括所述项目目录下的所有文件;根据所述项目文件集合与所述已使用文件集合,确定未使用文件,并在所述项目文件集合删除所述未使用文件,得到第一文件集合;识别所述第一文件集合中包含的Vue文件,分别确定各所述Vue文件对应的目标UI组件;根据所述目标UI组件,生成针对目标UI组件库的局部引用语句,并根据所述局部引用语句,将所述第一文件集合所包含的入口文件中对所述目标UI组件库的全局引入语句,替换为所述局部引用语句,完成对所述待优化项目的代码优化。
[0094] 本领域内的技术人员应明白,本发明的实施例可提供为方法、系统、或计算机程序产品。因此,本发明可采用完全硬件实施例、完全软件实施例、或结合软件和硬件方面的实施例的形式。而且,本发明可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。
[0095] 本发明是参照根据本发明实施例的方法、设备(系统)、和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。
[0096] 这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能。
[0097] 这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的步骤。
[0098] 在一个典型的配置中,计算设备包括一个或多个处理器 (CPU)、输入 / 输出接口、网络接口和内存。
[0099] 内存可能包括计算机可读介质中的非永久性存储器,随机存取存储器 (RAM) 和 / 或非易失性内存等形式,如只读存储器 (ROM) 或闪存(flash RAM)。内存是计算机可读介质的示例。
[0100] 计算机可读介质包括永久性和非永久性、可移动和非可移动媒体可以由任何方法或技术来实现信息存储。信息可以是计算机可读指令、数据结构、程序的模块或其他数据。计算机的存储介质的例子包括,但不限于相变内存 (PRAM)、静态随机存取存储器 (SRAM)、动态随机存取存储器 (DRAM)、其他类型的随机存取存储器 (RAM)、只读存储器 (ROM)、电可擦除可编程只读存储器 (EEPROM)、快闪记忆体或其他内存技术、只读光盘只读存储器(CD-ROM)、数字多功能光盘 (DVD) 或其他光学存储、磁盒式磁带,磁带磁磁盘存储或其他磁性存储设备或任何其他非传输介质,可用于存储可以被计算设备访问的信息。按照本文中的界定,计算机可读介质不包括暂存电脑可读媒体 (transitory media),如调制的数据信号和载波。
[0101] 还需要说明的是,术语"包括”、"包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、方法、商品或者设备不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、方法、商品或者设备所固有的要素。在没有更多限制的情况下,由语句"包括一个……”限定的要素,并不排除在包括所述要素的过程、方法、商品或者设备中还存在另外的相同要素。
[0102] 本领域技术人员应明白,本申请的实施例可提供为方法、系统或计算机程序产品。因此,本申请可采用完全硬件实施例、完全软件实施例或结合软件和硬件方面的实施例的形式。而且,本申请可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。
[0103] 以上所述仅为本申请的实施例而已,并不用于限制本申请。对于本领域技术人员来说,本申请可以有各种更改和变化。凡在本申请的精神和原理之内所作的任何修改、等同替换、改进等,均应包含在本申请的权利要求范围之内。< / script> < / u-button> < / template>
Claims
1. A code optimization method, characterized in that, include: Based on the obtained set of project files corresponding to the project to be optimized, determine the set of files already used, wherein the set of project files includes all files under the project directory; Based on the project file set and the used file set, unused files are identified and deleted from the project file set to obtain a first file set; Identify the Vue files contained in the first file set, and determine the target UI component corresponding to each Vue file; Based on the target UI component, generate local reference statements for the target UI component library, and based on the local reference statements, replace the global import statements for the target UI component library in the entry files contained in the first file set with the local reference statements, thereby completing the code optimization of the project to be optimized.
2. The method according to claim 1, characterized in that, Obtain the set of project files corresponding to the project to be optimized, specifically including: Traverse the root directory and subdirectories of the project to be optimized; According to the preset matching rules, the paths of all files in the root directory and the subdirectories are obtained respectively, thus obtaining the project file set.
3. The method according to claim 1, characterized in that, The step of determining the used file set based on the obtained set of project files corresponding to the project to be optimized specifically includes: Based on the project file set, determine the source code file corresponding to the project to be optimized; Based on the source code file, determine the reference statements, and based on the reference statements, determine the path of the referenced file; The set of used files is generated based on the referenced file paths.
4. The method according to claim 1, characterized in that, The determination of the target UI component corresponding to each of the Vue files specifically includes: In each of the aforementioned Vue files, look for component identifiers with a specific naming prefix; The target UI component is determined based on the component corresponding to the component identifier.
5. The method according to claim 1, characterized in that, The step of generating local reference statements for the target UI component library based on the target UI component specifically includes: Based on the target UI component corresponding to each Vue file, generate the local import code corresponding to the target UI component; Add the local import code to the script section of the corresponding Vue file to obtain a local reference statement for the target UI component library.
6. The method according to claim 1, characterized in that, The step of replacing the global import statements for the target UI component library in the entry files contained in the first file set with the local reference statements to obtain the project file set corresponding to the project to be optimized specifically includes: In the entry file, determine the global import statement used to globally import the target UI component library; Replace the global import statement with the local import statement.
7. A code optimization device, characterized in that, include: The used file determination unit is used to determine the used file set based on the obtained project file set corresponding to the project to be optimized, wherein the project file set includes all files under the project directory; The unused file deletion unit is used to determine unused files based on the project file set and the used file set, and delete the unused files in the project file set to obtain a first file set; The UI component determination unit is used to identify the Vue files contained in the first file set and determine the target UI component corresponding to each Vue file. The replacement unit is used to generate local reference statements for the target UI component library based on the target UI component, and to replace the global import statements for the target UI component library in the entry files contained in the first file set with the local reference statements based on the local reference statements, thereby completing the code optimization of the project to be optimized.
8. A code optimization device, comprising: processor; as well as A memory configured to store computer-executable instructions, which, when executed, cause the processor to perform the following operations: Based on the obtained set of project files corresponding to the project to be optimized, determine the set of files already used, wherein the set of project files includes all files under the project directory; Based on the project file set and the used file set, unused files are identified and deleted from the project file set to obtain a first file set; Identify the Vue files contained in the first file set, and determine the target UI component corresponding to each Vue file; Based on the target UI component, generate local reference statements for the target UI component library, and based on the local reference statements, replace the global import statements for the target UI component library in the entry files contained in the first file set with the local reference statements, thereby completing the code optimization of the project to be optimized.
9. A computer-readable storage medium storing one or more programs, which, when executed by an electronic device including a plurality of applications, cause the electronic device to perform the code optimization method as described in any one of claims 1-6.
10. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the code optimization method as described in any one of claims 1-6.