The opening

The west wind blows the old dongting wave, xiang jun white hair more overnight. Drunk after I do not know the day in the water, full boat clear dream pressure star river.

preface

As a developer, I don’t have my own bag yet, so I always feel like I’m missing something. My girlfriend and bag account for at least the same. It is just as unpleasant as a soldier who goes to war without a gun or eating without chopsticks. At the same time, interviewers often ask a tough question: Do you have an open source project? Have you written your own UI components? As lightning strikes, always feel shorter than others, so today tohekr-uiFor example, how to create your own UI library

  • The document address
  • Program source code

The selection

Our goal is to build a UE based UI framework based on Element-UI

  • The document is chosenVuepressBuild, simple and convenient
  • Package selectiongitlab CIThis article totravisAs an example
  • Initialization takesVue cli - 3.0,pugandsassDependencies and plug-ins (explore on your own)

purpose

  1. Quick builds are โค๏ธ’s ownnpmlibrary
  2. Familiar with the package ๐Ÿ“ฆ command and its syntax
  3. Good command of automated ๐Ÿ‘ท builds
  4. Contribute your footprint to the community ๐Ÿ‘ฃ

Basically,

Start a project

1. Initialize a project

The Vue CLI is recommended for quick builds

vue create hekr-ui
Copy the code

Let’s just go with the default configuration

2. Associate code with your library

Git does a great job of managing code versions

  • When you initialize the library
echo "# hekr-ui" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/xkloveme/hekr-ui.git
git push -u origin master
Copy the code
  • When associating a local library
git remote add origin https://github.com/xkloveme/hekr-ui.git
git push -u origin master
Copy the code

Of course we choose the second option. Remember to ignore node_modules

Configure your project

1. Add dependencies

This article focuses on YARN, of course NPM is also ๐Ÿ‘ŒOK

We document primarily with VuePress, which is optional, but if you just want to ship a package, you can skip this step and the necessary demos and tests will come in handy

Install VuePress as a local dependency
yarn add -D vuepress NPM install -d vuepress

Create a new docs folder
mkdir docs

Create a new markDown file
echo '# Hello VuePress! ' > docs/README.md

# Start writing
npx vuepress dev docs
Copy the code

Next, add some scripts to package.json:

{
  "scripts": {
    "docs:dev": "vuepress dev docs"."docs:build": "vuepress build docs"}}Copy the code

Then you can start writing:

yarn docs:dev NPM run docs:dev
Copy the code

To generate a static HTML file, run:

yarn docs:build NPM run docs:build
Copy the code

Add other dependencies, common syntax

// Install the specified package yarn add *** // install all packages, note the difference with NPM YARNCopy the code

Notice the difference between โš ๏ธ dependencies and devDependencies

  1. --saveAdd the dependency’s name, version requirements todependencies
  2. --save-devAdd the dependency’s name, version requirements todevDependencies

2. Some conventional directory structures

  • In the use ofvuepressDuring development, some directory structures are agreed upon
. โ”œ โ”€ โ”€ docs โ”‚ โ”œ โ”€ โ”€ the vuepress (optional) โ”‚ โ”‚ โ”œ โ”€ โ”€ components (optional) โ”‚ โ”‚ โ”œ โ”€ โ”€ the theme (optional) โ”‚ โ”‚ โ”‚ โ”” โ”€ โ”€ Layout. The vue โ”‚ โ”‚ โ”œ โ”€ โ”€ public (optional) โ”‚ โ”‚ โ”œ โ”€ โ”€ styles (optional) โ”‚ โ”‚ โ”‚ โ”œ โ”€ โ”€ but styl โ”‚ โ”‚ โ”‚ โ”” โ”€ โ”€ the palette. Styl โ”‚ โ”‚ โ”œ โ”€ โ”€ templates (optional, Careful configuration) โ”‚ โ”‚ โ”‚ โ”œ โ”€ โ”€ dev. HTML โ”‚ โ”‚ โ”‚ โ”” โ”€ โ”€ SSR. HTML โ”‚ โ”‚ โ”œ โ”€ โ”€ the config, js (optional) โ”‚ โ”‚ โ”” โ”€ โ”€ enhanceApp. Js (optional) โ”‚ โ”‚ โ”‚ โ”œ โ”€ โ”€ the README. โ”‚ md โ”œ โ”€ โ”€ guide โ”‚ โ”‚ โ”” โ”€ โ”€ the README. Md โ”‚ โ”” โ”€ โ”€ config. The md โ”‚ โ”” โ”€ โ”€ package. The jsonCopy the code
  • The directory structure in this project is

