Method, apparatus, electronic device and medium for generating code for introducing static resources

Through automated scanning and formatting processing, the code introduced by static resource is solved, and the problems of low manual development efficiency and high error rate are achieved, and efficient and accurate code generation is achieved.

CN118409740BActive Publication Date: 2025-07-08GUANGZHOU HUYA TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410553772.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-05-07
Publication Date
2025-07-08
Estimated Expiration
2044-05-07

AI Technical Summary

Technical Problem

In the prior art, the introduction code related to static resources needs to be developed manually, resulting in low development efficiency and error-prone.

Method used

By scanning the preset directory statically, obtaining resource identifiers and paths, and formatting them, converting the resource paths into relative paths, automatically generating imported code.

Benefits of technology

It improves the development efficiency of code introduction of static resources, reduces the error rate, and improves the efficiency and multiplexing rate of code generation through directory trees and depth-first traversal algorithms.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118409740B_ABST
    Figure CN118409740B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of computer technology, and provides a method, an apparatus, an electronic device and a medium for generating static resource import codes. The method includes: scanning static resources in a preset directory to obtain the resource identifiers and resource paths of the static resources in the preset directory; formatting the resource identifiers to obtain formatted identifiers representing the static resources; converting the resource paths into relative paths relative to the preset working path with reference to the preset working path; and generating import codes for importing the static resources in the preset directory according to the formatted identifiers and the relative paths. The present invention can improve the development efficiency of import codes related to static resources and is not prone to errors.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and more particularly, to a method, apparatus, electronic device, and medium for generating static resource import code. Background Art

[0002] Static resources generally refer to files or data used by an application that are not dynamically generated or changed, such as images, CSS style sheets, JavaScript files, HTML files, font files, etc. Since these resources do not change during the runtime of the application, but are fixed during deployment or compilation.

[0003] Currently, the import code related to static resources is developed manually by developers, with low development efficiency and high error-proneness. Summary of the Invention

[0004] The purpose of the present invention is to provide a method, apparatus, electronic device, and medium for generating static resource import code, which can improve the development efficiency of the import code related to static resources and is not error-prone.

[0005] The embodiments of the present invention can be implemented as follows:

[0006] In a first aspect, the present invention provides a method for generating static resource import code, the method comprising:

[0007] Performing a static resource scan on a preset directory to obtain a resource identifier and a resource path of the static resources in the preset directory;

[0008] Formatting the resource identifier to obtain a formatted identifier representing the static resource;

[0009] Taking a preset working path as a reference, converting the resource path into a relative path relative to the preset working path;

[0010] Generating import code for introducing the static resources in the preset directory according to the formatted identifier and the relative path.

[0011] In an alternative embodiment, the step of performing a static resource scan on a preset directory to obtain a resource identifier and a resource path of the static resources in the preset directory comprises:

[0012] Scanning the preset directory;

[0013] If a target file in the preset format exists in the preset directory, taking the file name of the target file as the resource identifier and the path of the target file as the resource path;

[0014] If there is a target sub-directory in the preset directory and there is a file in the target sub-directory in the preset format, then use the directory name of the target sub-directory as the resource identifier and use the path of the preset file in the target sub-directory as the resource path, where the preset file is used to introduce the file in the target sub-directory in the preset format.

[0015] In an alternative embodiment, the step of formatting the resource identifier to obtain a formatted identifier representing the static resource includes:

[0016] Convert the resource identifier into a to-be-formatted identifier in a preset format;

[0017] Format the to-be-formatted identifier according to a preset naming rule to obtain the formatted identifier.

[0018] In an alternative embodiment, the step of converting the resource identifier into a to-be-formatted identifier in a preset format includes:

[0019] If the first character of the resource identifier is a digit, then add a preset prefix before the first character of the resource identifier to obtain the to-be-formatted identifier;

[0020] If the first character of the resource identifier is not a digit, then directly use the resource identifier as the to-be-formatted identifier.

[0021] In an alternative embodiment, the method further includes:

