forked from formkit/formkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
135 lines (126 loc) · 3.43 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
import typescript from '@rollup/plugin-typescript'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import postcss from 'rollup-plugin-postcss'
import postcssNesting from 'postcss-nesting'
import autoprefixer from 'autoprefixer'
import atImport from 'postcss-import'
// import typescript2 from 'rollup-plugin-typescript2'
// import vue from 'rollup-plugin-vue'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const pkg = process.env.PKG
const format = process.env.FORMAT
const declarations = process.env.DECLARATIONS ? true : false
const theme = process.env.THEME || false
const plugin = process.env.PLUGIN || false
if (!pkg) throw Error('Please include a package to bundle')
if (!format) throw Error('Please include a bundle format')
const rootPath = resolve(__dirname, `packages/${pkg}`)
const tsConfig = createTypeScriptConfig()
export default {
external: ['vue', 'react', 'unocss', 'tailwindcss', 'windicss'],
input: createInputPath(),
output: createOutputConfig(),
plugins: createPluginsConfig(),
}
/**
* Create the expected path for the input file.
*/
function createInputPath() {
let subpath = ''
if (theme) subpath = 'css/' + theme + '/'
if (plugin) subpath = `${plugin}/`
return `${rootPath}/src/${subpath}index.ts`
}
/**
* Creates rollup output configuration.
*/
function createOutputConfig() {
if (!declarations) {
const extras = {}
let fileName =
format !== 'iife'
? `index.${format === 'esm' ? 'mjs' : format}`
: `formkit-${pkg}.js`
if (format === 'iife') {
extras.globals = {
vue: 'Vue',
}
}
if (theme) fileName = theme + '/theme.js'
if (plugin) fileName = `${plugin}/${fileName}`
return {
file: `${rootPath}/dist/${fileName}`,
name:
format === 'iife'
? `FormKit${pkg[0].toUpperCase()}${pkg.substr(1)}`
: `@formkit/${pkg}`,
format,
...extras,
}
}
return {
dir: `${rootPath}/dist`,
format: 'esm',
}
}
/**
* Creates the appropriate plugins array.
*/
function createPluginsConfig() {
const plugins = []
if (format === 'iife' && pkg === 'vue') {
plugins.push(nodeResolve())
}
if (pkg === 'themes') {
plugins.push(
postcss({
from: `${rootPath}/src/css/${theme}/${theme}.css`,
plugins: [atImport(), postcssNesting(), autoprefixer()],
extract: true,
})
)
}
// This commented out code is used for compiling
// .vue SFC files — current we dont have any:
// if (pkg === 'vue') {
// plugins.push(typescript2(tsConfig))
// plugins.push(
// vue({
// exposeFilename: false,
// })
// )
// } else {
plugins.push(typescript(tsConfig))
// }
return plugins
}
/**
* Creates the rollup/plugin-typescript plugin configuration, which is roughly
* equivalent to the typescript compilerOptions.
*/
function createTypeScriptConfig() {
let include = `./packages/${pkg}/src/**/*`
let out = `${rootPath}/dist`
if (plugin) {
include = `./packages/${pkg}/src/${plugin}/**/*`
out = `${rootPath}/dist/${plugin}`
}
const base = {
tsconfig: 'tsconfig.json',
rootDir: `./`,
outDir: out,
include: [include],
noEmitOnError: true,
}
if (!declarations) {
return base
}
// delete base.rootDir
return Object.assign(base, {
declaration: true,
emitDeclarationOnly: true,
})
}