A method and apparatus for packaging code for multi-scenario applications

By dynamically adjusting the build context by obtaining business scenario identifiers and using a conditional compilation engine to parse the code module set, the problem of large package size and low compilation efficiency caused by full compilation in cross-platform application development is solved, and efficient and concise multi-scenario application package generation is achieved.

CN123114530APending Publication Date: 2026-11-17TONGCHENG NETWORK TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611291034.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-25
Publication Date
2026-11-17

AI Technical Summary

Technical Problem

In current cross-platform application development, the full compilation mode results in excessively large application package size, low compilation efficiency, and high maintenance costs in multiple scenarios.

Method used

By acquiring business scenario identifiers, dynamically loading scenario configuration information, using a conditional compilation engine to parse the code module set and adjust the build context, only compiling necessary modules, and generating differentiated application packages.

Benefits of technology

It significantly reduces code maintenance costs for multi-scenario applications, improves compilation efficiency, and optimizes application package size and loading performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123114530A_ABST
    Figure CN123114530A_ABST
Patent Text Reader

Abstract

This application provides a method and apparatus for packaging code for multi-scenario applications, relating to the field of computer software development technology. The method includes: obtaining a packaging request for a target application, the packaging request carrying a business scenario identifier; loading corresponding scenario configuration information from a preset dynamic configuration library based on the business scenario identifier; parsing the scenario configuration information using a conditional compilation engine to determine a set of code modules and build parameters matching the business scenario identifier; dynamically adjusting the build context based on the set of code modules and the build parameters in the compilation and build process of a cross-platform development framework, to instruct the build tool to compile only the source code contained in the set of code modules; and executing the compilation process through the build tool to generate a target application package adapted to the business scenario identifier. This application improves the packaging efficiency of multi-scenario applications and effectively reduces the package size.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer software development, and more specifically, to a method and apparatus for packaging code for multi-scenario applications. Background Technology

[0002] In current cross-platform application development, developers typically use a unified codebase to adapt to multiple runtime platforms and different business scenarios. However, most existing build tools adopt a full compilation mode, meaning that regardless of which functional modules the target scenario actually requires, all accessible code modules in the project are included in the compilation process. When a project needs to support multiple differentiated business scenarios simultaneously (such as a main application version, a lightweight promotion version, a brand-customized version, etc.), this full compilation method results in the generated application package containing a large amount of redundant code that is not needed in the current scenario. This not only increases the size of the application package and affects loading performance, but also requires a large amount of computing resources to process invalid modules for each packaging for a specific scenario, leading to low compilation efficiency. In addition, to adapt to different scenarios, developers are often forced to maintain multiple code branches, further exacerbating the problems of difficult code synchronization and high maintenance costs. Summary of the Invention

[0003] The purpose of this application is to provide a code packaging method and apparatus for multi-scenario applications, which solves the above-mentioned problems existing in the prior art. By dynamically adjusting the build context according to the business scenario identifier in the compilation and build process, it realizes the on-demand trimming of modules based on the same set of source code and automatic generation of differentiated application packages, which significantly reduces the code maintenance cost of multi-scenario applications and improves the compilation efficiency.

[0004] Firstly, a method for packaging code for multi-scenario applications is provided, which may include: Obtain a packaging request for the target application, wherein the packaging request carries a business scenario identifier; Based on the business scenario identifier, load the corresponding scenario configuration information from the preset dynamic configuration library; The scenario configuration information is parsed using a conditional compilation engine to determine the set of code modules and construction parameters that match the business scenario identifier; In the compilation and build process of the cross-platform development framework, the build context is dynamically adjusted based on the code module set and the build parameters to instruct the build tool to compile only the source code contained in the code module set; The build tool performs a compilation process to generate a target application package adapted to the business scenario identifier.

[0005] In one possible implementation, the scenario configuration information includes at least one of page routing configuration, application global configuration, feature switches, and environment variables; The step of determining the set of code modules that match the business scenario identifier based on the scenario configuration information includes: Based on the page routing configuration, the page file paths required for the current business scenario are selected; Based on the aforementioned functional characteristic switch, determine the enabled or disabled state of the controlled code block in the source code; Based on the application's global configuration and the environment variables, generate compile-time constants to replace placeholders in the source code.

[0006] In one possible implementation, determining the set of code modules matching the business scenario identifier based on the scenario configuration information includes: Using the page file path specified in the scenario configuration information as the entry point, recursively trace the lower-level modules referenced by each entry module, and construct a module dependency graph during the recursive tracing process; All modules in the module dependency graph that have a reference link with any entry module are included in the code module set; Modules in the module dependency graph that have no reference links with any entry modules are marked as modules to be pruned.

[0007] In one possible implementation, the conditional compilation process includes: Inject the compile-time constants corresponding to the business scenario identifier into the abstract syntax tree of the source code; Traverse the abstract syntax tree to identify conditional code segments associated with the compile-time constants; Based on the current value of the compile-time constant, retain the matching conditional code segments and discard the non-matching conditional code segments.

[0008] In one possible implementation, the business scenario identifier includes at least one combination of the target operating platform identifier, the environment type identifier, and the business tenant identifier; After generating the target application package adapted to the business scenario identifier, the method further includes: Output a packaging report, which includes a list of modules included in this packaging and the size information of each module.

[0009] In one possible implementation, the compilation process performed by the build tools also includes: Identify asynchronous modules declared using dynamic import syntax in the source code; Based on the scenario configuration information corresponding to the business scenario identifier, an asynchronous sub-packet splitting strategy is determined. The splitting strategy includes at least one of the following: number of sub-packets, sub-packet naming rules, and maximum sub-packet size. According to the splitting strategy, each asynchronous module is assigned to a corresponding asynchronous sub-package, and the routing information of each asynchronous sub-package is written into the global configuration file of the target application, so that the target application loads the corresponding asynchronous sub-package on demand at runtime.

[0010] In one possible implementation, traversing the abstract syntax tree and identifying conditional code segments associated with the compile-time constants further includes: Identify platform files in the source files that exist according to multi-end file naming conventions, wherein the filenames of the platform files contain a target operating platform identifier suffix; Based on the target operating platform identifier in the business scenario identifier, select the target file that matches the target operating platform from multiple platform files corresponding to the same module, and remove the platform files that do not correspond to the target operating platform from the code module set.

