-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrollup.config.js
113 lines (97 loc) · 2.87 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
import path from 'path'
import typescript from 'rollup-plugin-typescript2'
import json from '@rollup/plugin-json'
import { terser } from "rollup-plugin-terser";
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
if (!process.env.TARGET) {
throw new Error('TARGET package must be specified via --environment flag.')
}
const packagesDir = path.resolve(__dirname, 'packages')
const packageDir = path.resolve(packagesDir, process.env.TARGET)
const name = path.basename(packageDir)
const resolve = p => path.resolve(packageDir, p)
const pkg = require(resolve(`package.json`))
// 对多个formats,只执行一次检查
let hasTSChecked = false
const defaultFormats = ['es', 'iife']
const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
const packageFormats = inlineFormats || defaultFormats
const enableProd = process.env.NODE_ENV === 'production'
const enableSourceMap = !!process.env.SOURCE_MAP
const enableType = !!process.env.TYPES
export default packageFormats.map(format => createConfig(format))
function createConfig(format) {
const output = {
file: resolve(`dist/${name}.${format}.js`),
sourcemap: enableSourceMap,
externalLiveBindings: false,
format,
name: getUpperCamelCase(name),
}
const isBrowser = ['umd', 'iife', 'amd', 'system'].includes(format)
const shouldEmitDeclarations = enableType && !hasTSChecked
hasTSChecked = true
const external = [...Object.keys(pkg.peerDependencies || {})]
if (!isBrowser) {
external.push(...Object.keys(pkg.dependencies || {}))
}
const plugins = [
json({
namedExports: false
}),
typescript({
check: enableProd && !hasTSChecked,
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
tsconfigOverride: {
compilerOptions: {
sourceMap: enableSourceMap,
declaration: shouldEmitDeclarations,
},
include: [
`./packages/${name}/src/**/*`
]
},
useTsconfigDeclarationDir: true,
}),
postcss({
plugins: [autoprefixer],
extract: true,
sourceMap: enableSourceMap,
minimize: enableProd
}),
commonjs({
sourceMap: false
}),
nodeResolve({
preferBuiltins: true
}),
];
if (enableProd) {
plugins.push(
terser({
module: format === 'es',
compress: {
ecma: 2015,
pure_getters: true
}
})
);
}
return {
input: resolve('index.ts'),
plugins,
external,
output,
}
}
/**
* 短横线/下划线/小驼峰 转 大驼峰命名(UpperCamelCase)
*/
export function getUpperCamelCase(str) {
const reg = /(^|-|_)(\w)/g;
return str.replace(reg, ($, $1, $2) => $2.toUpperCase());
}