-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.config.js
100 lines (93 loc) · 2.78 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const babel = require("@rollup/plugin-babel"); // 引入 babel
const commonjs = require("@rollup/plugin-commonjs"); // 引入 cjs 插件
const { nodeResolve } = require("@rollup/plugin-node-resolve"); // 引入 resolve
const typescript = require("rollup-plugin-typescript2"); // 引入 ts
const { terser } = require("rollup-plugin-terser"); // 压缩打包文件
const path = require('path')
const fs = require('fs');
const resolvePath = (src) => {
return path.resolve(__dirname, '.' , src)
}
const filePath = resolvePath('src')
const extensions = ['.js', '.ts'];
const pkg = require('./package.json'); // 从package.json引入
const version = pkg.version; // 项目版本
const license = pkg.license; // 协议
const author = pkg.author; // 作者
const banner =
'/*!\n' +
` * ${pkg.name} v${version}\n` +
` * (c) ${new Date().getFullYear()} ~ 至今 ${author}\n` +
` * Released under the ${license} License.\n` +
' */';
const filePathArr = []
const filenameArr = []
// 构建 exports 配置项
const buildRollupOptions = (filePathArr) => {
const rollupOptions = []
const plugins = [
nodeResolve({
extensions,
modulesOnly: true
}),
commonjs(),
typescript(),
babel({
babelHelpers: 'runtime',
include: 'src/**',
exclude: 'node_modules/**',
extensions
}),
terser()
]
filePathArr.forEach((item) => {
rollupOptions.push({
input: resolvePath(`.${item}`),
output: {
file: item.replace('/src', 'dist').replace('index.ts', `index.min.esm.js`),
format: 'esm',
banner,
name: `${item}`,
},
plugins,
})
rollupOptions.push({
input: resolvePath(`.${item}`),
output: {
file: item.replace('/src', 'dist').replace('index.ts', `index.min.umd.js`),
format: 'umd',
banner,
name: `${item}`,
},
plugins,
})
})
return rollupOptions
}
// 读取文件
const fileDisplay = (filePath) => {
const readdirSync = fs.readdirSync(filePath)
readdirSync.forEach((filename) => {
//获取当前文件的绝对路径
if(filename !== 'index.ts') {
filenameArr.push(filename)
}
const filedir = path.join(filePath, filename)
const stats = fs.statSync(filedir)
// 是否是文件
const isFile = stats.isFile()
// 是否是文件夹
const isDir = stats.isDirectory()
if (isFile) {
// 这块我自己处理了多余的绝对路径,第一个 replace 是替换掉那个路径,第二个是所有满足\\的直接替换掉
filePathArr.push(filedir.replace(__dirname, '').replace(/\\/img, '/'))
}
// 如果是文件夹
if (isDir) fileDisplay(filedir);
});
}
const getOptions = () => {
fileDisplay(filePath)
return buildRollupOptions(filePathArr)
}
module.exports = getOptions()