Method and device for building front-end universal UI component library based on vue
Through the front-end general UI component library construction method based on Vue 2.7, the problem of mismatch between the style of the component library and the business system and the inconsistency of the technology stack is solved, the diversity and applicability of the component library are realized, and the development efficiency and scalability are improved.
Patent Information
- Application Number
- CN202410783470.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-18
- Publication Date
- 2024-09-10
AI Technical Summary
The traditional front-end general UI component library does not match the component library system style of the business system, and the technology stack of the open source component library is inconsistent with the technology stack used in the business system, resulting in low expansion flexibility and poor scalability of the component library.
Adopt a front-end universal UI component library construction method based on Vue 2.7, use sass functions and CSS 3 variables for style development and theme configuration, combine Gulp and Rollup tools to build a component library, and use pnpm and monorepo solutions to split the structure and generate developers Documentation to achieve flexible expansion and migration of component libraries.
It achieves the diversity and applicability of the component library, can be applied to various business system styles, is compatible with multiple technical versions, simplifies the component library design and development process, reduces the frequency of designer intervention, and improves development efficiency.
Smart Images

Figure CN118626080A
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of front-end development, and in particular relates to a method and device for building a front-end universal UI component library based on Vue. Background Art
[0002] Vue is a progressive JavaScript framework that is mainly used to build user interfaces. Its core library focuses only on the view layer, which makes Vue very easy to integrate into other projects and other types of libraries. Vue's goal is to implement a responsive data binding and combined view component system through the simplest API possible. The front-end general UI component library is a set of reusable user interface components that are used to build the front end of web applications. These components include buttons, input boxes, drop-down menus, dialog boxes, prompt boxes, etc., which can be used in multiple projects or applications to improve development efficiency and consistency. These component libraries usually provide some customized functions, allowing developers to adjust the style and behavior of components according to the needs of their own projects.
[0003] Front-end general UI component libraries play a core role in the field of front-end development. They significantly improve development efficiency and ensure the consistency and professionalism of application interfaces by providing a series of pre-made, reusable interface components. These component libraries simplify the maintenance process, strengthen collaboration between teams, and support responsive design to adapt to the display requirements of different devices. Overall, they provide a solid foundation for developing complex modern web applications with good user experience.
[0004] The system style of the traditional component library does not match that of the component library of the business system. When applying the existing component library, it is often necessary to cooperate with the project designers to make further modifications. In addition, the technology stack used by the current open source component library is inconsistent with the technology stack used for development in the business system, which is not conducive to the subsequent expansion of the component library, has low flexibility and poor scalability. Summary of the invention
[0005] In view of the above deficiencies in the prior art, the purpose of the invention is to provide a method and device for building a front-end universal UI component library based on Vue, which can be applicable to various business system styles, and the component library built based on Vue2.7 can be compatible with multiple technology version technology stacks, which expands the scope of application of the constructed component library, and can facilitate subsequent component library expansion and flexible migration. It has a wide range of applications and is easy to operate, and can reduce the frequency of intervention of component library designers during the application of the component library.
[0006] The first aspect of the present invention provides a method for building a front-end universal UI component library based on Vue, the method comprising:
[0007] S1, determine the components to be packaged;
[0008] S2, styling the components to be encapsulated, wherein styling includes setting the style development of the components to be encapsulated through sass functions and configuring the theme of the components to be encapsulated through theme-scheme and css3 var;
[0009] S3, based on vue2.7 and typescript, develops components to be encapsulated after stylization;
[0010] S4, construct a component library for the developed components using the gulp tool and the rollup tool, where the component library includes an esm component library and a cjs component library;
[0011] S5, split the component library structure through the workspace solution based on pnpm and monrepo to obtain multiple component library parts;
[0012] S6, generate developer documentation for the component library and complete the construction of the component library.
[0013] In a second aspect of the present invention, a front-end universal UI component library construction device based on Vue is proposed, which is used to implement the front-end universal UI component library construction method based on Vue of any one of the first aspects, and the device includes:
[0014] A determination module, used to determine the components to be packaged;
[0015] Styling module, used to stylize the encapsulated components, where styling includes setting the style development of the encapsulated components through sass functions and configuring the theme of the encapsulated components through theme-scheme and css3 var;
[0016] Development module, used to develop components for styled encapsulated components based on Vue2.7 and TypeScript;
[0017] The construction module is used to build a component library for the components developed through the gulp tool and the rollup tool. The component library includes the esm component library and the cjs component library.
[0018] The split module is used to split the component library structure through the workspace solution based on pnpm and monrepo to obtain multiple component library parts;
[0019] The generation module is used to generate developer documentation for the component library and complete the construction of the component library.
[0020] The third aspect of the present invention proposes a front-end universal UI component library building system based on Vue, including: a memory and one or more processors.
[0021] One or more applications are stored in the memory, and the one or more applications are suitable for being executed by one or more processors to implement the Vue-based front-end general UI component library building method of the first aspect.
[0022] In a fourth aspect of the present invention, a computer-readable storage medium is proposed, on which a computer program is stored, and the computer program can be loaded and executed by a processor to implement the method for building a front-end general UI component library based on Vue in the first aspect.
[0023] The beneficial effects of the present invention are as follows:
[0024] The method and device of the present invention use sass functions to set the style development of components to be encapsulated and use theme-scheme and css3 var to configure the theme of the encapsulated components, generate a unified prefix to facilitate unified modification, and subsequently fine-tune the styles of components by only modifying the variables at the root of the component styles, without the need to query the scope of influence of the variables step by step. First, the variables required for the component to be used are uniformly inherited from the global basic variables, and then the variables required for the component are added. The component styles are diverse, the polymorphism of the component styles is realized, and it can be applied to various business system styles. The component library built based on vue2.7 can be compatible with multiple technology version technology stacks, which expands the scope of application of the built component library, and can conveniently carry out subsequent component library expansion and flexible and low-cost migration. It has a wide range of applications and is easy to operate. It can reduce the frequency of intervention of component library designers during the application of the component library and speed up the user interface development speed of developers for different business scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] The accompanying drawings are only used to illustrate specific embodiments and are not considered to limit the present invention. In the entire drawings, the same reference symbols represent the same components. Obviously, the drawings described below are only some embodiments recorded in the embodiments of the present invention. For those of ordinary skill in the art, other drawings can also be obtained based on these drawings.
[0026] Figure 1 A schematic diagram of a method for building a front-end universal UI component library based on Vue provided in an embodiment of the present invention;
[0027] Figure 2 A schematic diagram of the structure of a front-end universal UI component library building device based on Vue provided by an embodiment of the present invention;
[0028] Figure 3A structural schematic diagram of a Vue-based front-end universal UI component library building system provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0029] In order to enable those skilled in the art to better understand the technical solutions in the embodiments of the present invention, the technical solutions of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. It should be understood that these descriptions are only exemplary and are not intended to limit the scope of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in the field without making creative work should fall within the scope of protection of the present invention.
[0030] Furthermore, in the following description, descriptions of well-known structures and technologies are omitted to avoid unnecessarily obscuring the concepts disclosed in the present invention.
[0031] In the description of the present invention, it should be noted that, unless otherwise clearly specified and limited, the orientation or position relationship indicated by the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inside", "outside", etc. is based on the orientation or position relationship shown in the drawings, which is only for the convenience of describing the present invention and simplifying the description, and does not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as a limitation of the present invention. In addition, the terms "first", "second", and "third" are only used for descriptive purposes and cannot be understood as indicating or implying relative importance. The terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be a connection between the two elements. For those of ordinary skill in the art, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0032] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Instead, they are merely examples of methods and systems consistent with some aspects of the present invention as detailed in the appended claims.
[0033] The present invention proposes a method and device for building a front-end universal UI component library based on Vue. In order to solve the system style mismatch between the traditional component library and the component library of the business system, when applying the existing component library, it is often necessary to cooperate with the project designers to make further modifications. In addition, the technology stack used by the current open source component library is inconsistent with the technology stack used for development in the business system, which is not conducive to the subsequent expansion of the component library, and has low flexibility and poor scalability.
[0034] Method Embodiment
[0035] Reference Manual Attached Figure 1 , showing a flow chart of a method for building a front-end universal UI component library based on Vue provided in an embodiment of the present invention.
[0036] An embodiment of the present invention provides a method for building a front-end universal UI component library based on Vue, the method comprising:
[0037] Specifically, the method includes steps S1 to S6.
[0038] S1, determine the components to be packaged.
[0039] In a possible implementation, the components to be packaged include picture buttons, layout components, operation columns, tree-shaped drop-down single selections, tree-shaped drop-down multiple selections, and virtual drop-down selections.
[0040] It should be noted that commonly used front-end functions and UI interaction effects are encapsulated into independent components using the component API style of vue3, including but not limited to picture buttons, layout components, operation columns, tree-shaped drop-down single selection, tree-shaped drop-down multiple selection, virtual drop-down selection, etc. These components have good scalability, promptability and encapsulation, and developers can use them directly without rewriting code. Among them, Vue 3 is a popular front-end JavaScript framework for building user interfaces and single-page applications. It is the latest version of Vue.js, bringing many improvements and new features, including better performance, smaller size and more modern APIs. Vue3 provides faster rendering speed, better memory efficiency, and introduces new concepts such as Composition API, multi-root node components, and fragments. By using the Component API style of Vue 3 (especially the Composition API), commonly used UI functions and interaction effects can be encapsulated into independent and reusable components. This approach not only makes it easier to expand, maintain and reuse components, but also helps improve development efficiency, because developers can directly use these pre-packaged components without having to write repetitive code from scratch. This packaging method enhances the scalability, modularity, and encapsulation of components, making the development of front-end projects more modular and efficient.
[0041] S2, styling the components to be encapsulated, wherein styling includes setting the style development of the components to be encapsulated through sass functions and configuring the theme of the components to be encapsulated through theme-scheme and css3 var.
[0042] Among them, sass (Syntactically Awesome Style Sheets) is a powerful CSS preprocessor that allows the use of advanced features such as variables, nested rules, mixins, and functions, which are not available in native CSS. SASS functions allow you to define reusable logic that can be used to handle the calculation and conversion of style values such as color and size. With SASS functions, styles can be managed more flexibly, especially in large projects, by encapsulating commonly used style patterns through functions to improve development efficiency and maintain style consistency. Theme-scheme refers to a theme configuration scheme, usually a set of predefined style variable collections that define the visual design elements of the application, such as color, font, margin, etc. By switching different theme schemes (such as light theme, dark theme), the overall appearance of the application can be quickly changed. This method is usually implemented in conjunction with CSS variables, so that new styles can be applied by simply changing the values of some core variables when switching between different themes. CSS custom properties (also known as CSS variables) are a feature of CSS3 that allow you to store specific values in a variable and reuse them throughout the document. For example, you can define a theme color in the root element of the document and then reference it in other CSS rules. CSS variables are great for implementing a theming system, as you can adjust the color theme or font style of your entire website by changing the values of a few variables.
[0043] It should be noted that in style management, the component library provides a theme style management mechanism based on color-scheme, sass variables and css3var variables, and uses the bem specification to ensure that the styles between components do not interfere with each other. Developers can add style class names to components to overwrite the original styles and set new theme-schema style variables to achieve style customization.
[0044] In a possible implementation, S2 specifically includes:
[0045] S201, set the variable name of the sass function.
[0046] Use ()! default to set a variable, which is convenient for business projects to modify the default value through @forword to complete the customization of the theme.
[0047] S202, merging default values through the map.deep-merge of the sass function, where the default values are a set of variable names.
[0048] Among them, in SASS, map.deep-merge is a function that handles deeply nested map structures. The map function of SASS is similar to objects or dictionaries in other programming languages, allowing developers to store collections of key-value pairs. The map.deep-merge function is used to merge two or more deeply nested maps, which is very useful when dealing with complex style settings with multi-layer structures. The default values are merged through the map.deep-merge of the sass function, where variables can set the length, width, height, color, text size, etc. of the component.
[0049] S203, concatenating the variable names through the mixin function of the sass function, generating css3 variables with a unified prefix, and completing the global variable setting of the component to be encapsulated.
[0050] Among them, mixin is a powerful feature that allows developers to create reusable code blocks that can be inserted in multiple CSS rules. The main purpose of mixin is to promote the reuse of CSS code and avoid code duplication, thereby improving the maintainability and scalability of the project.
[0051] This is a pseudo-class process, that is, setting custom properties at the root, and using the sass mixin function to splice variables to set css3 variables such as "--cnos-empty-font-size", global variables of the component library are added. The unified prefix cnos is used through the global variable $namespace to facilitate unified modification. When it comes to the specific component style, first inherit the global basic variables about the variables needed by the component, and then add the variables needed by the component. This realizes the polymorphism of the component style. In this way, for subsequent fine-tuning of the component style, you only need to modify the variables at the root of the component style, and there is no need to query the scope of the variable one level at a time.
[0052] S204, inheriting the global variables of the component to be encapsulated.
[0053] S205, obtaining the variables required by the modified component, and adding the variables required by the modified component to the global variables of the component to be encapsulated, thereby completing the stylization of the component to be encapsulated.
[0054] In the specific application process, color-theme is used for the configured themes in the theme switching. For example, to add a dark style, first set the global basic variables for html.dart, mainly background color, text color, etc., and mark its color-theme as dart. When switching globally, you only need to add a style class name of .dark to html to achieve dynamic theme replacement. Style development is based on normal sass development, and also uses tools and methods such as include mixin and getting css variables. At the same time, a tool hook called useNamespace is also provided to set the classname class name for the component with js logic to improve development efficiency. The style construction uses autoprefixer to add browser prefixes to handle compatibility, clean-css to compress css, and rename to rename the css file with the prefix. At the same time, it provides copying the source code to the build file, which is convenient for viewing at any time or directly using and building on the business side.
[0055] S3, based on vue2.7 and typescript, develops components for styled components to be encapsulated.
[0056] Among them, Vue 2.7 is a version of the Vue.js framework, which is enhanced on the basis of the Vue 2 series and introduces some Vue 3 features while maintaining backward compatibility. Vue is a popular progressive JavaScript framework for building user interfaces. It is mainly easy to integrate into the case where other JavaScript libraries or projects are partially used in the project, and it can provide drivers for complex single-page applications. TypeScript is an open source language developed and maintained by Microsoft. It is a superset of JavaScript that adds optional static type checking and the latest ECMAScript features.
[0057] Using vue2.7, you can enjoy componentApi style development and avoid breadcrumb code. It also avoids the black box problem of mixin mixing in vue2. You can directly reuse hooks and use typescript for development. It has complete code prompts and regular type error checks, and provides business component code prompts with the vola plugin volar-service-vetur. It can be used by vue2 and vue2.7 at the same time, and is compatible with historical old versions. At the same time, if there is a need to migrate to vue3, the business system needs to change very few code places, reducing the migration cost.
[0058] In a possible implementation, S3 specifically includes:
[0059] S301, add the component information to be packaged in the path packages / component.
[0060] S302, add a style template in the path packages / theme-chalk and output the developed component.
[0061] S303, import the developed component into index.vue in play, wherein the style effect of the developed component is viewed in the browser through npm run dev.
[0062] S304, when the style effect meets the expected style effect, import the developed component into the path packsages / cnos-frontend.
[0063] S305, execute the sh. / scripts / build.sh command, add the tag version of the developed component and publish the developed component.
[0064] Among them, packages / component is a directory, usually located in the source code of a project, used to store various component files to be encapsulated. Under this directory, each component usually has its own folder, containing its Vue file, possible JavaScript or TypeScript files, and other resources. This structure helps to organize and manage a large number of components. packages / theme-chalk is another directory used to store component style templates. theme-chalk usually refers to the default or basic style file of the project. Adding a style template here means defining the appearance and style of the component. These styles can be SASS / SCSS files, which are compiled into CSS and applied to the component. The play directory in play / index.vue is an environment for local development and testing of components, and index.vue is a Vue file in this environment, which is used to import and display recently developed components so that developers can view and test the performance and functionality of components in real time. npm run dev is a commonly used command line instruction used to start the development server of the project, which is usually configured in the scripts section of the project's package.json file. Executing this command compiles the project and starts a hot-reloaded development server locally, allowing developers to view the effects of code changes in real time. packages / cnos-frontend is a directory containing the final front-end library or framework, used to store and manage all components verified by the development process. Importing components into this directory means that they are ready to be included in the release version. sh. / scripts / build.sh is the path to a shell script, which is usually used to automate the build process, such as compiling, packaging, tagging versions, and publishing component libraries. Executing this script can automate complex build steps, ensuring that each build is executed according to the predetermined process and reducing human errors.
[0065] In a possible implementation manner, after S3, the following steps are further included:
[0066] Output code hints for components developed using the volar-service-vetur plugin of volar.
[0067] Among them, Volar is a language service plug-in. Volar-service-vetur refers to the part of the Volar plug-in that is responsible for handling old Vue applications (such as Vue 2.x). It allows developers to use Volar while still getting compatible support for old code. This service ensures that even in new projects, code built with old versions of Vue can be seamlessly migrated and maintained.
[0068] S4, construct a component library for the developed components through the gulp tool and the rollup tool, where the component library includes the esm component library and the cjs component library.
[0069] Among them, Gulp is an automated build tool. Rollup is a JavaScript module bundler that focuses on generating the most concise code possible. ESM (ECMAScript Modules) is the official standard module system for JavaScript. CJS (CommonJS) is another JavaScript module specification. Gulp is used to automate and optimize the build process, while Rollup is used to package components into modules of different formats to ensure that the library can be used in different JavaScript environments, whether using ESM directly in the browser or using CJS in Node.js. Such a setup can significantly improve development efficiency, reduce compatibility issues, and make the component library more robust and easy to maintain.
[0070] Among them, the component library is the UI component library.
[0071] Specifically, gulp is used to organize the build process, and based on rollup and esbuild, full and on-demand loading products that comply with umd, cjs, esm specifications, as well as typescript type products are built.
[0072] In a possible implementation, S4 specifically includes:
[0073] S401, start the gulpfile.ts file of the gulp tool through the package.json command.
[0074] S402, in combination with the rollup tool, the browser-callable product of the developed component is generated through the gulpfile.ts file, wherein the browser-callable product includes an esm product and a cjs product.
[0075] Among them, the package.json file plays a central role and is used to manage project dependencies, scripts, project configuration and other information. gulpfile.ts is the configuration file of Gulp. esm products and cjs products are files that convert browser-callable products into files that support esm or cjs environments.
[0076] The browser-callable product also includes a compressed package of the browser-callable product.
[0077] The actions performed by the gulpfile.ts file are: 1. Clean up historical products (the dist folder in the root directory and the dist in each package). 2. Create a folder to store files. 3. Build esm and cjs products that are loaded on demand (each component is built separately). 4. Fully build umd and esm products that can be used directly in the browser, and the products must also provide compressed versions. 5. Build d.ts files for development to provide code hints. 6. Copy style files and d.ts files to the dist folder in the root directory.
[0078] S403, converting the browser-callable product into a recognizable module of the rollup tool.
[0079] Among them, the recognizable module is es6 module.
[0080] Specifically, the construction of products is based on rollup and plug-ins: use @vitejs / plugin-vue2 to edit Vue SFC into javascript code, use @rollup / plugin-node-resolve to support node.js's resolution algorithm to process node_modules, configure the extensions property to load the target file with the configuration suffix, and use @rollup / plugin-commonjs to convert commonjs modules into es6, which becomes a module that rollup can parse.
[0081] S404, through the roolup module, configure the entry of the identifiable module, eliminate the dependency and call the required plug-in to complete the construction of the component library.
[0082] Before step S404, the process also includes: using rollup-plugin-esbuid to convert typescript to javascript, configuring the minify option of the ebuild plug-in to determine whether to compress the code, configuring the target to determine compatibility with lower version browsers (currently at least compatible with ES2018), and configuring loaders to treat vue files as js files.
[0083] The specific process of building a front-end component library is as follows: first, create new packages and internal folders, and use pnpm-workspace to support monorepo. Then, add eslint-config to internal to support global code formatting, add build to use gulp to organize code building, use rollup to build code, and build targets include umd, esm, cjs code, full packaging and on-demand loading. After that, add cnos-frontend to packages as the export of the component library, package.json is used for version information, add components to store components, add theme-chalk as a style library, add hooks and utils to add component library tools. Then, add playground to use vite to debug the component library code. Finally, add docs to use vite and vite-plugin-md, write md documents to provide documentation support for components, and use vite-plugin-md-preview to show the demo of the component.
[0084] S5, split the component library structure through the workspace solution based on pnpm and monrepo to obtain multiple component library parts.
[0085] Among them, pnpm (Performant npm) is a package manager for JavaScript and Node.js projects. Monorepo (single repository) is a project management strategy in which multiple related code libraries are stored in the same version control system repository. In pnpm, workspace is a tool for configuring and managing multiple packages (projects, libraries, applications, etc.) in the same physical location. By combining pnpm and monorepo, large component libraries can be split into small, independently maintainable units, each responsible for a part of the function or service. Ensure that when a component is updated, other components that depend on it can easily get the update without having to update the dependency separately in each project. This not only helps keep the project clean and organized, but also greatly simplifies the process of version management and dependency upgrades.
[0086] It should be noted that the monorepo workspace solution based on pnpm organizes the component library into internal builds, code standards, external tools, components, style libraries, and debugging structures, which facilitates and quickly calls each other to solve the pain points of code reuse.
[0087] Based on the monorepo workspace solution of pnpm, the component library is organized into internal construction, code specifications, external tools, components, style libraries, and debugging structures, which are convenient and quick to call each other to solve the pain point of code reuse. In conjunction with the monorepo solution of pnpm's workspace, commonly used hooks and tool methods are decoupled into independent packages, which can be quickly introduced and used through global soft links, and can also be packaged and deployed independently.
[0088] With the help of pnpm's workspace, code style formatting, construction, style theme library, component library, tool library, hook, documentation, and debugging are separated. The withInstall of the tool library is used to add the install attribute to the component, which is convenient for registering global components through vue.use. At the unified component library export, each component is exported for on-demand use. All components are collected into an array, and a global install attribute is added to facilitate the registration of all components at once on the business side. A version attribute is also provided to facilitate the generation and viewing of the version used by the component library.
[0089] After generating the component library, you can also generate type files, that is, configure the script command of package.json through the shell script to complete quick release.
[0090] In a possible implementation, the component library portion includes internal construction, code specifications, external tools, components, style libraries, and debugging.
[0091] S6, generate developer documentation for the component library and complete the construction of the component library.
[0092] In a possible implementation, the developer documentation includes documentation and examples of adding a custom preview block through the vite-plugin-md-preview plug-in to display real effects, wherein the documentation includes component usage methods, parameter descriptions, and sample codes.
[0093] In a possible implementation, S6 specifically includes:
[0094] S601, add the markdown document of the component library.
[0095] S602, add instructions and examples for use of each component in the component library to the markdown document.
[0096] Instructions and Examples Includes instructions and examples for the button component's size, color, and states.
[0097] S603, convert markdown document to html file through vite-plugin-md.
[0098] Among them, Vite-plugin-md is a plugin for Vite, which allows converting Markdown files (.md) into Vue components.
[0099] S604, generate the Vue code of the HTML file.
[0100] S605, use vite-plugin-md-preview to parse the vue code and get the vue component.
[0101] S606, describe the properties and slots of the vue component, and generate developer documentation for the component library, wherein the properties include lang information, meta information, and property code information, and the slots include default information and slot code information.
[0102] Among them, markdown is the most commonly used document description file for programmers, so this document library is based on markdown. Viewing the actual effect of the component and its code is extremely important for the document library. The general practice is to add a custom block (plus a unique logo) in the markdown, and convert it into a component that has both original code and successful rendering in the process of parsing the markdown. Regarding the component preview part: by adding the preview logo, use vite-plugin-md-preview to parse and replace the vue code with the vue component. After parsing, the props provided have lang, meta, and code information, which can be used to implement the copy and view functions. The slot part has default and code parts. With the global code block custom component, the default part is rendered on the page to show the actual effect of the component. Use vite-plugin-page to read the specified folder (src / page) to generate a predetermined route, reduce component route maintenance, and let developers focus on document writing. Custom document libraries can add global components and styles to implement enterprise-level component library document libraries with as little code as possible.
[0103] It should be noted that in order to facilitate developers to use and learn, this component library converts markdown files into Vue single-file component codes based on vite and vite-plugin-md and provides detailed documentation and examples, including component usage, parameter descriptions and sample codes. The example uses the vite-plugin-md-preview plug-in to add a custom preview block to show the real effect. Developers can quickly master the use of components through the documentation.
[0104] In the actual application process, the component encapsulation implementation process is as follows: complete the style and logic of the component according to the needs. The style part is developed based on sass, and theme-scheme and css3var are used to implement theme configuration, which is convenient for subsequent theme customization. The component part is mainly developed based on vue2.7 and typescript, and is organized and managed with the workspace solution of pnpm, such as the decoupling of common hooks and common tool methods. Finally, the component library esm and cjs are built based on gulp and rollup, and the type file construction is coordinated with the shell script to configure the script command of package.json to complete the quick release. Finally, the theme style library based on css3 var, sass, and element-ui is implemented under the vue2.7 version to realize business components such as fast theme customization, custom layout, tree-shaped multi-selection, tree-shaped single-selection, and virtual scroll drop-down selection components. The component products of umd, cjs, and esm are realized through gulp organization compilation and rollup and esbuild construction.
[0105] After the component library is built, the calling method of the component library is very simple. For example, in the entry file of the business end, the style and components of the component library are introduced, and the components are globally mounted through vue.use. The on-demand import method is to add the required components to the corresponding business components as follows: import{CnosInputAffix}from"@cnos / frontend"; the corresponding business components can be used. At the same time, when importing components on demand, pay attention to whether the style file of the component is imported.
[0106] It should be noted that using Vue2.7 for development is not only compatible with old Vue2.x projects, but also can use the new syntax provided by Vue3.x, which greatly optimizes the development experience. The code style is mainly componentApi, and the code can be directly migrated to vue3. Use typescript with vola to provide good code prompts. There is currently no component library specifically for vue2.7 with typescript. This project enriches the front-end ecology. At the same time, the organizational structure of monorepo provides a set of enterprise-level management system component styles through the development iteration of the component library, and provides a set of out-of-the-box general components, which improves the technical level and standardization of development and design personnel.
[0107] The beneficial effects of the present invention are as follows:
[0108] The method and device of the present invention use sass functions to set the style development of components to be encapsulated and use theme-scheme and css3 var to configure the theme of the encapsulated components, generate a unified prefix to facilitate unified modification, and subsequently fine-tune the styles of components by only modifying the variables at the root of the component styles, without the need to query the scope of influence of the variables step by step. First, the variables required for the component to be used are uniformly inherited from the global basic variables, and then the variables required for the component are added. The component styles are diverse, the polymorphism of the component styles is realized, and it can be applied to various business system styles. The component library built based on vue2.7 can be compatible with multiple technology version technology stacks, which expands the scope of application of the built component library, and can conveniently carry out subsequent component library expansion and flexible and low-cost migration. It has a wide range of applications and is easy to operate. It can reduce the frequency of intervention of component library designers during the application of the component library and speed up the user interface development speed of developers for different business scenarios.
[0109] Device Embodiment
[0110] Reference Manual Attached Figure 2 , showing a structural schematic diagram of a front-end general UI component library building device based on Vue provided by an embodiment of the present invention.
[0111] Another specific embodiment of the present invention discloses a front-end universal UI component library construction device based on Vue, which is used to implement any of the front-end universal UI component library construction methods based on Vue in the method embodiments. The front-end universal UI component library construction device 20 based on Vue includes:
[0112] A determination module 201 is used to determine a component to be packaged;
[0113] A styling module 202, used for styling the component to be packaged, wherein the styling includes setting the style development of the component to be packaged through a sass function and configuring the theme of the component to be packaged through theme-scheme and css3 var;
[0114] Development module 203, used for developing components to be encapsulated after stylization based on Vue2.7 and TypeScript;
[0115] A construction module 204 is used to construct a component library for the developed components by using the gulp tool and the rollup tool, wherein the component library includes an esm component library and a cjs component library;
[0116] A splitting module 205 is used to split the component library structure by using a workspace solution based on pnpm and monrepo to obtain multiple component library parts;
[0117] The generation module 206 is used to generate developer documentation for the component library to complete the construction of the component library.
[0118] In a possible implementation, the components to be packaged include picture buttons, layout components, operation columns, tree-shaped drop-down single selections, tree-shaped drop-down multiple selections, and virtual drop-down selections.
[0119] In a possible implementation, the stylization module is specifically used for:
[0120] S201, setting the variable name of the sass function;
[0121] S202, merging default values through map.deep-merge of the sass function, wherein the default values are a set of the variable names;
[0122] S203, concatenating the variable names through the mixin function of the sass function to generate css3 variables with a unified prefix, and completing the global variable setting of the component to be encapsulated;
[0123] S204, inheriting the global variables of the component to be packaged;
[0124] S205, obtaining variables required by the modified component, and adding the variables required by the modified component to the global variables of the component to be encapsulated, thereby completing the stylization of the component to be encapsulated.
[0125] In a possible implementation, the development module is specifically used to:
[0126] S301, adding the component information to be packaged in the path packages / component;
[0127] S302, add a style template in the path packages / theme-chalk and output the developed component;
[0128] S303, importing the developed component into index.vue in play, wherein the style effect of the developed component is viewed in a browser through npm run dev;
[0129] S304, when the style effect meets the expected style effect, import the developed component into the path packsages / cnos-frontend;
[0130] S305, executing the sh. / scripts / build.sh command, adding the tag version of the developed component and publishing the developed component.
[0131] In a possible implementation, it further includes:
[0132] The output module is used to output code hints of components developed through the volar-service-vetur plugin of volar.
[0133] In a possible implementation, the building block is specifically used for:
[0134] S401, starting the gulpfile.ts file of the gulp tool through the package.json command;
[0135] S402, combining the rollup tool, generating a browser-callable product of the developed component through the gulpfile.ts file, wherein the browser-callable product includes an esm product and a cjs product;
[0136] S403, converting the browser-callable product into a recognizable module of the rollup tool;
[0137] S404, configuring the entry of the identifiable module, eliminating dependencies and calling required plug-ins through the roolup module to complete the construction of the component library.
[0138] In a possible implementation, the component library part includes internal construction, code specifications, external tools, components, style libraries, and debugging.
[0139] In a possible implementation, the developer documentation includes documentation and examples of adding a custom preview block through the vite-plugin-md-preview plug-in to display a real effect, wherein the documentation includes component usage methods, parameter descriptions, and sample codes.
[0140] In a possible implementation manner, the generating module is specifically used for:
[0141] S601, adding the markdown document of the component library;
[0142] S602, adding instructions and examples for use of each component in the component library to the markdown document;
[0143] S603, converting the markdown document into an HTML file through vite-plugin-md;
[0144] S604, generating the Vue code of the HTML file;
[0145] S605, using vite-plugin-md-preview to parse the vue code to obtain a vue component;
[0146] S606, describe the properties and slots of the vue component, and generate developer documentation for the component library, wherein the properties include lang information, meta information, and property code information, and the slots include default information and slot code information.
[0147] The device provided in the embodiment of the present invention can implement each process and step in the above method embodiment, and can obtain the same or similar technical effects. To avoid repetition, the present invention will not go into details.
[0148] The beneficial effects of the present invention are as follows:
[0149] The method and device described in the present invention use sass functions to set the style development of the component to be packaged and use theme-scheme and css3 var to configure the theme of the component to be packaged, generate a unified prefix to facilitate unified modification, and subsequently fine-tune the style of the component only by modifying the variables at the root of the component style, without the need to query the scope of influence of the variable step by step. First, the variables required for the component are uniformly inherited from the global basic variables, and then the variables required for the component are added. The component styles are diverse, and the polymorphism of the component styles is realized. It can be applied to various business system styles, and the component library built based on vue2.7 can be compatible with multiple technology version technology stacks, which expands the scope of application of the built component library, and can conveniently carry out subsequent component library expansion and flexible and low-cost migration. It has a wide range of applications and is easy to operate. It can reduce the frequency of intervention of component library designers during the application of the component library and speed up the user interface development speed of developers for different business scenarios.
[0150] System Example
[0151] Reference Manual Attached Figure 3 , showing a structural schematic diagram of a front-end general UI component library building system based on Vue provided by an embodiment of the present invention.
[0152] The present invention proposes a front-end universal UI component library building system 30 based on Vue, including: a memory 303 and one or more processors 301.
[0153] One or more applications are stored in the memory 303, and the one or more applications are suitable for being executed by the one or more processors 301 to implement the method for building a front-end general UI component library based on Vue as described in the method embodiment.
[0154] The front-end universal UI component library building system 30 based on Vue includes: a processor 301 and a memory 303. The processor 301 and the memory 303 are connected, such as through a bus 302.
[0155] The structure of the Vue-based front-end universal UI component library building system 30 does not constitute a limitation on the embodiments of the present invention.
[0156] Processor 301 may be a CPU, a general purpose processor, a DSP, an ASIC, an FPGA or other programmable logic device, a transistor logic device, a hardware component or any combination thereof. It may implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the disclosure of the present invention. Processor 301 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.
[0157] The bus 302 may include a path to transmit information between the above components. The bus 302 may be a PCI bus or an EISA bus, etc. The bus 302 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0158] The memory 303 can be a ROM or other type of static storage device that can store static information and instructions, a RAM or other type of dynamic storage device that can store information and instructions, or an EEPROM, a CD-ROM or other optical disk storage, an optical disk storage (including a compressed optical disk, a laser disk, an optical disk, a digital versatile disk, a Blu-ray disk, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to these.
[0159] Computer Readable Storage Medium Embodiments
[0160] The present invention proposes a computer-readable storage medium having a computer program stored thereon, which can be loaded and executed by a processor according to the method for building a front-end universal UI component library based on Vue as described in the first aspect.
[0161] The applicant of the present invention has made a detailed explanation and description of the implementation examples of the present invention in conjunction with the drawings in the specification. However, those skilled in the art should understand that the above implementation examples are only preferred implementation schemes of the present invention, and the detailed description is only to help readers better understand the spirit of the present invention, but not to limit the scope of protection of the present invention. On the contrary, any improvements or modifications based on the inventive spirit of the present invention should fall within the scope of protection of the present invention.
[0162] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the embodiments of the present invention, rather than to limit them. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that they can still modify the technical solutions recorded in the above embodiments, or replace some of the technical features therein by equivalents; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention. Any changes or replacements that can be easily thought of by those skilled in the art within the technical scope disclosed in the present invention should be covered within the protection scope of the present invention.
Claims
1. A method for building a front-end general UI component library based on Vue, characterized in that: Methods include: S1, determine the components to be packaged; S2, stylizing the component to be packaged, wherein the stylization includes setting the style development of the component to be packaged through a sass function and configuring the theme of the component to be packaged through theme-scheme and css3 var; S3, based on vue2.7 and typescript, develops components to be encapsulated after stylization; S4, constructing a component library for the developed components by using the gulp tool and the rollup tool, wherein the component library includes an esm component library and a cjs component library; S5, structurally splitting the component library through a workspace solution based on pnpm and monrepo to obtain multiple component library parts; S6, generating developer documentation for the component library to complete the construction of the component library.
2. According to a method for building a front-end general UI component library based on Vue according to claim 1, it is characterized in that: The components to be packaged include picture buttons, layout components, operation columns, tree-shaped drop-down single selection, tree-shaped drop-down multiple selection, and virtual drop-down selection.
3. According to a method for building a front-end general UI component library based on Vue in claim 1, it is characterized in that: The S2 specifically includes: S201, setting the variable name of the sass function; S202, merging default values through map.deep-merge of the sass function, wherein the default values are a set of the variable names; S203, concatenating the variable names through the mixin function of the sass function to generate css3 variables with a unified prefix, and completing the global variable setting of the component to be encapsulated; S204, inheriting the global variables of the component to be packaged; S205, obtaining variables required by the modified component, and adding the variables required by the modified component to the global variables of the component to be encapsulated, thereby completing the stylization of the component to be encapsulated.
4. According to a method for building a front-end general UI component library based on Vue in claim 1, it is characterized in that: The S3 specifically includes: S301, adding the component information to be packaged in the path packages / component; S302, add a style template in the path packages / theme-chalk and output the developed component; S303, importing the developed component into index.vue in play, wherein the style effect of the developed component is viewed in a browser through npm run dev; S304, when the style effect meets the expected style effect, import the developed component into the path packsages / cnos-frontend; S305, executing the sh. / scripts / build.sh command, adding the tag version of the developed component and publishing the developed component.
5. According to a method for building a front-end general UI component library based on Vue in claim 1, it is characterized in that: After S3, the following is also included: Output code hints for components developed using the volar-service-vetur plugin of volar.
6. According to a method for building a front-end universal UI component library based on Vue in claim 1, it is characterized in that: The S4 specifically includes: S401, starting the gulpfile.ts file of the gulp tool through the package.json command; S402, combining the rollup tool, generating a browser-callable product of the developed component through the gulpfile.ts file, wherein the browser-callable product includes an esm product and a cjs product; S403, converting the browser-callable product into a recognizable module of the rollup tool; S404, configuring the entry of the identifiable module, eliminating dependencies and calling required plug-ins through the roolup module to complete the construction of the component library.
7. According to a method for building a front-end universal UI component library based on Vue in claim 1, it is characterized in that: The component library part includes internal construction, code specifications, external tools, components, style libraries and debugging.
8. According to a method for building a front-end general UI component library based on Vue in claim 1, it is characterized in that: The developer documentation includes documentation and examples of adding a custom preview block through the vite-plugin-md-preview plug-in to show the real effect, wherein the documentation includes component usage methods, parameter descriptions and sample codes.
9. A method for building a front-end universal UI component library based on Vue according to claim 8, characterized in that: The S6 specifically includes: S601, adding the markdown document of the component library; S602, adding instructions and examples for use of each component in the component library to the markdown document; S603, converting the markdown document into an HTML file through vite-plugin-md; S604, generating the Vue code of the HTML file; S605, using vite-plugin-md-preview to parse the vue code to obtain a vue component; S606, describe the properties and slots of the vue component, and generate developer documentation for the component library, wherein the properties include lang information, meta information, and property code information, and the slots include default information and slot code information.
10. A front-end universal UI component library building device based on Vue, characterized in that: The device is used to implement the method for building a front-end universal UI component library based on Vue according to any one of claims 1 to 9, comprising: A determination module, used to determine the components to be packaged; A styling module, used for styling the component to be packaged, wherein the styling includes setting the style development of the component to be packaged through a sass function and configuring the theme of the component to be packaged through theme-scheme and css3 var; Development module, used to develop components for styled encapsulated components based on Vue2.7 and TypeScript; A construction module is used to construct a component library for the developed components through the gulp tool and the rollup tool, wherein the component library includes an esm component library and a cjs component library; A splitting module is used to split the component library structure through a workspace solution based on pnpm and monrepo to obtain multiple component library parts; The generation module is used to generate developer documentation for the component library and complete the construction of the component library.