[0022] Obtain a preset configuration file, where the preset configuration file includes a plurality of target directories, and there is at least one static resource to be introduced in each target directory;

[0023] Generate a directory tree according to the hierarchical relationship between the plurality of target directories, where each node in the directory tree represents a target directory, the hierarchical relationship between the target directory represented by each node and the target directories represented by the child nodes of this node is an inclusion relationship, and the hierarchical relationship between the target directories represented by the nodes belonging to the same level is a parallel relationship;

[0024] Obtain the target directory represented by any node in the directory tree as the preset directory, and execute the step of performing a static resource scan on the preset directory to obtain the resource identifier and resource path of the static resource in the preset directory, and generate an introduction code for introducing the static resource in the preset directory;

[0025] Traverse each target directory represented by a node in the directory tree as the preset directory until an introduction code for introducing the static resource in each target directory represented by a node in the directory tree is generated.

[0026] In an alternative embodiment, the step of traversing each target directory represented by a node in the directory tree as the preset directory includes:

[0027] Starting from the root node of the directory tree, access each node of the directory tree in turn according to the depth-first traversal algorithm. If the current node is a node visited for the first time, use the target directory represented by the current node as the preset directory.

[0028] In an alternative embodiment, after generating the introduction code for introducing the static resources according to the formatting identifier and the relative path, the method further includes:

[0029] Write the introduction code into a preset file and store the preset file in the preset directory.

[0030] In a second aspect, the present invention provides an apparatus for generating an introduction code for static resources, the apparatus including:

[0031] A scanning module, configured to perform a static resource scan on a preset directory to obtain a resource identifier and a resource path of the static resources in the preset directory;

[0032] A processing module, configured to format the resource identifier to obtain a formatting identifier representing the static resources;

[0033] The processing module is further configured to convert the resource path into a relative path relative to the preset working path with reference to the preset working path;

[0034] A generating module, configured to generate an introduction code for introducing the static resources in the preset directory according to the formatting identifier and the relative path.

[0035] In a third aspect, the present invention provides an electronic device, including a processor and a memory, where the memory is used to store a program, and the processor is configured to implement the method for generating an introduction code for static resources according to any one of the foregoing embodiments when executing the program.

[0036] In a fourth aspect, the present invention provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the method for generating an introduction code for static resources according to any one of the foregoing embodiments is implemented.

[0037] The present invention automatically obtains the resource identifier and resource path of static resources by scanning, then formats the resource identifier and converts the resource path into a relative path, and automatically generates an introduction code to introduce the static resources according to the formatting identifier and the relative path, avoiding the problems of low development efficiency and easy errors caused by developers manually developing the introduction code to introduce the static resources. Brief Description of the Drawings

[0038] To more clearly illustrate the technical solutions of the embodiments of the present invention, the following will briefly introduce the drawings required for the embodiments. It should be understood that the following drawings only show some embodiments of the present invention, and thus should not be regarded as limiting the scope. For those of ordinary skill in the art, without creative efforts, other relevant drawings can be obtained based on these drawings.

[0039] Figure 1 It is a flowchart example of the method for generating static resource import code provided in this embodiment.

[0040] Figure 2 It is a structural example diagram of the directory tree provided in this embodiment.

[0041] Figure 3 It is an overall flowchart example of the import code provided in this embodiment.

[0042] Figure 4 It is an example of the automatically generated import code provided in this embodiment.

[0043] Figure 5 It is a block diagram of the device for generating static resource import code provided in this embodiment.

[0044] Figure 6 It is a block diagram of the electronic device 10 provided in this embodiment.

[0045] Icons: 10 - Electronic device; 11 - Processor; 12 - Memory; 13 - Bus; 100 - Device for generating static resource import code; 110 - Scanning module; 120 - Processing module; 130 - Generating module; 140 - Traversing module. Detailed Description of the Embodiments

[0046] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the drawings in the embodiments of the present invention. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. Usually, the components of the embodiments of the present invention described and illustrated in the drawings here can be arranged and designed in various different configurations.

[0047] Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the claimed present invention, but merely represents the selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present invention.