[0011] Secondly, a code packaging device for multi-scenario applications is provided, the device may include: The acquisition unit is used to acquire a packaging request for the target application, wherein the packaging request carries a business scenario identifier; The loading unit is used to load the corresponding scenario configuration information from the preset dynamic configuration library according to the business scenario identifier; The determination unit is used to parse the scenario configuration information using a conditional compilation engine to determine the set of code modules and construction parameters that match the business scenario identifier. In the compilation and build process of the cross-platform development framework, the build context is dynamically adjusted based on the code module set and the build parameters to instruct the build tool to compile only the source code contained in the code module set; The build tool performs a compilation process to generate a target application package adapted to the business scenario identifier.

[0012] Thirdly, an electronic device is provided, which includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; When a processor executes a program stored in memory, it implements any of the steps described in the first aspect above.

[0013] Fourthly, a computer-readable storage medium is provided, wherein a computer program is stored therein, and when executed by a processor, the computer program implements the steps of any of the methods described in the first aspect above.

[0014] The technical solution provided in this application obtains a packaging request carrying a business scenario identifier and loads the corresponding scenario configuration information. It then uses a conditional compilation engine to parse out a set of code modules and build parameters that precisely match the scenario. This allows for dynamic adjustment of the build context within the cross-platform development framework's compilation and build process, instructing the build tool to compile only the source code within that specific set of code modules. This mechanism changes the traditional full-scale compilation model, transforming the differentiated configuration of business scenarios into physical constraints during compilation. This enables the build tool to skip non-target modules during module parsing and conversion, thus eliminating the compilation overhead of invalid code at the source. Compared to solutions that only eliminate dead code during code generation, this application completes module-level trimming at an earlier build context adjustment stage, significantly improving the packaging efficiency of multi-scenario applications. Simultaneously, it ensures that the generated target application package contains only the minimum code set necessary for the current scenario, effectively reducing package size and solving the technical problems of high maintenance costs and redundant artifacts when reusing codebases across multiple scenarios. Attached Figure Description

[0015] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0016] Figure 1 A flowchart illustrating a code packaging method for multi-scenario applications provided in this application embodiment; Figure 2 A schematic diagram of the structure of a code packaging device for multi-scenario applications provided in this application embodiment; Figure 3 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation

[0017] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of the embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.

[0018] In the field of cross-platform application development, when the same project needs to adapt to multiple business scenarios (such as different brands, channels, or customers), existing technologies generally adopt the approach of maintaining separate code branches or independent projects for each scenario. This approach leads to a large amount of redundant common code, and feature changes require simultaneous modifications across multiple branches, with maintenance costs increasing linearly with the number of scenarios. Meanwhile, existing build tools lack the ability to tailor code to specific needs, requiring a full compilation for each build, and cannot dynamically combine modules based on scenario differences, resulting in low compilation efficiency. Furthermore, when multiple platforms and multiple scenarios are combined, the complexity of branch management and adaptation increases exponentially, severely restricting the development efficiency and iteration speed of multi-scenario applications.

[0019] Taking cross-platform application development based on the Taro framework as an example, developers typically need to maintain multiple business scenarios simultaneously (such as the main application version, a lightweight promotion version, and a brand-customized version) and adapt them to multiple operating platforms such as WeChat Mini Programs, Alipay Mini Programs, and H5. In existing technologies, developers often achieve differentiated outputs by maintaining separate code branches or independent projects for different scenarios. This results in the same common functionality needing to be repeatedly modified in multiple code repositories, with maintenance costs increasing linearly with the number of scenarios. Simultaneously, Taro's default build tool uses a full compilation mode, including all accessible code modules in the project in the compilation process, regardless of which functional modules the target scenario actually requires. This not only increases the application package size but also leads to low compilation efficiency. When multi-platform adaptation and multi-scenario customization are combined, the complexity of branch management and packaging increases exponentially, severely restricting the development efficiency of multi-scenario applications.

[0020] Therefore, this application provides a code packaging method for multi-scenario applications to solve the above-mentioned problems in the prior art. By dynamically adjusting the build context according to the business scenario identifier in the compilation and build process, it realizes the on-demand trimming of modules based on the same set of source code and automatic generation of differentiated application packages, which significantly reduces the code maintenance cost of multi-scenario applications and improves compilation efficiency.

[0021] The preferred embodiments of this application are described below with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are for illustration and explanation only and are not intended to limit this application. Furthermore, the embodiments and features in the embodiments of this application can be combined with each other without conflict.

[0022] Example 1: Figure 1 This is a flowchart illustrating a method for packaging multi-scenario application code, provided in an embodiment of this application. This method is applied to the compilation and build environment of a cross-platform development framework, aiming to solve the problems of low compilation efficiency and redundant artifacts when reusing code libraries across multiple scenarios. Figure 1 As shown, the method may include: Step S110: Obtain the packaging request for the target application.

[0023] The package request includes a business scenario identifier.

[0024] Specifically, the business scenario identifier is a feature data used to uniquely determine the form of the application to be generated by the current packaging task. In practical applications, the business scenario identifier can be a single-dimensional identifier, such as specifying only the target operating platform (e.g., WeChat Mini Program, H5, React Native, etc.); or it can be a composite identifier, such as consisting of "target operating platform, environment type (development / test / production), and business tenant (brand A / brand B)". Packaging requests can be triggered in various ways, either manually by developers entering specific commands in the command-line interface, or automatically by the continuous integration / continuous deployment (CI / CD) process based on code commit events or scheduled tasks. In the implementation based on the Taro framework, packaging requests are triggered through extended Taro CLI commands. Developers can enter commands containing the target platform identifier and the business scenario identifier in the command-line interface, such as executing `taro build --type weapp --scene lite` to initiate a lightweight promotional packaging task for the WeChat Mini Program platform. Taro CLI, acting as the entry point for receiving requests, first parses the command-line parameters, extracting the target operating platform corresponding to the `--type` parameter (e.g., weapp for WeChat Mini Programs, alipay for Alipay Mini Programs, h5 for web pages), the business scenario identifier corresponding to the `--scene` parameter (e.g., main, lite, brand, etc., custom scenarios), and the environment type corresponding to the optional `--mode` parameter (e.g., development environment, production environment). It then passes the parsed standardized parameters to the subsequent configuration loading and compilation process, achieving precise routing of the packaging task. This embodiment, by receiving requests carrying clear business scenario identifiers, provides a precise input benchmark for subsequent differentiated compilation, avoiding errors in full compilation or manual selection caused by ambiguous scenario definitions in traditional solutions.

