参考地址:

经过前面的学习,我们来实践下配置vue文件的打包环境,并进行使用

基本插件安装及打包配置

使用命令方式进行打包

  1. "scripts": {
  2. "dev": "rollup -wc rollup.config.js",
  3. "build": "rollup -c rollup.config.js",
  4. "build:prod": "rollup -c rollup.config.prod.js"
  5. },
  6. "devDependencies": {
  7. "@babel/core": "^7.12.3",
  8. "@babel/preset-env": "^7.12.1",
  9. "@babel/plugin-transform-flow-strip-types": "^7.12.1",
  10. "rollup": "^2.33.1",
  11. "rollup-plugin-babel": "^4.4.0",
  12. "rollup-plugin-buble": "^0.19.8",
  13. "rollup-plugin-commonjs": "^10.1.0",
  14. "rollup-plugin-json": "^4.0.0",
  15. "rollup-plugin-node-resolve": "^5.2.0"
  16. },
  1. // rollup.config.js
  2. const path = require('path')
  3. const resolve = require('rollup-plugin-node-resolve')
  4. const commonjs = require('rollup-plugin-commonjs')
  5. const json = require('rollup-plugin-json')
  6. const babel = require('rollup-plugin-babel')
  7. const pathResolve = p => path.resolve(__dirname, p)
  8. module.exports = {
  9. input: pathResolve('./src/index.js'),
  10. output: [{
  11. file: pathResolve('./dist/datav.js'),
  12. format: 'umd',
  13. name: 'datav'
  14. }, {
  15. file: pathResolve('./dist/datav.es.js'),
  16. format: 'es',
  17. name: 'datav'
  18. }],
  19. //! 注意插件加载顺序
  20. plugins: [
  21. resolve(),
  22. json(),
  23. babel({
  24. exclude: 'node_modules/**'
  25. }),
  26. commonjs(),
  27. ]
  28. }

常见错误:
参考地址:https://blog.csdn.net/weixin_34037173/article/details/91473658
插件的加载顺序需要注意一下,不然有时候报错,搞不明白为什么

配置开发生产环境打包配置

目前主要需要解决的就是代码压缩问题,我们都知道开发环境都是以min.js结尾的文件,是经过压缩的,我们配置下生产环境的打包配置,和开发环境区分开。

  1. // package.json
  2. {
  3. "name": "rollup",
  4. "version": "1.0.0",
  5. "main": "index.js",
  6. "license": "MIT",
  7. "scripts": {
  8. "dev": " rollup -wc rollup.config.js --environment NODE_ENV:development ",
  9. "build": "rollup -c rollup.config.js --environment NODE_ENV:development ",
  10. "build:prod": "rollup -c rollup.config.js --environment NODE_ENV:production"
  11. },
  12. "devDependencies": {
  13. "@babel/core": "^7.12.3",
  14. "@babel/plugin-transform-flow-strip-types": "^7.12.1",
  15. "@babel/preset-env": "^7.12.1",
  16. "cross-env": "^7.0.2",
  17. "rollup": "^2.33.1",
  18. "rollup-plugin-babel": "^4.4.0",
  19. "rollup-plugin-buble": "^0.19.8",
  20. "rollup-plugin-commonjs": "^10.1.0",
  21. "rollup-plugin-json": "^4.0.0",
  22. "rollup-plugin-node-resolve": "^5.2.0",
  23. "rollup-plugin-terser": "^7.0.2"
  24. },
  25. "dependencies": {
  26. "sam-test-data": "^0.0.5"
  27. }
  28. }
  1. // rollup.config.js
  2. const path = require('path')
  3. const resolve = require('rollup-plugin-node-resolve')
  4. const commonjs = require('rollup-plugin-commonjs')
  5. const json = require('rollup-plugin-json')
  6. const babel = require('rollup-plugin-babel')
  7. const { terser } = require('rollup-plugin-terser')
  8. const pathResolve = p => path.resolve(__dirname, p)
  9. const outputOptions = require('./rollup-output-options')
  10. const isProd = process.env.NODE_ENV == 'production'
  11. // 通用的插件
  12. const basePlugins = [
  13. resolve(),
  14. json(),
  15. babel({
  16. exclude: 'node_modules/**'
  17. }),
  18. commonjs(),
  19. ]
  20. // 开发环境需要使用的插件
  21. const devPlugins = []
  22. // 生产环境需要使用的插件
  23. const prodPlugins = [
  24. terser({
  25. output: {
  26. ascii_only: true // 仅输出ascii字符
  27. },
  28. compress: {
  29. pure_funcs: ['console.log'] // 去掉console.log函数
  30. }
  31. })
  32. ]
  33. let plugins = [...basePlugins].concat(isProd ? prodPlugins : devPlugins)
  34. let config = {
  35. input: pathResolve('./src/index.js'),
  36. output: outputOptions,
  37. //! 注意插件加载顺序
  38. plugins: plugins
  39. }
  40. module.exports = config
  1. // rollup-output-options.js
  2. const path = require('path')
  3. const pathResolve = p => path.resolve(__dirname, p)
  4. const builds = [{
  5. file: pathResolve('./dist/datav.js'),
  6. format: 'umd',
  7. name: 'datav',
  8. globals: {
  9. vue: 'Vue'
  10. },
  11. env: 'development'
  12. }, {
  13. file: pathResolve('./dist/datav.es.js'),
  14. format: 'es',
  15. name: 'datav',
  16. env: 'development'
  17. }, {
  18. file: pathResolve('./dist/datav.min.js'),
  19. format: 'umd',
  20. name: 'datav',
  21. globals: {
  22. vue: 'Vue'
  23. },
  24. env: 'production'
  25. }, {
  26. file: pathResolve('./dist/datav.es.min.js'),
  27. format: 'es',
  28. name: 'datav',
  29. env: 'production'
  30. }]
  31. let output = builds.filter(item => {
  32. return item.env == process.env.NODE_ENV
  33. })
  34. module.exports = output