[0048] It should be noted that like reference numerals and letters refer to like items in the following figures, and thus, once an item is defined in one figure, it will not be necessary to further define or explain it in subsequent figures.

[0049] In the description of the present invention, it should be noted that if terms such as "upper", "lower", "inner", "outer", etc. are used to indicate the orientation or positional relationship, it is based on the orientation or positional relationship shown in the figures, or the orientation or positional relationship in which the product of the present invention is usually placed during use. This is only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and thus should not be construed as a limitation to the present invention.

[0050] In addition, terms such as "first", "second", etc. are only used for distinguishing descriptions and cannot be understood as indicating or implying relative importance.

[0051] It should be noted that, without conflict, the features in the embodiments of the present invention can be combined with each other.

[0052] Please refer to Figure 1 , Figure 1 which is a flowchart example of the method for generating the code for introducing static resources provided in this embodiment. The method includes the following steps:

[0053] Step S101: Perform a static resource scan on a preset directory to obtain the resource identifiers and resource paths of the static resources in the preset directory.

[0054] In this embodiment, the preset directory can be directly specified by the user or can be pre-written by the user in a configuration file, and then read by this embodiment from the configuration file. The preset directory can be one or multiple. The static resources in any two of the multiple preset directories do not overlap. In addition to including the preset directory, the configuration file can also include a preset working directory representing the business that uses the static resources, and can also specify an output directory. The code file of the generated introduction code is stored in the output directory. For example, the content of the configuration file is as follows:

[0055]

[0056] Among them, "__dirname" represents the preset working directory. The preset directory specified by "shared" is all directories under the ". / img-income" directory except for '. / app / *' and '. / web / *'. The preset directory specified by "app" is '. / img-income / app', and the preset directory specified by "web" is '. / img-income / web'. It can be seen that there are no overlapping static resources among the preset directories "shared", "app", and "web". outputDir is the specified output directory '. / src / config', that is, the generated import code files are stored in the directory '. / src / config'.

[0057] In this embodiment, static resources include, but are not limited to, images, CSS style sheets, JavaScript files, HTML files, font files, etc. Taking images as an example, scanning the preset directory for static resources can be to scan the extensions of files representing image formats in the preset directory. The extensions include, but are not limited to,.png,.jpeg, etc. The resource identifier is used to uniquely represent the static resource, and the resource identifier can be a string composed of one or more letters, numbers, symbols, etc. The resource path is the absolute path of the static resource. The absolute path is a path that completely describes the location of the static resource. The specification of the absolute path starts from the root directory at the top of the tree directory structure to a certain directory or file, consisting of a series of consecutive directories separated by slashes until the static resource to be specified. The last name in the path is the static resource to be pointed to.

[0058] Step S102: Format the resource identifier to obtain a formatted identifier representing the static resource.

[0059] In this embodiment, in order to make the resource identifier meet the code writing rules of the import code, it needs to be formatted. For example, if the import code requires the resource identifier to adopt the camel case naming method, the obtained formatted identifier conforms to the camel case naming method.

[0060] Step S103: With the preset working path as a reference, convert the resource path into a relative path relative to the preset working path.

[0061] In this embodiment, the relative path refers to the path relationship with other files (or directories) caused by the path where the static resource is located. For example, the absolute path of the static resource is: c: / website / img / photo.jpg, and the preset working path is c: / website / test. Then the relative path of the static resource relative to the preset working path can be expressed as:.. / img / photo.jpg. To improve the generality of the introduced code, when the preset working path changes, there is no need for the user to manually modify it correspondingly. Convert the resource path to a relative path relative to the preset working path, and use the relative path of the static resource when generating the introduced code.

[0062] Step S104, generate introduced code for introducing static resources in the preset directory according to the formatting identifier and the relative path.

[0063] In this embodiment, a preset template can be set in advance according to the rules of the introduced code, and then the formatting identifier and the relative path are filled into the preset template according to the preset template, and finally the introduced code is obtained.