[0025] Step S120: Load the corresponding scenario configuration information from the preset dynamic configuration library according to the business scenario identifier.

[0026] The dynamic configuration library is a collection storing differentiated definitions for all business scenarios. It can adopt a hierarchical configuration structure, containing a basic configuration applicable to all scenarios and multiple dedicated configurations corresponding to different business scenarios. Upon receiving a packaging request, the system first identifies the business scenario identifier, then retrieves and loads the dedicated configuration file matching that identifier from the dynamic configuration library. In this embodiment, the dynamic configuration library is organized as a file system directory. A `scenes / dedicated` directory is created in the project root directory to store differentiated configuration files for different scenarios. This directory contains subdirectories that correspond one-to-one with the business scenario identifier, such as `scenes / main / ` for the main application version, `scenes / lite / ` for the lightweight promotion version, and `scenes / brand / ` for the brand-customized version. Each scenario subdirectory contains the scenario-specific mini-program configuration file `app.config.ts` and customized page resources. When the system loads the configuration, it first reads the project's basic configuration file (such as config / index.ts), then reads the specific configuration file in the target scene's subdirectory. A field-level deep merging strategy is used to integrate the two: for array-type configurations (such as page route lists), an append or overwrite strategy is used; for object-type configurations (such as application identifiers and theme variables), a recursive overwrite strategy is used. This ultimately generates complete and unique scene configuration information for the current scene, eliminating the need to maintain a separate full configuration file for each scene. Subsequently, the system deeply merges the specific configuration with the basic configuration to generate complete scene configuration information for the current scene. This merging mechanism supports field-level overwrite and appending. For example, it can append scene-specific page paths to the general page list defined in the basic configuration, or overwrite the default application name and application ID. In this way, developers do not need to maintain a separate complete configuration file for each scene; they only need to maintain the differences, significantly reducing the complexity and maintenance cost of configuration management.

[0027] Step S130: Use the conditional compilation engine to parse the scenario configuration information and determine the set of code modules and construction parameters that match the business scenario identifier.

[0028] It's important to note that the "parsing" here is not simply reading configuration information, but a proactive decision-making process that includes pre-construction static dependency analysis. Before the module packaging tool's compilation and build process officially begins, the conditional compilation engine performs static scanning and analysis of the project's source code based on the loaded scenario configuration information (especially the page routing configuration and feature switches). Specifically, the engine uses the page file path specified in the scenario configuration as the root node and recursively traces the sub-modules referenced by each entry module, thereby constructing a module dependency graph for the current scenario in memory. Based on this dependency graph, the engine can accurately identify which modules are essential for the current scenario (i.e., included in the code module set) and which modules are irrelevant to the current scenario (i.e., modules to be trimmed). Simultaneously, the engine generates compile-time constants based on the application's global configuration and environment variables to replace placeholders in the source code; these constants serve as the basis for subsequent conditional compilation. This pre-construction analysis step is crucial; it pre-solidifies dependencies that would otherwise need to be dynamically explored during compilation, providing deterministic data support for the next step of dynamically adjusting the build context, ensuring the accuracy and completeness of trimming decisions.

[0029] Step S140: In the compilation and build process of the cross-platform development framework, the build context is dynamically adjusted based on the code module set and build parameters to instruct the build tool to only compile the source code contained in the code module set.

[0030] The build context refers to the global state and configuration environment that the module packaging tool relies on when executing compilation tasks, including but not limited to module parsing rules, entry point definitions, and loader chain configurations. In this embodiment, the system uses the set of code modules determined in step S130 to modify the default build context. For example, the entry point of the module packaging tool can be redirected to a specific module in the set of code modules, or filtering rules can be injected into the configuration of the module parser, so that when the parser processes a module reference request, it can directly determine whether the target module belongs to the set of code modules. For non-target modules that do not belong to the set, the parser will return preset placeholder data (such as an empty module or an empty object), thereby physically blocking them from entering the subsequent code transformation (Loader) and code generation process. It should be understood that this context-based physical blocking mechanism is fundamentally different from the traditional Tree Shaking technique: Tree Shaking usually occurs in the code generation stage, at which point all modules have completed the time-consuming code transformation, and only unused exports are removed from the final product; while in this embodiment, by adjusting the context in the parsing stage, non-target modules do not undergo expensive transformation operations such as syntax parsing, transpilation, and compression at all. This strategy not only significantly reduces CPU and memory consumption during the compilation process, but also completely avoids the risk of the entire packaging task failing due to syntax errors or missing dependencies in non-target modules, thus significantly improving the stability and efficiency of packaging in multiple scenarios.

[0031] Step S150: Perform compilation processing using the build tool to generate a target application package adapted to the business scenario identifier.

[0032] After dynamically adjusting the build context, the module packaging tool executes the standard compilation process according to the adjusted context. Since non-target modules are effectively intercepted during the parsing phase, the scope of source code actually processed by the build tool is strictly limited to the code module set. The final output target application package contains only the minimum code set required for the current business scenario, and the application configurations within the package (such as AppID, page routing tables, etc.) are consistent with the scenario configuration information. During the artifact output phase, the build tool outputs the compiled code resources to differentiated directories named "target platform + business scenario." For example, the lightweight promotion version artifact for the WeChat Mini Program platform is output to the dist / weapp-lite / directory, and the main application version artifact for the Alipay Mini Program platform is output to the dist / alipay-main / directory, achieving physical isolation between artifacts from different scenarios and platforms, facilitating subsequent deployment and distribution. Simultaneously, the system dynamically generates platform-specific configuration files based on scenario configuration information: for the mini-program platform, it automatically generates an app.json file containing the current scenario's AppID, page routing table, and window style configuration, as well as a project.config.json file containing the project's private configuration; for the H5 platform, it dynamically generates a manifest.json file containing variables such as the API base address and application name, ensuring that the final product's application identity and behavioral logic fully comply with the current scenario's customization requirements. Furthermore, the compilation process can synchronously generate a packaging report, recording the list of modules included in this package and the size information of each module, facilitating developers to verify the trimming effect and perform performance analysis. Through the above process, this embodiment achieves a complete closed loop from configuration parsing, dependency analysis, context adjustment to precise compilation, maximizing the optimization of compilation performance and product quality while ensuring the correctness of multi-scenario application functions.