Template โ”œ โ”€ docs โ”‚ โ”” โ”€. # vuepress configuration, presentation components, static resource โ”‚ โ”‚ โ”œ โ”€ compnents # demonstrate component โ”‚ โ”‚ | โ”” โ”€ demo # demo component โ”‚ โ”‚ โ”œ โ”€ public # static resource โ”‚ โ”‚ | โ”” โ”€ images # image file โ”‚ โ”‚ | โ”” โ”€ SCSS # style file โ”‚ โ”‚ โ”œ โ”€ config. Js # config file โ”‚ โ”‚ โ”” โ”€ enhanceApp # introduction file | โ”œ โ”€ compnent # component md file | โ”” โ”€ โ”œโ”€ โ”œโ”€ public โ”œโ”€ download.json # โ”œโ”€ public โ”œโ”€ download.txt # โ”œโ”€ public โ”œโ”€ download.txtCopy the code

3. Modify yourspackage.jsonfile

This document is the soul of the project ๐Ÿ‘ป and is one of the most important. For details, see Packagejson

  • Modifying the main entrancemain

The main field specifies the entry file to load, which requires (‘moduleName’) will load. The default value for this field is index.js under the module root.

"main": "dist/hekr-ui.umd.min.js"
Copy the code

You can build a single entry into a library by using the following command

vue-cli-service build --target lib --name myLib [entry]
Copy the code

The running results are as follows:

File Size Gzipped dist/hk-ui.umd.min.js 13.64kib 5.31 KiB dist/hk-ui.umd.js 43.87 KiB 11.37 KiB dist/hk-ui.mon.js Dist/HK-UI. CSS 0.54KIb 0.29kibCopy the code
  • addlibThe command
    "lib": "vue-cli-service build --target lib --name hk-ui --dest dist ./src/packages/index.js"
Copy the code

This entry can be a.js or a.vue file. If no entry is specified, SRC/app.vue is used.

Building a library outputs:

  • Dist/mylib.common.js: a CommonJS package for packers (unfortunately, Webpack doesn’t currently support ES Modules output)

  • Dist/mylib.umd. js: a UMD package for use directly by browsers or AMD loaders

  • Dist/mylib.umd.min.js: compressed UMD build

  • Dist/mylib.css: Extracted CSS file (you can force inlining by setting CSS: {extract: false} in vue.config.js)

4. Document configuration

After the project starts, we need to configure our Vuepress

/* * @describe: configuration file config. Js * @author: superDragon * @date: 2019-10-09 23:25:49 * @lasteditors: superDragon * @LastEditTime: 2019-10-10 18:49:25 */
let component = [
  'hk-button'.'hk-card'
]
module.exports = {
  base: '/hekr-ui/'.title: 'hekr-ui'.description: 'hekr-ui'.themeConfig: {
    nav: [{text: 'home'.link: '/' },
      {
        text: 'components'.link: '/component/hk-button'}].sidebar: {
      '/component/': component
    },
    lastUpdated: 'Last Update'.repo: ' '.repoLabel: 'GitLab'
  },
  plugins: [].serviceWorker: true
}
Copy the code

When we can successfully run the following

    "docs:dev": "vuepress dev docs"."docs:build": "vuepress build docs".Copy the code
  • Run locally

    Or: NPM run docs:dev

  • packaging

    Yarn docs:build # or NPM run docs:build

5. Continuous integration