[0064] The above method provided in this embodiment automatically obtains the resource identifier and resource path of the static resource by scanning the static resource, then formats the resource identifier and converts the resource path into a relative path. According to the formatting identifier and the relative path, automatically generate introduced code to introduce the static resource, avoiding the problems of low development efficiency and easy errors caused by developers manually developing introduced code to introduce the static resource.

[0065] In an alternative embodiment, the preset directory can be a directory directly including static resources, or a directory including subdirectories, and the subdirectories include directories of static resources. These two processing methods are different. This embodiment gives an implementation manner of scanning the preset directory for static resources to obtain the resource identifier and resource path of the static resources in the preset directory:

[0066] First, scan the preset directory;

[0067] Then, if there is a target file in the preset directory in the preset format, use the file name of the target file as the resource identifier and the path of the target file as the resource path; if there is a target subdirectory in the preset directory and there is a file in the preset format in the target subdirectory, use the directory name of the target subdirectory as the resource identifier and the path of the preset file in the target subdirectory as the resource path. The preset file is used to introduce the file in the preset format in the target subdirectory.

[0068] In this embodiment, the preset format can be determined according to the type of the static resource. For example, if the type of the static resource is an image type, the preset format is an image format; if the type of the static resource is a CSS type, the preset format is a CSS file.

[0069] In this embodiment, in order to facilitate determining the target subdirectory in the preset directory that contains files with the preset format, the import code for the static resources introduced into the subdirectory can be placed in a preset file. For example, if the preset file is index.js, and there is an index.js in the subdirectory of the preset directory, it means that the subdirectory contains static resources, and the static resources of this subdirectory can be introduced through the index.js file.

[0070] The preset directory can be one of the following multiple situations:

[0071] Situation 1: The preset directory includes multiple target files and does not include target subdirectories

[0072] At this time, the resource identifier includes the resource identifiers of all target files in the preset directory, and the resource path is the paths of all target files;

[0073] Situation 2: The preset target includes multiple target subdirectories and does not include target files

[0074] For each target subdirectory, a preset file index.js will be generated for it. At this time, the resource identifier includes the index.js of all target subdirectories, and the resource path is the path of the index.js of the target subdirectory;

[0075] Situation 3: The preset target includes target files and target subdirectories

[0076] At this time, the resource identifier includes the resource identifiers of all target files and the index.js of all target subdirectories, and the resource path is the paths of all target files and the paths of the index.js of all target subdirectories.

[0077] It should be noted that the preset directory can include multi-level subdirectories, and each level of subdirectory belongs to one of the above three situations. At this time, starting from the deepest-level subdirectory, the code file corresponding to the import code of this subdirectory can be generated, and then from deep to shallow in turn, the code files of the import codes of each subdirectory are generated step by step, and finally the code file of the import code of the preset directory is generated. For example, for the preset directory:.. / test1 / test11 / test111, first generate the code file of the import code of the subdirectory test111, then generate the code file of the import code of the subdirectory test11, and finally generate the code file of the import code of test1.

[0078] In an alternative implementation, in order to facilitate the unified processing of resource identifiers, the resource identifiers are first uniformly converted into a preset format, and then the resource identifiers in the preset format are formatted. One implementation method is:

[0079] First, convert the resource identifier into a to-be-formatted identifier in a preset format;

[0080] In this embodiment, the resource identifier may include numbers and / or non-numbers. For resource identifiers whose first character is a number and those whose first character is a non-number, the processing methods are different. Specifically: If the first character of the resource identifier is a number, add a preset prefix before the first character of the resource identifier to obtain the to-be-formatted identifier; if the first character of the resource identifier is a non-number, directly use the resource identifier as the to-be-formatted identifier.

[0081] In this embodiment, the preset prefix can be set according to the rule of introducing code. For example, set the preset prefix to "_". For example, if the resource identifier is "1.png", the converted to-be-formatted identifier is "_1.png".

[0082] Secondly, format the to-be-formatted identifier according to the preset naming rule to obtain the formatted identifier.

