初始化项目

  • npm init -y
  • cnpm i -D rollup

配置

rollup.config.dev.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const path = require('path')
const rollupResolve = require('rollup-plugin-node-resolve')
const rollupCommonjs = require('rollup-plugin-commonjs')
const babel = require('rollup-plugin-babel')
const json = require('rollup-plugin-json')
const vue = require('rollup-plugin-vue')
const postcss = require('rollup-plugin-postcss')

const resolve = dir => path.resolve(__dirname, dir)
const inputPath = resolve('./src/index.js')
const outputUmdPath = resolve('./dist/cxvh.datav.js')
const outputEsmPath = resolve('./dist/cxvh.datav.esm.js')

module.exports = {
input: inputPath,
output: [
{
file: outputUmdPath,
format: 'umd',
name: 'cxvhDatav',
globals: {
vue: "Vue"
}
},
{
file: outputEsmPath,
format: 'esm',
globals: {
vue: "Vue"
}
}
],
plugins: [
vue(),
rollupResolve(),
rollupCommonjs(),
babel({
exclude: 'node_modules/**'
}),
postcss({
plugins: []
})
],
external: [
"vue"
]
}

rollup.config.prod.js

1
2
3
4
const { terser } = require('rollup-plugin-terser')
plugins: [
terser() // 压缩js
],

打包

rollup -c rollup.config.prod.js
rollup -wc rollup.config.dev.js