配置打包vue文件

我们要配置的是vue3的打包环境,下面开始

  • 安装所需要的插件:

    1. yarn add -D rollup-plugin-vue rollup-plugin-postcss @vue/compiler-sfc sass
  • 在通用插件中加入我们的插件 ```javascript const vue = require(‘rollup-plugin-vue’) const postcss = require(‘rollup-plugin-postcss’)

// 通用的插件 const basePlugins = [ resolve(), json(), vue(), babel({ exclude: ‘node_modules/**’ }), commonjs(), postcss() ]

  1. - 编写vue组件
  2. ```javascript
  3. // src/Test.vue
  4. <template>
  5. <div class="test">
  6. <div>{{ message }}</div>
  7. <div>count:{{ count }}</div>
  8. <div>double:{{ doubleCount }}</div>
  9. <button @click="handleClick">点击看double效果</button>
  10. </div>
  11. </template>
  12. <script>
  13. import { computed, ref } from 'vue';
  14. export default {
  15. name: 'TestComponent',
  16. setup() {
  17. const message = 'hello world';
  18. const count = ref(1);
  19. const doubleCount = computed(() => count.value * 2);
  20. const handleClick = () => {
  21. count.value++;
  22. };
  23. return {
  24. message,
  25. count,
  26. doubleCount,
  27. handleClick,
  28. };
  29. },
  30. };
  31. </script>
  32. <style lang="scss" scoped>
  33. .test {
  34. color: red;
  35. }
  36. </style>
// src/index.js
import Test from "./Test.vue";
export default function (Vue) {
  Vue.component(Test.name, Test)
}
  • 打包我们的库文件

    yarn build
    yarn build:prod
    
  • 在页面中进行使用

    // example/index.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>datav libs example</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@3.0.2/dist/vue.global.js"></script>
    <script src="../dist/datav.js"></script>
    </head>
    <body>
    <div id="app">
      {{massage}}
      <test-component/>
    </div>
    <script>
      Vue.createApp({
        setup() {
          var massage = 'hello world'
          return {
            massage
          }
        }
      }).use(datav).mount('#app')
    </script>
    </body>
    </html>