[0083] In this embodiment, the preset command rule can be the camel case naming rule, that is: 1. The first letter of all words is capitalized, including the first word. 2. There are no spaces or underscores between words. As an implementation method, the to-be-formatted identifier can be multiple words connected by "-" or "_". Take the strings separated by "-" or "_" in the to-be-formatted identifier as one word, and convert the first letter of each word into the corresponding capital letter. For example, if the to-be-formatted identifier is: bg-tab.png, the corresponding formatted identifier is bgTab. Another example, if the to-be-formatted identifier is: _11.png, the corresponding formatted identifier is _11.

[0084] In an alternative embodiment, for static resources that need to be introduced in multiple directories and there is an inclusion relationship among the multiple directories, in order to generate the introduction code more efficiently, this embodiment also provides an implementation method:

[0085] First, obtain a preset configuration file. The preset configuration file includes multiple target directories, and there is at least one static resource to be introduced in each target directory;

[0086] In this embodiment, there is at least one static resource to be introduced in each target directory. It can be that the target directory directly includes static resources, or one or more levels of subdirectories of the target directory include static resources.

[0087] Secondly, generate a directory tree according to the hierarchical relationship among the multiple target directories. Each node in the directory tree represents a target directory. The hierarchical relationship between the target directory represented by each node and the target directories represented by the child nodes of this node is an inclusion relationship, and the hierarchical relationship between the target directories represented by the nodes belonging to the same level is a parallel relationship;

[0088] Please refer to Figure 2 , Figure 2 which is a structural example diagram of the directory tree provided for this embodiment. Figure 2 In it, test is the root node, and the nodes app, web, and share are in a parallel relationship. today is a child node of share, and the two are in an inclusion relationship, that is, share includes today. The app directory includes two static resources a1.png and a2.png. The web directory and the today directory are similar. The share directory includes the today directory and a static resource s1.png.

[0089] Third, obtain the target directory represented by any node in the directory tree as the preset directory, and perform the step of static resource scanning on the preset directory to obtain the resource identifier and resource path of the static resources in the preset directory, and generate the introduction code for introducing the static resources in the preset directory;

[0090] In this embodiment, for the target directory represented by any node in the directory tree, use it as the preset directory, and perform the above steps S101 - S104 or the above steps S101 - S104 and their specific implementation methods to obtain the introduction code of this preset directory.

[0091] Fourth, traverse the target directory represented by each node in the directory tree as the preset directory until the introduction code for introducing the static resources in the target directory represented by each node in the directory tree is generated.

[0092] Since the directory tree is organized in a tree structure, in order to facilitate traversing all nodes in the directory tree and obtaining the introduction code of the target directory represented by each node, one traversal method is:

[0093] Start from the root node of the directory tree, and sequentially visit each node of the directory tree according to the depth - first traversal algorithm. If the current node is the first - visited node, then use the target directory represented by the current node as the preset directory.

[0094] In this embodiment, when the depth - first traversal algorithm traverses the directory tree, a node may be traversed multiple times. To avoid unnecessary repeated processing and improve processing efficiency, when a node is first traversed, perform the above steps S101 - S104 or the above steps S101 - S104 and their specific implementation methods on it to obtain its introduction code. After that, when the node is traversed again, no repeated processing will be performed on it.

[0095] In an alternative implementation, to facilitate the reference of the upper - level directory of the preset directory, this embodiment also provides another implementation method:

[0096] Write the import code into a preset file and store the preset file in a preset directory.

[0097] For the parent directory of the preset directory, it can introduce static resources of the preset directory through the preset file.