[0033] Example 2 Building upon Example 1, this example further details the mechanism for using a conditional compilation engine to parse scenario configuration information to determine the specific implementation of the code module set. As one implementation method, scenario configuration information includes at least one of page routing configuration, application global configuration, feature switches, and environment variables. Specifically, page routing configuration not only defines the application's navigation structure but also serves as the physical boundary starting point for module dependency analysis in this application. For example, in the configuration for a lightweight business scenario, the page routing configuration may only include path identifiers for the homepage and product details page, excluding page paths present in the full version, such as the personal center and settings page. Feature switches are Boolean configuration items used to control the enabling or disabling of specific business logic, such as "whether to enable the membership points function" or "whether to display advertising banners." These switches are converted into definite true or false values ​​during the parsing phase, thereby determining the compilation status of controlled code blocks in the source code. Application global configuration and environment variables contain metadata such as application identifier (AppID), application name, API base address, and current runtime environment type. The system generates compile-time constants based on this information to replace placeholders in the source code. It is important to emphasize that the compile-time constants here are different from runtime environment variables. They are constants that are statically replaced with specific literal values ​​during the packaging and building phase. This allows subsequent conditional compilation to directly eliminate mismatched branch code at the syntax tree level, rather than making dynamic judgments at runtime, thereby fundamentally reducing the size of the output and improving runtime efficiency.

[0034] Based on the aforementioned scenario configuration information, the process of determining the set of code modules matching the business scenario identifier is essentially a precise mapping process from logical configuration to physical modules. According to the page routing configuration, the page file paths required for the current business scenario are filtered out. For example, when the business scenario identifier points to "promotion version," the system extracts the landing page path and registration page path specific to that scenario from the configuration, parses these paths into the corresponding absolute paths of the source code files, and uses them as the root node set for subsequent dependency tracing. Simultaneously, based on feature switches, the enabled or disabled status of controlled code blocks in the source code is determined; and based on application global configuration and environment variables, compile-time constants are generated to replace placeholders in the source code. This step ensures that before building the dependency graph, the system has mastered all static constraints affecting module reachability, laying the data foundation for subsequent precise trimming.

[0035] To transform discrete page paths into a complete set of code modules, this embodiment employs a dependency graph construction strategy based on reference chains. Starting with the page file path specified in the scene configuration information, the system recursively traces the lower-level modules referenced by each entry module, constructing a module dependency graph during this recursive tracing process. Specifically, the conditional compilation engine uses each selected page file path as its traversal starting point, employing depth-first search or breadth-first search algorithms to parse the import statements (such as `import`, `require`, etc.) within that page module, identifying its directly dependent submodules (including components, utility functions, style files, static resources, etc.). For each identified submodule, the engine continues to recursively parse its own dependencies until all reachable leaf nodes have been traversed or a previously visited module has been encountered. During this process, the engine maintains a directed graph in memory, recording the reference relationships between modules. It should be understood that this recursive tracing is strictly limited to the entry point defined in the current scene configuration, fundamentally different from the traditional full-package approach that traverses the entire project starting from a single fixed entry point (such as `app.tsx`).

[0036] After constructing the dependency graph, the system includes all modules in the module dependency graph that have a reference link with any entry module into the code module set. This means that as long as a module is directly or indirectly referenced by any page configured in the current scenario, regardless of whether it is also referenced by other unconfigured scenario pages, the module will be determined as a valid module required by the current scenario and will be retained. For example, a generic button component may be referenced by both "Home" and "Personal Center". When packaging a lightweight scenario that only includes "Home", the button component will still be correctly included in the code module set because it exists in the reference link of "Home" and will not be mistakenly deleted because "Personal Center" is not configured. This mechanism ensures the security and integrity of shared modules when reused in multiple scenarios.

[0037] Accordingly, the system marks modules that have no reference links with any entry modules in the module dependency graph as modules to be pruned. These modules to be pruned are usually pages unique to other business scenarios and their own dependency chains. For example, suppose the full codebase contains a "Management Backend" page and its own dependencies such as "Permission Verification Tool" and "Data Report Component". When the current packaging request is for a "lightweight" scenario for ordinary users, the "Management Backend" page will not be an entry point for dependency tracking because it does not appear in the page routing configuration for this scenario. Therefore, the "Management Backend" page itself, as well as the "Permission Verification Tool" and "Data Report Component" that are only referenced by this page, will not appear in the built module dependency graph, or although they are found in the global scan, they have no connection path with the current entry point, and are thus automatically marked as modules to be pruned. This marking result will be directly passed to the build context adjustment stage, instructing the module packaging tool to return placeholder data for these modules during the parsing phase, so that it can completely skip the subsequent code transformation and packaging process. Through this binary decision mechanism based on reference links, this embodiment achieves a deterministic mapping from configuration intent to physical module set. This avoids the tediousness and error-proneness of manually maintaining the module whitelist, while ensuring the accuracy and completeness of the trimming results, providing solid intermediate layer support for the efficient packaging of multi-scenario applications.

[0038] Example 3 Building upon the file-level physical interception achieved through module dependency analysis in Examples 1 and 2, this example further elaborates on the fine-grained conditional compilation processing performed during the code conversion phase. It should be understood that the aforementioned examples addressed the question of "whether the entire module needs to participate in compilation," while this example targets source code files that have been included in the code module set but still contain redundant cross-platform or cross-scenario logic, performing deeper line-level code trimming. Specifically, this conditional compilation processing includes injecting compile-time constants corresponding to business scenario identifiers into the abstract syntax tree of the source code. During the translation of the source code by the module packaging tool, the conditional compilation engine first reads the compile-time constants determined in the current build context (e.g., SCENE_LITE=true, PLATFORM_WEAPP=true, etc. generated by scenario configuration), and directly replaces these constants in the corresponding nodes of the abstract syntax tree in literal form through the syntax tree conversion plugin. This injection operation occurs before code generation, ensuring that subsequent logical judgments can be based on determined static values, rather than relying on runtime dynamic variable readings.

