- https://www.yuque.com/docs/share/899569e3-29ce-4b3d-b16c-235caa243d84?# 《快速入门rollup.js》
- https://www.yuque.com/docs/share/9336a052-144a-46b7-a589-4a7616217e10?# 《进阶rollup.js》
- https://www.yuque.com/docs/share/675b7fe0-0c57-4327-9620-63b0654ea250?# 《精通rollup.js——前置学习之基础知识篇》
- https://www.yuque.com/docs/share/e14ac5b2-9bf2-4536-9119-3ee5bb8a8490?# 《精通rollup.js——前置学习之rollup.js插件篇》
- https://www.yuque.com/docs/share/695e98ac-9b3c-43a1-ae08-bb1cd3ad48ce?# 《10分钟快速精通rollup.js——Vue.js源码打包原理深度分析》
参考地址:
- https://blog.csdn.net/weixin_34037173/article/details/91473658
- https://segmentfault.com/a/1190000010628352
- https://www.imooc.com/article/293823
经过前面的学习,我们来实践下配置vue文件的打包环境,并进行使用
基本插件安装及打包配置
使用命令方式进行打包
"scripts": {
"dev": "rollup -wc rollup.config.js",
"build": "rollup -c rollup.config.js",
"build:prod": "rollup -c rollup.config.prod.js"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@babel/plugin-transform-flow-strip-types": "^7.12.1",
"rollup": "^2.33.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-buble": "^0.19.8",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0"
},
// rollup.config.js
const path = require('path')
const resolve = require('rollup-plugin-node-resolve')
const commonjs = require('rollup-plugin-commonjs')
const json = require('rollup-plugin-json')
const babel = require('rollup-plugin-babel')
const pathResolve = p => path.resolve(__dirname, p)
module.exports = {
input: pathResolve('./src/index.js'),
output: [{
file: pathResolve('./dist/datav.js'),
format: 'umd',
name: 'datav'
}, {
file: pathResolve('./dist/datav.es.js'),
format: 'es',
name: 'datav'
}],
//! 注意插件加载顺序
plugins: [
resolve(),
json(),
babel({
exclude: 'node_modules/**'
}),
commonjs(),
]
}
常见错误:
参考地址:https://blog.csdn.net/weixin_34037173/article/details/91473658
插件的加载顺序需要注意一下,不然有时候报错,搞不明白为什么
配置开发生产环境打包配置
目前主要需要解决的就是代码压缩问题,我们都知道开发环境都是以min.js结尾的文件,是经过压缩的,我们配置下生产环境的打包配置,和开发环境区分开。
// package.json
{
"name": "rollup",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": " rollup -wc rollup.config.js --environment NODE_ENV:development ",
"build": "rollup -c rollup.config.js --environment NODE_ENV:development ",
"build:prod": "rollup -c rollup.config.js --environment NODE_ENV:production"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/plugin-transform-flow-strip-types": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"cross-env": "^7.0.2",
"rollup": "^2.33.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-buble": "^0.19.8",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^7.0.2"
},
"dependencies": {
"sam-test-data": "^0.0.5"
}
}
// rollup.config.js
const path = require('path')
const resolve = require('rollup-plugin-node-resolve')
const commonjs = require('rollup-plugin-commonjs')
const json = require('rollup-plugin-json')
const babel = require('rollup-plugin-babel')
const { terser } = require('rollup-plugin-terser')
const pathResolve = p => path.resolve(__dirname, p)
const outputOptions = require('./rollup-output-options')
const isProd = process.env.NODE_ENV == 'production'
// 通用的插件
const basePlugins = [
resolve(),
json(),
babel({
exclude: 'node_modules/**'
}),
commonjs(),
]
// 开发环境需要使用的插件
const devPlugins = []
// 生产环境需要使用的插件
const prodPlugins = [
terser({
output: {
ascii_only: true // 仅输出ascii字符
},
compress: {
pure_funcs: ['console.log'] // 去掉console.log函数
}
})
]
let plugins = [...basePlugins].concat(isProd ? prodPlugins : devPlugins)
let config = {
input: pathResolve('./src/index.js'),
output: outputOptions,
//! 注意插件加载顺序
plugins: plugins
}
module.exports = config
// rollup-output-options.js
const path = require('path')
const pathResolve = p => path.resolve(__dirname, p)
const builds = [{
file: pathResolve('./dist/datav.js'),
format: 'umd',
name: 'datav',
globals: {
vue: 'Vue'
},
env: 'development'
}, {
file: pathResolve('./dist/datav.es.js'),
format: 'es',
name: 'datav',
env: 'development'
}, {
file: pathResolve('./dist/datav.min.js'),
format: 'umd',
name: 'datav',
globals: {
vue: 'Vue'
},
env: 'production'
}, {
file: pathResolve('./dist/datav.es.min.js'),
format: 'es',
name: 'datav',
env: 'production'
}]
let output = builds.filter(item => {
return item.env == process.env.NODE_ENV
})
module.exports = output
配置打包vue文件
我们要配置的是vue3的打包环境,下面开始
安装所需要的插件:
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() ]
- 编写vue组件
```javascript
// src/Test.vue
<template>
<div class="test">
<div>{{ message }}</div>
<div>count:{{ count }}</div>
<div>double:{{ doubleCount }}</div>
<button @click="handleClick">点击看double效果</button>
</div>
</template>
<script>
import { computed, ref } from 'vue';
export default {
name: 'TestComponent',
setup() {
const message = 'hello world';
const count = ref(1);
const doubleCount = computed(() => count.value * 2);
const handleClick = () => {
count.value++;
};
return {
message,
count,
doubleCount,
handleClick,
};
},
};
</script>
<style lang="scss" scoped>
.test {
color: red;
}
</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>