[0098] To illustrate the whole process of generating the import code, please refer to Figure 3 , Figure 3 which is the overall flow example diagram of the import code provided in this embodiment. Figure 3 In [the reference], a scaffolding tool is used to read the directory where the static resources belong from the configuration file and generate a directory tree according to the directory structure. Figure 3 It includes 3 nodes: dir-1, dir-2, and dir-3. Scan the directory represented by each node in the directory tree to obtain the target folder where the static resources to be imported are located in the corresponding directory. Perform a depth-first traversal on each node in the directory tree output by the scaffolding tool, and perform rendering processing on each traversed node. After the processing of each node is completed, an index.js file will be generated in the corresponding folder. This file contains the import code of all static resources in the corresponding folder, and it is referenced by the index.js of the parent node. The rendering processing mainly includes: (1) converting the absolute path of the static resource file into a relative path; (2) uniformly formatting the file name and converting it into the camel case naming method; (3) exporting a key-value object, where the key represents the file name in the camel case naming method, and the value is the corresponding file path object for the import. Generate the import code according to the rendering result, and finally generate the import code file corresponding to the root directory.

[0099] It should be noted that Figure 3 this is just an example of an implementation method and an application scenario. In fact, in the actual application scenario, the directory tree may include more or fewer directories, and may also include more levels of directories. The scaffolding tool can also be obtained by developers making adaptability improvements to the existing scaffolding tool, or can be replaced by function code developed by developers that can implement the corresponding functions.

[0100] To more intuitively illustrate the generated import code, please refer to Figure 4 , Figure 4 which is an example of the automatically generated import code provided in this embodiment. Figure 4 In [the reference], index.js in the web directory is the code file of the import code generated by using the method provided in this embodiment, and the content of the file is as Figure 4 shown.

[0101] To execute the corresponding steps in the above embodiments and all possible implementation manners, an implementation manner of an apparatus 100 for generating static resource import code is given below. Please refer to Figure 5 , Figure 5 which is a block diagram of the apparatus for generating static resource import code provided in this embodiment. It should be noted that for the apparatus 100 for generating static resource import code provided by the present invention, its basic principle and the generated technical effects are the same as those of the corresponding above embodiments. For the sake of brief description, they are not mentioned in this embodiment.

[0102] The apparatus 100 for generating static resource import code includes a scanning module 110, a processing module 120, a generating module 130, and a traversing module 140.

[0103] The scanning module 110 is configured to perform static resource scanning on a preset directory to obtain a resource identifier and a resource path of the static resources in the preset directory.

[0104] The processing module 120 is configured to format the resource identifier to obtain a formatted identifier representing the static resource.

[0105] The processing module 120 is further configured to convert the resource path into a relative path relative to the preset working path with reference to the preset working path.

[0106] The generating module 130 is configured to generate import code for importing the static resources in the preset directory according to the formatted identifier and the relative path.

[0107] In an alternative implementation manner, the scanning module 110 is specifically configured to: scan the preset directory; if there is a target file in the preset format in the preset directory, use the file name of the target file as the resource identifier and the path of the target file as the resource path; if there is a target sub-directory in the preset directory and there is a file in the preset format in the target sub-directory, use the directory name of the target sub-directory as the resource identifier and the path of the preset file in the target sub-directory as the resource path, where the preset file is used to import the file in the preset format in the target sub-directory.

[0108] In an alternative implementation manner, the processing module 120 is specifically configured to: convert the resource identifier into a to-be-formatted identifier in a preset format; format the to-be-formatted identifier according to a preset naming rule to obtain the formatted identifier.

[0109] In an alternative implementation manner, when the processing module 120 is used to convert the resource identifier into a to-be-formatted identifier in a preset format, it is specifically configured to: if the first character of the resource identifier is a number, add a preset prefix before the first character of the resource identifier to obtain the to-be-formatted identifier; if the first character of the resource identifier is not a number, directly use the resource identifier as the to-be-formatted identifier.

[0110] In an alternative embodiment, the traversal module 140 is configured to: obtain a preset configuration file, where the preset configuration file includes a plurality of target directories, and there is at least one static resource to be introduced in each target directory; generate a directory tree according to the hierarchical relationship between the plurality of target directories, where each node in the directory tree represents a target directory, and the hierarchical relationship between the target directory represented by each node and the target directories represented by the child nodes of this node is an inclusion relationship, and the hierarchical relationship between the target directories represented by the nodes belonging to the same level is a parallel relationship; obtain the target directory represented by any node in the directory tree as a preset directory, and perform a static resource scan on the preset directory to obtain the resource identifiers and resource paths of the static resources in the preset directory, and generate introduction code for introducing the static resources in the preset directory; traverse the target directories represented by each node in the directory tree as preset directories until introduction code for introducing the static resources in the target directories represented by each node in the directory tree is generated.