[0039] Subsequently, the abstract syntax tree (AST) is traversed to identify conditional code segments related to compile-time constants. The conditional compilation engine performs a depth-first traversal of the AST, searching for all nodes containing conditional expressions, such as if statements, ternary expressions, or logical AND / OR operations. When a conditional expression is found to have an injected compile-time constant as a factor, the engine statically evaluates the conditional expression based on the current value of that constant. For example, if the source code contains the structure "if(process.env.SCENE === 'lite') { ...} else { ...}", and the currently injected compile-time constant indicates that the scenario is indeed 'lite', then the conditional expression is evaluated as true at compile time. Based on this static evaluation result, the engine retains the matching conditional code segments (i.e., the code within the if branch) and directly removes the non-matching conditional code segments (i.e., the code within the else branch) from the AST. It is important to emphasize that this removal is a physical deletion; the removed code segment will not appear in the final generated bytecode or object file. This is fundamentally different from the traditional approach of retaining conditional judgments for runtime execution. The conditional compilation process described above applies not only to JavaScript / TypeScript source code but also to style files. While traversing the abstract syntax tree, the conditional compilation engine synchronously parses style files (such as .css, .scss, .less, etc.) in the project, identifying conditional compilation directives based on compile-time constants in the style code. For example, when the source code is marked with platform-specific style blocks (e.g., / * #if process.env.TARO_ENV === 'weapp' * / ) or scene-specific style blocks (e.g., / * #if process.env.SCENE === 'brand' * / ), the engine statically evaluates the injected compile-time constants, retaining only the style rules matching the target platform and the current scene, and directly removing style code from other branches. This style-level conditional compilation mechanism avoids redundant styles from different platforms or scenarios being packaged into the final output, further reducing the application package size and avoiding style conflict issues. In this way, not only is the application package size significantly reduced, but runtime conditional branching overhead is also eliminated, improving application execution efficiency.

[0040] As one implementation method, the process of traversing the abstract syntax tree to identify conditional code segments related to compile-time constants also includes a fine-grained selection mechanism for multi-platform adaptation files. Specifically, it identifies platform files in the source files that adhere to multi-platform file naming conventions, where the filename of the platform file contains a target platform identifier suffix. In cross-platform development frameworks, to adapt to the differentiated APIs or UI performance of different platforms, developers typically write multiple platform-specific files for the same functional module, following specific naming conventions. For example, for a payment component named "Payment," the project may contain three files: "Payment.weapp.tsx" (WeChat Mini Program version), "Payment.h5.tsx" (web version), and "Payment.tsx" (general default version). When the build tool processes module import requests, the conditional compilation engine scans the candidate file list in the same directory, parsing whether the filename contains a preset platform identifier suffix.

[0041] Next, based on the target runtime platform identifier in the business scenario identifier, the engine selects the target file that matches the target runtime platform from multiple platform files corresponding to the same module, and removes platform files that do not correspond to the target runtime platform from the code module set. For example, when the target runtime platform specified in the current packaging request is WeChat Mini Program, the engine will prioritize matching and selecting "Payment.weapp.tsx" as the actual compilation object for this module, while excluding other platform-specific files such as "Payment.h5.tsx" from the scope of this compilation. If no platform-specific file is found, it will fall back to selecting the general default file. This mechanism essentially implements platform-based conditional compilation at the file reference resolution level. It works in conjunction with the aforementioned code block-level conditional compilation based on abstract syntax trees to form a dual fine-grained trimming defense of "file selection + code elimination". Through this combined strategy, the system can ensure that the final generated target application package is not only concise at the module level, but also contains only the minimum code units necessary for the current platform and scenario within each module, completely avoiding the redundant accumulation of multi-platform adaptation code in a single platform output.

[0042] Example 4 Building upon Examples 1 to 3, this example further expands the dimensions of the business scenario identifier and the feedback mechanism for the packaging results. As one implementation, the business scenario identifier includes at least one combination of the target runtime platform identifier, environment type identifier, and business tenant identifier. Specifically, the target runtime platform identifier distinguishes the underlying carrier on which the application runs, such as WeChat Mini Programs, Alipay Mini Programs, H5 web pages, or React Native native applications; the environment type identifier distinguishes different stages in the software development lifecycle, such as development environment, testing environment, pre-release environment, or production environment; and the business tenant identifier distinguishes different customer entities or brands served by the same set of code, such as a customized version for brand A, a customized version for brand B, or a general standard version. In practical applications, these three dimensions can be orthogonally combined to form a matrix-like scenario definition; for example, "WeChat Mini Program + Production Environment + Brand A" constitutes a unique business scenario identifier. This multi-dimensional identifier design enables the system to flexibly respond to complex business needs. Developers do not need to create independent code branches or configuration files for every subtle scenario difference; they can dynamically generate corresponding application packages simply by combining different identifier dimensions, greatly improving code reusability and maintenance efficiency.

[0043] To enhance the observability of the packaging process and verify the trimming effect, after generating the target application package adapted to the business scenario identifier, a packaging report is also output. This report includes a list of modules included in the packaging and the size information of each module. Specifically, the packaging report is a structured data file automatically generated after the compilation and build process is complete. It records in detail the physical path, original code size, compressed size, and percentage of the total package size for each code module in the final output. For example, the report might list entries such as "pages / home / index.js: 12KB (3.5%)" and "components / Button.js: 4KB (1.1%)", while also summarizing the number of modules successfully trimmed and their estimated size savings. The technical effect of this mechanism is to solve the "trimming black box" problem in traditional dynamic packaging solutions. Without a packaging report, developers often find it difficult to confirm whether configuration changes have truly taken effect or to intuitively determine which modules are taking up excessive space. By providing a volume perspective accurate to the module level, the packaging report not only allows developers to instantly verify whether the pruning strategy corresponding to the business scenario identifier meets expectations, but also provides a quantitative basis for subsequent package size optimization. For example, when it is found that a non-core utility library still occupies a large volume in a lightweight scenario, developers can adjust feature switches or refactor dependencies accordingly to achieve continuous performance iteration. It should be understood that although this embodiment uses a report containing a module list and volume information as an example, in other embodiments, the packaging report may further include compilation time statistics, dependency graph visualization snapshots, or pruning warning information, etc. As long as it can provide feedback data on the packaging process, it falls within the protection scope of this application.

[0044] Example 5 Building upon the previous embodiments that primarily focused on the selection and trimming of synchronous code modules, this embodiment further optimizes asynchronous resource loading within the application. As one implementation method, the build tool's compilation process also includes identifying asynchronous modules declared using dynamic import syntax in the source code. Specifically, dynamic import syntax refers to code loading methods distinct from static import statements, such as using import() function calls or asynchronous component loading APIs provided by specific frameworks. Unlike the synchronous modules in the previous embodiments that were retained or excluded based on dependency graphs, asynchronous modules typically correspond to non-initial screen pages, pop-up components, or on-demand loaded functional plugins. Their characteristic is that they are not executed immediately during application initialization but are loaded only when specific triggering conditions are met. Therefore, the processing logic for these modules is not a simple binary trimming but requires reasonable physical splitting and organization while preserving their functional usability to balance package size and loading performance.

[0045] Based on this, the asynchronous sub-packet splitting strategy is determined according to the scenario configuration information corresponding to the business scenario identifier. The splitting strategy includes at least one of the following: the number of sub-packets, the sub-packet naming rules, and the maximum sub-packet size. This feature reflects the dynamic adaptability of the sub-packet mechanism in this application. In traditional technical solutions, asynchronous sub-packet strategies are often globally fixed or hard-coded in the build configuration, and cannot perceive specific differences in business scenarios. In this embodiment, however, the splitting strategy is directly driven by the business scenario identifier carried by the current packaging request. For example, for the "lightweight" scenario, the scenario configuration information may specify a stricter maximum sub-packet size (e.g., 50KB) and adopt a flat sub-packet naming rule to ensure fast response on low-end devices or in weak network environments; while for the "full-feature professional version" scenario, the configuration information may allow a larger sub-packet size (e.g., 200KB) and adopt a hierarchical naming rule divided by business domain to utilize the browser caching mechanism to improve the switching speed of subsequent pages. Furthermore, the limit on the number of sub-packages can also vary depending on the scenario. For example, in some mini-program platform scenarios that are sensitive to the number of concurrent requests, the configuration information can forcibly limit the maximum number of sub-packages, forcing the build tool to merge and package multiple small modules, thereby avoiding platform throttling due to excessive requests at runtime. This dynamic strategy ensures that the same set of source code can generate the optimal resource organization structure for different distribution channels and runtime environments.

[0046] Furthermore, according to the splitting strategy, each asynchronous module is assigned to its corresponding asynchronous sub-package, and the routing information of each asynchronous sub-package is written into the target application's global configuration file, enabling the target application to load the corresponding asynchronous sub-package on demand at runtime. Specifically, once the build tool determines the current splitting strategy, it aggregates eligible asynchronous modules into independent physical files during the code generation phase. More importantly, the system automatically extracts the mapping relationship between these newly generated asynchronous sub-packages and their original logical paths, serializes this mapping relationship, and injects it into the target application's global configuration file (such as app.json or manifest.json). This step is the core of the technical closed loop for achieving correct runtime loading. Because during compilation, the original module reference path has been converted into the new sub-package file path, without this dynamically generated routing mapping table, the application will not be able to locate the correct code resources at runtime, leading to functional failure. By synchronously generating and binding routing information with sub-package artifacts, this embodiment not only achieves flexible splitting at compile time but also ensures the determinism and reliability of runtime loading behavior. It should be understood that although this embodiment uses the number of sub-packages, naming rules, and volume limit as examples to illustrate the content of the splitting strategy, in other embodiments, the splitting strategy may also include sub-package priority, preloading rules, or dependency sharing strategies, as long as it can dynamically adjust the organization of asynchronous resources according to the scenario configuration information, it falls within the protection scope of this application.

[0047] To more intuitively demonstrate the effectiveness of the technical solution of this application in practical applications, this embodiment provides a comprehensive application scenario example. It should be noted that the following scenario is only for illustrating the implementation effect of the technical solution of this application and is not intended to limit the scope of protection of this application. In practical applications, developers can flexibly define scenario dimensions and configuration parameters according to specific business needs.

[0048] In this application scenario, assume a cross-platform application project maintains three business models simultaneously based on the same source code repository: a main application version, a lightweight promotion version, and a brand customized version. The main application version includes all twenty pages and all functional modules; the lightweight promotion version only includes two core pages—the homepage and product details page—and disables membership points and ad push functionality; the brand customized version replaces specific theme resources and third-party login interfaces with those found in the main application version. For each of these models, the system maintains corresponding scenario configuration information in a pre-defined dynamic configuration library. For example, in the lightweight promotion version's scenario configuration information, the page routing configuration only declares the path identifiers for the homepage and product pages, the feature switches mark membership points and ad push as disabled, and the target operating platform is specified as WeChat Mini Program.

[0049] When developers need to release a lightweight promotional version, they enter a packaging command through the command-line interface. This command carries the business scenario identifier `scene=lite` and the target runtime platform identifier `type=weapp`. Upon receiving this packaging request, the system first loads the lightweight promotional version's dedicated configuration file from the dynamic configuration library and deeply merges it with the project's basic configuration to generate complete scenario configuration information for the current scenario. Subsequently, the conditional compilation engine intervenes before the compilation and build process begins, recursively tracing module reference relationships using the homepage and product page file paths specified in the configuration as root nodes, constructing a module dependency graph containing only these two pages and their associated components and utility functions. During this process, pages not declared in the configuration, such as the personal center page, settings page, and management backend page, as well as dedicated dependency modules referenced only by these pages, are automatically marked as modules to be pruned because they do not exist in the current entry point's reference chain.

[0050] Next, the system dynamically adjusts the build context of the module packaging tool based on the defined set of code modules. Specifically, during the module parsing phase, when the packaging tool attempts to parse file paths marked as modules to be trimmed, the custom parsing plugin directly returns preset empty module placeholder data, allowing these modules to completely skip subsequent syntax transpilation, style compilation, and code compression processes. Simultaneously, for source files already included in the code module set, the conditional compilation engine traverses their abstract syntax tree during the code transformation phase, statically evaluating the injected compile-time constants SCENE_LITE=true and PLATFORM_WEAPP=true, removing the H5 platform-specific adaptation branches and conditional code blocks for the membership points function from the source code. Finally, the build tool only performs compilation processing on the streamlined code set, generating a lightweight promotional application package adapted for the WeChat Mini Program platform.

[0051] To verify the trimming effect, the system automatically generated a packaging report after packaging. The report showed that the package contained 12 code modules, with a total compressed size of 48KB. The homepage module accounted for 35%, the product page module for 28%, and the general basic library for 37%. The report also clearly listed the modules that were successfully trimmed, including 8 unrelated pages and their associated 15 proprietary components and utility functions. In comparison, under the traditional full-package mode without this solution, the WeChat Mini Program package size for the same project was 125KB, with a compilation time of 42 seconds; while after adopting this solution, the lightweight promotional version's package size was reduced to 48KB, a reduction of 61.6%, and the compilation time was shortened to 23 seconds, an efficiency improvement of approximately 45%. The physical mechanism behind this significant difference is that while traditional solutions can remove unused exports through Tree Shaking during the code generation stage, all modules still need to undergo a complete parsing and conversion process, and the computational overhead is not reduced. In contrast, this application directly excludes a large number of invalid modules from the compilation pipeline by physically blocking them during the parsing stage, thereby achieving a dual optimization of compilation performance and output size.

[0052] It should be understood that the above-described lightweight promotional version scenario is merely a specific application example of this application. In other implementations, the business scenario identifier can be further combined with dimensions such as environment type (e.g., development / production) and business tenant (e.g., brand A / brand B) to form a more complex matrix packaging strategy. For example, for the combined scenario of "production environment + brand A", the system can automatically load the corresponding theme configuration and API address, and inject the corresponding environment variables during compilation to achieve write-once, multi-morphic distribution. Regardless of how the scenario changes, its underlying implementation follows the core logic of configuration parsing, dependency trimming, context adjustment, and precise compilation described in this application. Therefore, the specific parameters and data shown in this embodiment should not be regarded as the sole limitation of the technical solution of this application, and any scenario adaptation and parameter adjustment based on the core concept of this application should be covered within the protection scope of this application.

[0053] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

[0054] like Figure 2 As shown, this embodiment provides a code packaging device for multi-scenario applications. This device serves as a structured carrier for the aforementioned multi-scenario application code packaging method, achieving precise control over the compilation and building process through functional modularization. The device includes an acquisition unit, a loading unit, a determination unit, and a compilation and execution section.

[0055] The acquisition unit 210 is used to acquire packaging requests for the target application, which carry business scenario identifiers. Specifically, the acquisition unit is the entry point for system interaction with the outside world, and can take the form of a command-line interface parser, a graphical user interface component, or a network API gateway. When a developer or automated pipeline initiates a packaging task, the acquisition unit is responsible for receiving the raw instruction stream and extracting the key business scenario identifier parameters. This device not only supports single-dimensional identifier parsing but also handles composite identifiers composed of platform type, environment stage, and tenant ID, ensuring that subsequent processing units can receive standardized scenario input data. Through the standardized parsing of the acquisition unit, the differences in instruction formats between different triggering sources are masked, providing a unified starting benchmark for the entire packaging process.

[0056] Loading unit 220 is used to load corresponding scenario configuration information from a preset dynamic configuration library based on the business scenario identifier. Specifically, the loading unit maintains a connection channel with the dynamic configuration library, which can be a local file system directory, a remote configuration center, or database storage. After receiving the business scenario identifier, the loading unit performs configuration retrieval and merging operations: first, it locates the exclusive configuration file matching the identifier, and then deeply merges it with the basic configuration file to generate a complete scenario configuration information object that is only effective for the current scenario. This process involves field-level overriding, array concatenation, and recursive merging of nested objects, ensuring that the final output configuration information includes both general default values ​​and accurately reflects the differentiated customization content of the current scenario. The output of the loading unit directly determines the boundary conditions for subsequent code trimming and serves as a bridge connecting abstract business intent and specific technical implementation.

[0057] Unit 230 is used to parse scenario configuration information using a conditional compilation engine to determine the set of code modules and build parameters that match the business scenario identifier. Specifically, the determination unit is the core decision-making center of this device. Its built-in conditional compilation engine performs pre-static dependency analysis before the compilation process officially starts. Starting with the page routing configuration in the scenario configuration information, the determination unit recursively traces module reference relationships, constructs a complete module dependency graph under the current scenario, and divides the set of code modules and modules to be trimmed accordingly. At the same time, the determination unit is also responsible for generating compile-time constants based on the application's global configuration and environment variables. These constants will serve as the basis for static evaluation in the subsequent code transformation stage. Through the preprocessing of the determination unit, the dependencies that originally needed to be dynamically explored during the compilation process are solidified into a deterministic list of modules in advance, providing accurate data support for adjusting the build context.

[0058] In the compilation and build process of cross-platform development frameworks, the build context is dynamically adjusted based on the code module set and build parameters to instruct the build tool to compile only the source code contained in the code module set. Specifically, this functionality is typically implemented by the compilation control module in the device or the extended interface of the deterministic unit. This part is responsible for transforming the code module set output by the deterministic unit into a context constraint that the module packaging tool can recognize, such as modifying the entry point configuration, injecting module parsing filtering rules, or registering custom parsing plugins. When the build tool attempts to parse a non-target module, this context constraint triggers an interception mechanism and returns preset placeholder data, thereby physically blocking invalid modules from entering the subsequent code transformation pipeline. This context-based dynamic adjustment mechanism allows the same build tool to adapt to various drastically different packaging scenarios without modifying its own source code.

[0059] The build tool performs compilation processing to generate a target application package adapted to the business scenario identifier. Specifically, after the build context is adjusted, the device calls the underlying module packaging tool to perform actual compilation, translation, compression, and resource copying operations according to the established pipeline. Since non-target modules have been effectively isolated during the parsing phase, the actual processing scope of the build tool is strictly limited to the code module set, thereby significantly reducing the consumption of computing resources. The final generated target application package not only achieves a minimized size, but also maintains a high degree of consistency with the current business scenario identifier in its internal application configuration, page routing table, and sub-package structure. It should be understood that although this embodiment describes each function as an independent unit or module, in actual engineering implementation, these units can be merged, split, or deployed in a distributed manner. As long as the whole can achieve the above functional logic, it falls within the protection scope of this application. Through the above device architecture, this embodiment solidifies each step in the method flow into a reusable software component, which not only improves the maintainability and scalability of the system, but also provides stable structured support for the efficient packaging of multi-scenario applications.

[0060] The functions of each functional unit in the code packaging device for multi-scenario applications provided in the above embodiments of this application can be implemented through the above-described method steps. Therefore, the specific working process and beneficial effects of each unit in the code packaging device for multi-scenario applications provided in the embodiments of this application will not be repeated here.

[0061] This application also provides an electronic device, such as... Figure 3 As shown, it includes a processor 310, a communication interface 320, a memory 330, and a communication bus 340, wherein the processor 310, the communication interface 320, and the memory 330 communicate with each other through the communication bus 340.

[0062] Memory 330 is used to store computer programs; When the processor 310 executes the program stored in the memory 330, it performs the following steps: Obtain a packaging request for the target application, wherein the packaging request carries a business scenario identifier; Based on the business scenario identifier, load the corresponding scenario configuration information from the preset dynamic configuration library; The scenario configuration information is parsed using a conditional compilation engine to determine the set of code modules and construction parameters that match the business scenario identifier; In the compilation and build process of the cross-platform development framework, the build context is dynamically adjusted based on the code module set and the build parameters to instruct the build tool to compile only the source code contained in the code module set; The build tool performs a compilation process to generate a target application package adapted to the business scenario identifier.

[0063] The communication bus mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.

[0064] The communication interface is used for communication between the aforementioned electronic devices and other devices.

[0065] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.

[0066] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.

[0067] The implementation methods and beneficial effects of the various components of the electronic device in the above embodiments for solving the problem can be found in [reference needed]. Figure 1 The steps in the illustrated embodiments are used to implement the electronic device. Therefore, the specific working process and beneficial effects of the electronic device provided in this application will not be repeated here.

[0068] In another embodiment provided in this application, a computer-readable storage medium is also provided, which stores instructions that, when executed on a computer, cause the computer to execute a code packaging method for a multi-scenario application as described in any of the above embodiments.

[0069] In another embodiment provided in this application, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute a code packaging method for a multi-scenario application as described in any of the above embodiments.

[0070] Those skilled in the art will understand that the embodiments in this application can be provided as methods, systems, or computer program products. Therefore, the embodiments in this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the embodiments in this application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0071] This application describes embodiments of methods, apparatus (systems), and computer program products according to embodiments of this application with reference to flowchart illustrations and / or block diagrams. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0072] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0073] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0074] Unless otherwise defined, the technical or scientific terms used in this application shall have the ordinary meaning understood by one of ordinary skill in the art to which this invention pertains. The terms "first," "second," and similar terms used in this application do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Terms such as "comprising" or "including" mean that the element or object preceding the word encompasses the elements or objects listed following the word and their equivalents, without excluding other elements or objects. Terms such as "connected," "coupled," or "linked" are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. Terms such as "upper," "lower," "left," and "right" are used only to indicate relative positional relationships; when the absolute position of the described object changes, the relative positional relationship may also change accordingly.

