-
Notifications
You must be signed in to change notification settings - Fork 23
/
rollup.config.js
145 lines (136 loc) · 4.03 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import fs from 'fs'
import path from 'path'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import ts from 'rollup-plugin-typescript2'
import terser from '@rollup/plugin-terser'
import copy from 'rollup-plugin-copy'
import clear from 'rollup-plugin-clear'
import pkg from './package.json'
import dts from 'rollup-plugin-dts'
import filesize from 'rollup-plugin-filesize'
const extensions = ['.js', '.jsx', '.ts', '.tsx']
const upCaseFirst = str => (str[0] ? str[0].toUpperCase() + str.slice(1) : '')
const formatName = n => {
return n
.trim()
.replace(/\.(js|ts)$/, '')
.split('-')
.map((v, i) => (i === 0 ? v.trim() : upCaseFirst(v.trim())))
.join('')
}
// plugins
const tsPlugin = ts({ extensions })
const terserPlugin = terser()
const dtsPlugin = dts()
// fileName: pkg.module,
const configFactory = config => {
const { input, fileName, filePath, name, isClear, createEs, copyFile, pluginsPush } = config
// load rollup plugins
const plugins = [resolve(), commonjs(), tsPlugin, terserPlugin, filesize()]
if (isClear) plugins.unshift(clear({ targets: ['dist', 'plugins', 'locale', 'markers'] }))
if (copyFile) plugins.push(copy(copyFile))
if (pluginsPush) {
plugins.push(...pluginsPush)
}
// output
const output = [
{
// UMD for browser-friendly build
name: name || 'lunisolar',
file: filePath && fileName ? path.join(filePath, `${fileName}.js`) : pkg.main,
format: 'umd'
}
]
if (createEs) {
output.push({
// ES module for bundlers build
file: fileName ? path.join(filePath, `${fileName}.esm.js`) : pkg.module,
format: 'es'
})
}
return {
input,
output,
plugins
}
}
const configs = [
configFactory({
input: 'src/index.ts',
isClear: true,
createEs: true,
copyFile: {
targets: [
{
src: 'typings/plugins/**/*',
dest: 'plugins'
}
]
}
})
]
// 对指定dir打包的配置
const configDir = (dir, config) => {
const dirPath = path.join(__dirname, 'src', dir)
const dirNames = fs.readdirSync(dirPath)
const namePrefix = config?.namePrefix ?? null
const inputName = config?.inputName ?? ''
const useDts = config?.useDts ?? false
for (const dirName of dirNames) {
const fixDirName = namePrefix ? namePrefix + upCaseFirst(dirName) : dirName
const input = path.join(dirPath, dirName, inputName)
const filePath = path.join(__dirname, dir)
const fileName = dirName.replace(/\.(js|ts)$/, '')
const config = configFactory({
name: formatName(fixDirName),
input,
filePath,
fileName
})
configs.push(config)
if (useDts) {
configs.push({
input,
output: {
file: path.join(filePath, `${fileName}.d.ts`),
format: 'es'
},
plugins: [dtsPlugin]
})
}
}
}
// 对插件语言包进行打包的配置
const configPluginLocaleDir = () => {
const dirPath = path.join(__dirname, 'src', 'plugins')
const dirNames = fs.readdirSync(dirPath)
for (const dirName of dirNames) {
const localePath = path.join(dirPath, dirName, 'locale')
const localeNames = fs.readdirSync(localePath)
for (const localeName of localeNames) {
const filePath = path.join(__dirname, 'plugins', dirName, 'locale')
const fileName = localeName.replace(/\.(js|ts)$/, '')
const config = configFactory({
name: dirName + '_' + formatName(localeName),
input: path.join(localePath, localeName),
filePath,
fileName
})
configs.push(config)
configs.push({
input: path.join(localePath, localeName),
output: {
file: path.join(filePath, `${fileName}.d.ts`),
format: 'es'
},
plugins: [dtsPlugin]
})
}
}
}
configDir('plugins', { namePrefix: 'lunisolarPlugin', inputName: 'index.ts' })
configDir('locale', { namePrefix: 'lunisolarLocale' })
configDir('markers', { namePrefix: 'lunisolarMarkers', useDts: true })
configPluginLocaleDir()
export default configs