[0111] In an alternative embodiment, the traversal module 140 is specifically configured to: start from the root node of the directory tree, and sequentially access each node of the directory tree according to the depth-first traversal algorithm. If the current node is the first node to be accessed, use the target directory represented by the current node as the preset directory.

[0112] In an alternative embodiment, the generation module 130 is further configured to: write the introduction code into a preset file, and store the preset file in a preset directory.

[0113] An embodiment of the present invention further provides a block diagram of the electronic device 10. For the electronic device 10 to implement the method for generating static resource introduction code in the foregoing embodiment or the method for generating static resource introduction code in the foregoing embodiment, please refer to Figure 6 , Figure 6 which is the block diagram of the electronic device 10 provided in this embodiment. The electronic device 10 includes a processor 11, a memory 12, and a bus 13. The processor 11 and the memory 12 are connected through the bus 13.

[0114] The processor 11 may be an integrated circuit chip having signal processing capability. In the implementation process, each step of the method for generating static resource introduction code in the above embodiment may be completed by hardware integrated logic circuits or software instructions in the processor 11. The above processor 11 may be a general-purpose processor, including a CPU (Central Processing Unit), a NP (Network Processor), etc.; it may also be a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Logic Gate Array) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components.

[0115] The memory 12 is used to store the method for generating static resource introduction code of the aforementioned embodiment or the program for generating static resource introduction code of the aforementioned embodiment. The program can be a software function module stored in the memory 12 in the form of software or firmware or solidified in the OS (Operating System) of the electronic device 10.

[0116] After receiving the execution instruction, the processor 11 executes the program to implement the method for generating static resource introduction code of the aforementioned embodiment or the method for generating static resource introduction code of the aforementioned embodiment.

[0117] An embodiment of the present invention provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the method for generating a static resource introduction code as described in any one of the aforementioned implementations is implemented.

[0118] In summary, the embodiments of the present invention provide a method, apparatus, electronic device, and medium for generating static resource import code. The method includes: scanning static resources in a preset directory to obtain the resource identifiers and resource paths of the static resources in the preset directory; formatting the resource identifiers to obtain formatted identifiers representing the static resources; converting the resource paths into relative paths relative to the preset working path with reference to the preset working path; and generating import code for importing the static resources in the preset directory according to the formatted identifiers and the relative paths. Compared with the prior art, the embodiments of the present invention have at least the following advantages: (1) It can improve the development efficiency of the import code related to static resources and is not prone to errors; (2) Using a tree structure to organize multi-level directories into a directory tree and traversing the directory tree through a depth-first algorithm improves the directory tree traversal efficiency and ultimately improves the generation efficiency of the import code; (3) By setting a configuration file, the method provided in this embodiment can be used to automatically generate import code, improving the code reuse rate.

[0119] As described above, the above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by those skilled in the art within the technical scope disclosed by the present invention should be covered by the protection scope of the present invention. Therefore, the protection scope of the present invention should be subject to the protection scope of the claims.

Claims

1. A method for generating code for introducing static resources, characterized in that The method includes: Performing a static resource scan on a preset directory to obtain the resource identifier and resource path of the static resources in the preset directory; wherein, if the preset directory includes multiple target subdirectories and does not include target files, the resource identifier includes the preset files corresponding to all the target subdirectories, and the resource path is the path of the preset file corresponding to the target subdirectory; the preset file is used to introduce files in the preset format in the target subdirectory; if the preset directory includes target files and target subdirectories, the resource identifier includes the resource identifiers of all the target files and the preset files of all the target subdirectories, and the resource path is the path of all the target files and the path of the preset files of all the target subdirectories; formatting the resource identifier to obtain a formatted identifier representing the static resources; Taking the preset working path as a reference, converting the resource path into a relative path relative to the preset working path; Generating an introduction code for introducing the static resources in the preset directory according to the formatted identifier and the relative path.