[0075] Although preferred embodiments have been described in this application, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the embodiments in this application are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of the embodiments in this application.

[0076] Obviously, those skilled in the art can make various modifications and variations to the embodiments of this application without departing from the spirit and scope of the embodiments of this application. Therefore, if these modifications and variations to the embodiments of this application fall within the scope of the embodiments of this application and their equivalents, then these modifications and variations are also intended to be included in the embodiments of this application.

Claims

1. A method for packaging code for multi-scenario applications, characterized in that, The method includes: Obtain a packaging request for the target application, wherein the packaging request carries a business scenario identifier; Based on the business scenario identifier, load the corresponding scenario configuration information from the preset dynamic configuration library; The scenario configuration information is parsed using a conditional compilation engine to determine the set of code modules and construction parameters that match the business scenario identifier; In the compilation and build process of the cross-platform development framework, the build context is dynamically adjusted based on the code module set and the build parameters to instruct the build tool to compile only the source code contained in the code module set; The build tool performs a compilation process to generate a target application package adapted to the business scenario identifier.

2. The method according to claim 1, characterized in that, The scenario configuration information includes at least one of the following: page routing configuration, application global configuration, feature switches, and environment variables. The step of determining the set of code modules that match the business scenario identifier based on the scenario configuration information includes: Based on the page routing configuration, the page file paths required for the current business scenario are selected; Based on the aforementioned functional characteristic switch, determine the enabled or disabled state of the controlled code block in the source code; Based on the application's global configuration and the environment variables, generate compile-time constants to replace placeholders in the source code.

