1 Star 0 Fork 44

ff-space/lunisolar

forked from 簡 Kan/lunisolar 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
rollup.config.js 4.03 KB
一键复制 编辑 原始数据 按行查看 历史
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ff-blog/lunisolar.git
git@gitee.com:ff-blog/lunisolar.git
ff-blog
lunisolar
lunisolar
main

搜索帮助

D67c1975 1850385 1daf7b77 1850385