2. The method for generating the static resource import code according to claim 1, wherein The step of performing a static resource scan on a preset directory to obtain the resource identifier and resource path of the static resources in the preset directory includes: Scanning the preset directory; If there is a target file in the preset format in the preset directory, using the file name of the target file as the resource identifier and the path of the target file as the resource path; If there is a target subdirectory in the preset directory and there is a file in the preset format in the target subdirectory, using the directory name of the target subdirectory as the resource identifier and the path of the preset file in the target subdirectory as the resource path.

3. The method for generating the static resource import code according to claim 1, wherein The step of formatting the resource identifier to obtain a formatted identifier representing the static resources includes: Converting the resource identifier into a to-be-formatted identifier in a preset format; Formatting the to-be-formatted identifier according to a preset naming rule to obtain the formatted identifier.

4. The method for generating the static resource introduction code according to claim 3, wherein, The step of converting the resource identifier into a to-be-formatted identifier in a preset format includes: If the first character of the resource identifier is a number, adding a preset prefix before the first character of the resource identifier to obtain the to-be-formatted identifier; If the first character of the resource identifier is non-numeric, directly using the resource identifier as the to-be-formatted identifier.

5. The method for generating the static resource import code according to claim 1, wherein The method further includes: Obtaining a preset configuration file, where the preset configuration file includes multiple target directories, and there is at least one static resource to be introduced in each target directory; Generating a directory tree according to the hierarchical relationship between the multiple target directories, where each node in the directory tree represents a target directory, the hierarchical relationship between the target directory represented by each node and the target directories represented by the child nodes of this node is an inclusion relationship, and the hierarchical relationship between the target directories represented by the nodes belonging to the same level is a parallel relationship; Obtain the target directory represented by any node in the directory tree as the preset directory, and execute the step of performing a static resource scan on the preset directory to obtain the resource identifiers and resource paths of the static resources in the preset directory, and generate an import code for importing the static resources in the preset directory; Traverse the target directory represented by each node in the directory tree as the preset directory until an import code for importing the static resources in the target directory represented by each node in the directory tree is generated.

6. The method for generating the static resource import code according to claim 5, wherein The step of traversing the target directory represented by each node in the directory tree as the preset directory includes: Starting from the root node of the directory tree, sequentially access each node of the directory tree according to the depth-first traversal algorithm. If the current node is the first node to be accessed, use the target directory represented by the current node as the preset directory.

7. The method for generating the code for introducing static resources according to claim 1, wherein After generating the import code for importing the static resources according to the formatting identifier and the relative path, the method further includes: Write the import code into a preset file, and store the preset file in the preset directory.

8. An apparatus for generating code for introducing static resources, characterized in that The device includes: A scanning module, configured to perform a static resource scan on a preset directory to obtain the resource identifiers and resource paths of the static resources in the preset directory; wherein, if the preset directory includes multiple target subdirectories and does not include target files, the resource identifier includes the preset files corresponding to all target subdirectories, and the resource path is the path of the preset file corresponding to the target subdirectory; the preset file is used to import files in a preset format in the target subdirectory; if the preset directory includes target files and target subdirectories, the resource identifier includes the resource identifiers of the preset files of all target files and all target subdirectories, and the resource path is the paths of all target files and the paths of the preset files of all target subdirectories; A processing module, configured to format the resource identifier to obtain a formatting identifier representing the static resource; The processing module is further configured to convert the resource path into a relative path relative to the preset working path with reference to the preset working path; A generating module, configured to generate an import code for importing the static resources in the preset directory according to the formatting identifier and the relative path.

9. An electronic device, characterized in that, It includes a processor and a memory. The memory is used to store a program, and the processor is configured to implement the method for generating a static resource import code according to any one of claims 1-7 when executing the program.

10. A computer-readable storage medium, characterized in that, A computer program is stored thereon, and when the computer program is executed by a processor, it implements the method for generating a static resource import code according to any one of claims 1-7.