Take GitHub Pages and Travis CI for example

  • Pack and place in the projectdocsIn the directorydistIn, pay attention to โš ๏ธ distinction;
  • The default build output location is used;
  • VuePress is installed in your project as a local dependency with NPM scripts configured as follows:
{
  "scripts": {
    "docs:build": "vuepress build docs"}}Copy the code
  1. Set the correct base in docs/.vuepress/config.js here we set /hekr-ui/ ๐ŸŒฟ.

    If you want to post to https://

    .github. IO /, you can skip this step, because base is “/” by default.

    If you plan to post to https://

    .github. IO /

    / (that is, your repository is at https://github.com/

    /

    ), set base to “/

    /”.




  2. In your project, create the following.travis. Yml file ๐Ÿ“ƒ:

language: node_js
node_js:
  - '8'

install:
  - npm install

script:
  - npm run build

after_success:
  - cd ./docs/.vuepress/dist
  - git init
  - git config --global user.name "${U_NAME}"
  - git config --global user.email "${U_EMAIL}"
  - git add .
  - git commit -m "๐Ÿ‘ทโ€Automatically Update from Travis -ci ๐Ÿ‘ทโ€"
  - git push --quiet --force  "https://${GH_TOKEN}@${GH_REF}" master:${P_BRANCH}

branches:
  only:
    - master

notifications:
  email:
    - [email protected]
  on_failure: always

# Note: you should set Environment Variables here or 'Settings' on travis-ci.org
env:
  global:
    # I added it to the Travis -ci environment variable
    - U_NAME: xkloveme
    - U_EMAIL: [email protected]
    - P_BRANCH: gh-pages
    - GH_REF: github.com/xkloveme/vue-v-xxx.git
Copy the code
  1. registeredgithub,travisAnd the configurationtravisRelated, enable build, ownGoogleportal
  2. For more configurations, see VuePress deployment

practice

Complete and package components

To prepare the HK-button component as an example, and export

  1. Add components and export
<template lang="pug"> el-tooltip(effect="dark" v-if="tips" :content="tips" placement="top-start") el-button.hk-button(:size="size" :type="type" v-bind="$attrs" @click="handleClick") slot el-button.hk-button(:size="size" v-else :type="type" v-bind="$attrs" @click="handleClick") slot </template> <script> import { Debounce } from './.. /util/debounce' export default { name: 'hk-button', props: { type: { type: String, default: 'primary' }, size: { type: String, default: 'medium' }, tips: { type: String, default: null } }, methods: { handleClick: Debounce(function (e) { this.$emit('click', e) }, 200) } } </script> <style lang="scss"> .hk-button { i + span { padding-left: 10px; } } </style>Copy the code
  1. Export components
import HkButton from './hk-button'

export default HkButton
Copy the code
  1. Export toinstall
Install * @author: superDragon * @date: 2019-10-09 23:21:11 * @lasteditors: superDragon * @LastEditTime: 2019-10-10 19:09:57 */
import HkButton from './hk-button'
import HKcard from './hk-card'

const install = Vue= > {
  Vue.component(HkButton.name, HkButton)
  Vue.component(HKcard.name, HKcard)
}

if (typeof window! = ='undefined' && window.Vue) {
  install(window.Vue)
}

export {
  HkButton,
  HKcard
}

export default { install }

Copy the code

Upload your ownnpm

Sign up at ยฎ๏ธ and switch to officialnpmThe source

The sign-innpm

Username: Username: // enter the Username you registered in the official website, press enter Password: Please make sure your password is correct, press Email:(this is Public) // enter the Email address bound to the official website registration, press enterCopy the code

upload

NPM publish // Here upload command, enterCopy the code

A Hekr-UI is released

undo

It’s easy to undo it within 24 hours.

npm unpublish --force 
Copy the code

You’re done

Let’s enjoy the taste of life, if we feel, the more we will live longer.

Give a star if you think it’s good

  • This article demonstrates the library address hekr-ui
  • The document address
  • Program source code