Write a UI library referring to elment- UI
I wrote a VUE UI library in my work, so record the steps of writing UI library here
First, start development
- Create a new folder (it’s already under development)
- Open the editor and import the project (I used WebStorm)
- The new package. The json
- I’m going to create a.EditorConfig file, and I’m going to use the standard webStorm mode which is 4 Spaces for all files and so on
- Create four folders:
- Packages are component directories
- SRC Directory of common code for some components
- Build is mainly used to write webPack configuration files
- Examples were used to test our components, and I also used the contents of this directory as documents when they were finally packaged in my work
- Create a.gitignore file
Write a component
- Create a button folder in Packages
- Create the index.js file and SRC folder in “button” and create “button.vue” in “SRC”
- Write a component in button.vue as shown in the following example:
<template>
<button class="act-button" @click="handleClick($event)" :type="type">
<slot></slot>
</button>
</template>
<script>
export default {
name: "act-button".props: {
type: {
type: String.default: 'primary',}},methods: {
handleClick(evt) {
this.$emit('click', evt); }}}</script>
Copy the code
- Write the following in button/index.js:
import Button from './src/button';
Button.install = function (Vue){
Vue.components(Button.name, Button);
}
export default Button;
Copy the code
- Create a new theme-chalk folder in Packages and a new button.less and index.less file in theme-Chalk.
- Style the button component in button.less (longer code, no examples) and introduce button.less in index.less
Package components
- Create a components.json folder under the project root directory to configure the component name and component entry file as shown in the following example:
{
"button": "./packages/button/index.js"
}
Copy the code
- Install dependencies
"devDependencies": {
"vue": "^ 2.6.14"."vue-loader": "^ 15.9.8"."vue-loader-plugin": "^ 1.3.0"."vue-template-compiler": "^ 2.6.14"."webpack": "^ 5.54.0"."webpack-cli": "^ 4.8.0"."webpack-merge": "^ 5.8.0"
}
Copy the code
- Create a new one in the Build folder
webpack.common.js
File and write the following
const VueLoaderPlugin = require('vue-loader-plugin');
module.exports = {
resolve: {
extensions: ['.vue'.'.js'.'.jsx'],},module: {
rules: [{test: /.vue$/,
exclude: /node_modules/,
use: 'vue-loader'}, {test: /.less/,
exclude: /node_modules/,
use: [
'style-loader'.'css-loader'.'less-loader',]}]},plugins: [
new VueLoaderPlugin(),
]
}
Copy the code
- Create a new one in the Build folder
webpack.lib.js
File and write the following
const {merge} = require('webpack-merge'); const common = require('./webpack.common'); const components = require('.. /components.json'); const path = require('path'); module.exports = merge(common, { mode: 'development', entry: components, output: { path: path.resolve('./lib'), filename: '[name].js', libraryTarget: 'commonjs2', clean: true, } })Copy the code
- Configure the NPM script
"scripts": {
"build:lib": "webpack --config build/webpack.lib.js"
},
Copy the code
Package style files
Gulp is used to package the style content separately
- Install the following dependencies
"gulp": "^ 4.0.2." "."gulp-autoprefixer": "^ 8.0.0." "."gulp-cssmin": "^ 0.2.0." "."gulp-less": "^ 5.0.0".Copy the code
- Create gulpfile.js in the project root directory and write the following
const {src, dest} = require('gulp');
const less = require('gulp-less');
const cssmin = require('gulp-cssmin');
const autoprefixer = require('gulp-autoprefixer');
function css() {
return src('packages/theme-chalk/*.less')
.pipe(less())
.pipe(autoprefixer({
cascade: false,
}))
.pipe(cssmin())
.pipe(dest('lib/theme-chalk'))}exports.default = css;
Copy the code
- Modify the NPM build directive build:lib
"build:lib": "webpack --config build/webpack.lib.js && gulp"
Copy the code
Debug component library
We developed the component, we also need to debug it in the browser to see how it works
1. Create the SRC /index.js file and write the following content
import Button from '.. /packages/button';
export default {
install(Vue) {
Vue.use(Button);
},
Button,
}
Copy the code
2. Create a Vue project in the examples folder and introduce SRC /index.js and packages/theme-chalk/index.less files, then develop as a normal Vue project