-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
97 lines (81 loc) · 2.56 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
import fs from 'fs'
import path from 'path'
import postcss from 'rollup-plugin-postcss'
import vuePlugin from 'rollup-plugin-vue'
import dtsPlugin from 'rollup-plugin-dts'
import replace from 'rollup-plugin-replace'
import tsPlugin from 'rollup-plugin-typescript2'
import resolveNodePlugin from '@rollup/plugin-node-resolve'
import lodash from 'lodash'
const { kebabCase, camelCase, upperFirst } = lodash
const __dirname = (() => {
const path = new URL(import.meta.url).pathname
return path.substring(0, path.lastIndexOf('/'))
})()
const packageName = kebabCase(process.env.PACKAGE)
if (!packageName) {
throw new Error('No package specified.')
}
const packagePath = path.resolve(__dirname, 'packages', packageName)
const srcIndexPath = path.resolve(packagePath, 'src/index.ts')
const demoIndexPath = path.resolve(packagePath, 'demo/index.ts')
if (!fs.existsSync(packagePath)) {
throw new Error('Incorrect package name.')
}
const formats = process.env.FORMATS?.split('+') || ['es', 'cjs', 'iife']
const basicConfig = process.env.DEMO ? createConfig(demoIndexPath, 'demo') : createConfig(srcIndexPath)
const configs = [basicConfig]
if (process.env.BUILD_TYPES) {
const typeConfig = {
input: `${packagePath}/dist/types/${packageName}/src/index.d.ts`,
output: {
file: `${packagePath}/dist/index.d.ts`,
format: 'es',
},
plugins: [dtsPlugin()],
}
configs.push(typeConfig)
}
export default configs
function createConfig(entryFilePath, outFileBaseName = null, options = {}) {
outFileBaseName = outFileBaseName || path.basename(entryFilePath, '.ts')
return {
input: entryFilePath,
output: formats.map((format) => createOutputConfig(outFileBaseName, format)),
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify(process.env.PRODUCTION ? 'production' : 'development'),
}),
tsPlugin({
useTsconfigDeclarationDir: true,
tsconfigOverride: {
compilerOptions: {
declarationDir: `${packagePath}/dist/types`,
},
},
}),
vuePlugin({
preprocessStyles: true,
}),
postcss(),
resolveNodePlugin(),
],
external: ['vue'],
...options,
}
}
function createOutputConfig(outFileBaseName, format) {
const outputConfig = {
file: `${packagePath}/dist/${outFileBaseName}.${format}.js`,
sourcemap: true,
externalLiveBindings: false,
format,
}
if (format === 'iife') {
outputConfig.name = 'Muku' + upperFirst(camelCase(packageName))
outputConfig.globals = {
vue: 'Vue',
}
}
return outputConfig
}