-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
rollup.config.js
84 lines (74 loc) · 1.85 KB
/
rollup.config.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* eslint-disable no-console */
const path = require('path')
const fs = require('fs')
const rollup = require('rollup')
const { babel } = require('@rollup/plugin-babel')
const { uglify } = require('rollup-plugin-uglify')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const commonjs = require('@rollup/plugin-commonjs')
const typescript = require('@rollup/plugin-typescript')
const version = require('./package.json').version
const banner
= '/*!\n'
+ ` * Vue3-Lazyload.js v${version}\n`
+ ' * A Vue3.x image lazyload plugin' + '\n'
+ ` * (c) ${new Date().getFullYear()} MuRong <[email protected]>\n`
+ ' * Released under the MIT License.\n'
+ ' */\n'
const external = [
'vue',
'@vue/composition-api',
]
const commonPlugin = [
commonjs(),
babel(),
typescript(),
nodeResolve(),
]
async function build(options, outputOptions) {
try {
const bundle = await rollup.rollup(options)
const { output } = await bundle.generate({
format: outputOptions.format,
name: 'VueLazyload',
})
exists()
await write(path.resolve(__dirname, outputOptions.filename), output[0].code)
}
catch (e) {
console.error(e)
}
}
function getSize(code) {
return `${(code.length / 1024).toFixed(2)}kb`
}
function blue(str) {
return `\x1B[1m\x1B[34m${str}\x1B[39m\x1B[22m`
}
function write(dest, code) {
return new Promise((resolve, reject) => {
code = banner + code
fs.writeFile(dest, code, (err) => {
if (err)
return reject(err)
console.log(`${blue(dest)} ${getSize(code)}`)
resolve()
})
})
}
function exists() {
const pathE = path.resolve(__dirname, 'dist/')
if (!fs.existsSync(pathE))
fs.mkdirSync(pathE)
}
build({
input: path.resolve(__dirname, 'src/index.ts'),
plugins: [
...commonPlugin,
uglify(),
],
external,
}, {
format: 'umd',
filename: 'dist/index.min.js',
})