• 基于ElementUI封装组件库(二)


    这章继续解析ElementUI的css打包方式

    elementUI组件的css文件单独维护在/packages/theme-chalk/src中,每个单独的组件对应一个scss
    先看package.json

      "scripts": {
        "bootstrap": "yarn || npm i",
        "build:file": "node build/bin/iconInit.js & node build/bin/build-entry.js & node build/bin/i18n.js & node build/bin/version.js",
        "build:theme": "node build/bin/gen-cssfile && gulp build --gulpfile packages/theme-chalk/gulpfile.js && cp-cli packages/theme-chalk/lib lib/theme-chalk",
        "build:utils": "cross-env BABEL_ENV=utils babel src --out-dir lib --ignore src/index.js",
        "build:umd": "node build/bin/build-locale.js",
        "clean": "rimraf lib && rimraf packages/*/lib && rimraf test/**/coverage",
        "deploy:build": "npm run build:file && cross-env NODE_ENV=production webpack --config build/webpack.demo.js && echo element.eleme.io>>examples/element-ui/CNAME",
        "deploy:extension": "cross-env NODE_ENV=production webpack --config build/webpack.extension.js",
        "dev:extension": "rimraf examples/extension/dist && cross-env NODE_ENV=development webpack --watch --config build/webpack.extension.js",
        "dev:umd": "webpack --config build/webpack.conf.js",
        "dev": "npm run bootstrap && npm run build:file && cross-env NODE_ENV=development webpack-dev-server --config build/webpack.demo.js & node build/bin/template.js",
        "dev:play": "npm run build:file && cross-env NODE_ENV=development PLAY_ENV=true webpack-dev-server --config build/webpack.demo.js",
        "dist": "npm run clean && npm run build:file && npm run lint && webpack --config build/webpack.conf.js && webpack --config build/webpack.common.js && webpack --config build/webpack.component.js && npm run build:utils && npm run build:umd && npm run build:theme",
        "i18n": "node build/bin/i18n.js",
        "lint": "eslint src/**/* test/**/* packages/**/* build/**/* --quiet",
        "pub": "npm run bootstrap && sh build/git-release.sh && sh build/release.sh && node build/bin/gen-indices.js",
        "test": "npm run lint && npm run build:theme && cross-env CI_ENV=/dev/ BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
        "test:watch": "npm run build:theme && cross-env BABEL_ENV=test karma start test/unit/karma.conf.js"
      },
    build:theme 打包组件的scss文件
    "node build/bin/gen-cssfile && gulp build --gulpfile packages/theme-chalk/gulpfile.js && cp-cli packages/theme-chalk/lib lib/theme-chalk"
    node build/bin/gen-cssfile 创建每个组件的scss文件及index.scss文件
    gulp build --gulpfile packages/theme-chalk/gulpfile.js 将组件的/packages/theme-chalk/src/**.scss  解析压缩到./lib中,可以单独运行一下看看效果
    'use strict';
    
    const { series, src, dest } = require('gulp');
    const sass = require('gulp-sass');
    const autoprefixer = require('gulp-autoprefixer');
    const cssmin = require('gulp-cssmin');
    
    function compile() {
      return src('./src/*.scss')
        .pipe(sass.sync())
        .pipe(autoprefixer({
          browsers: ['ie > 9', 'last 2 versions'],
          cascade: false
        }))
        .pipe(cssmin())
        .pipe(dest('./lib'));
    }
    
    function copyfont() {
      return src('./src/fonts/**')
        .pipe(cssmin())
        .pipe(dest('./lib/fonts'));
    }
    
    exports.build = series(compile, copyfont);
    cp-cli packages/theme-chalk/lib lib/theme-chalk 将packages/theme-chalk/lib文件copy到lib/theme-chalk

    lib目录如下

     注:

    1.为什么全量引入时还需要引入css文件,而按需引入不需要
    element组件样式是分离的,build出来的包只包含模版和js代码,没有css文件

    按需引入时,需安装babel-plugin-component,配置.babelrc 文件

    {
      "presets": [["es2015", { "modules": false }]],
      "plugins": [
        [
          "component",
          {
            "libraryName": "element-ui",
            "styleLibraryName": "theme-chalk"
          }
        ]
      ]
    }
    import Button from 'components' =>

     var button require('components/lib/button')

    require('components/lib/theme-chalk/button.css')

    2.引申:package.json 中files: {} 为npm publish中的ignore配置

    "files": [
      "lib",
      "src",
      "packages",
      "types"
    ]
    
    
  • 相关阅读:
    MVC3中输出Html标签的方法
    Server.MapPath 出现未将对象引用设置到对象的实例
    谈谈网站静态化
    WCF 服务应用程序与 服务库之间的区别
    插入中国所有省和市的SQL语句--以后用
    KiCad 元件值 F4NNIU 规范 (2020-04-30)[31.98%]
    FastAdmin 安装后点登录没有反应怎么办?
    笔记:读英国老太太的复仇计划 (2019-10-15)
    KiCad 工程用 Git 管理需要忽略哪些文件?
    关于 SSD 的接口和相关名词(2019-09-10)
  • 原文地址:https://www.cnblogs.com/yaokunlun/p/14347616.html
Copyright © 2020-2023  润新知