3. The method as described in claim 2, characterized in that, The step of determining the set of code modules that match the business scenario identifier based on the scenario configuration information includes: Using the page file path specified in the scenario configuration information as the entry point, recursively trace the lower-level modules referenced by each entry module, and construct a module dependency graph during the recursive tracing process; All modules in the module dependency graph that have a reference link with any entry module are included in the code module set; Modules in the module dependency graph that have no reference links with any entry modules are marked as modules to be pruned.

4. The method as described in claim 1, characterized in that, The conditional compilation process includes: Inject the compile-time constants corresponding to the business scenario identifier into the abstract syntax tree of the source code; Traverse the abstract syntax tree to identify conditional code segments associated with the compile-time constants; Based on the current value of the compile-time constant, retain the matching conditional code segments and discard the non-matching conditional code segments.

5. The method as described in claim 1, characterized in that, The business scenario identifier includes at least one combination of the target operating platform identifier, environment type identifier, and business tenant identifier; After generating the target application package adapted to the business scenario identifier, the method further includes: Output a packaging report, which includes a list of modules included in this packaging and the size information of each module.

6. The method as described in claim 1, characterized in that, The compilation process performed by the build tools also includes: Identify asynchronous modules declared using dynamic import syntax in the source code; Based on the scenario configuration information corresponding to the business scenario identifier, an asynchronous sub-packet splitting strategy is determined. The splitting strategy includes at least one of the following: number of sub-packets, sub-packet naming rules, and maximum sub-packet size. According to the splitting strategy, each asynchronous module is assigned to a corresponding asynchronous sub-package, and the routing information of each asynchronous sub-package is written into the global configuration file of the target application, so that the target application loads the corresponding asynchronous sub-package on demand at runtime.

7. The method as described in claim 4, characterized in that, The step of traversing the abstract syntax tree and identifying conditional code segments related to the compile-time constants further includes: Identify platform files in the source files that exist according to multi-end file naming conventions, wherein the filenames of the platform files contain a target operating platform identifier suffix; Based on the target operating platform identifier in the business scenario identifier, select the target file that matches the target operating platform from multiple platform files corresponding to the same module, and remove the platform files that do not correspond to the target operating platform from the code module set.

8. A code packaging device for multi-scenario applications, characterized in that, The device includes: The acquisition unit is used to acquire a packaging request for the target application, wherein the packaging request carries a business scenario identifier; The loading unit is used to load the corresponding scenario configuration information from the preset dynamic configuration library according to the business scenario identifier; The determination unit is used to parse the scenario configuration information using a conditional compilation engine to determine the set of code modules and construction parameters that match the business scenario identifier. In the compilation and build process of the cross-platform development framework, the build context is dynamically adjusted based on the code module set and the build parameters to instruct the build tool to compile only the source code contained in the code module set; The build tool performs a compilation process to generate a target application package adapted to the business scenario identifier.

9. An electronic device, characterized in that, The electronic device includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the steps of the method according to any one of claims 1-7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the method described in any one of claims 1-7.