forked from atomiks/tippyjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.build.js
153 lines (131 loc) · 4.4 KB
/
rollup.build.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
146
147
148
149
150
151
152
153
/* eslint-disable no-console, @typescript-eslint/no-var-requires */
const fs = require('fs')
const pkg = require('./package.json')
const { rollup } = require('rollup')
const babel = require('rollup-plugin-babel')
const { terser } = require('rollup-plugin-terser')
const sass = require('rollup-plugin-sass')
const postcss = require('postcss')
const autoprefixer = require('autoprefixer')
const cssnano = require('cssnano')
const resolve = require('rollup-plugin-node-resolve')
// const commonjs = require('rollup-plugin-commonjs')
const json = require('rollup-plugin-json')
const cssOnly = require('rollup-plugin-css-only')
const replace = require('rollup-plugin-replace')
const { green, blue } = require('colorette')
const BANNER = `/**!
* tippy.js v${pkg.version}
* (c) 2017-${new Date().getFullYear()} atomiks
* MIT License
*/`
const NAMESPACE_PREFIX = process.env.NAMESPACE || 'tippy'
const extensions = ['.js', '.ts']
const plugins = {
babel: babel({
exclude: 'node_modules/**',
extensions,
}),
replace: replace({
__NAMESPACE_PREFIX__: NAMESPACE_PREFIX,
}),
minify: terser(),
resolve: resolve({ extensions }),
css: cssOnly({ output: false }),
json: json(),
}
const BASE_OUTPUT_CONFIG = {
name: 'tippy',
globals: { 'popper.js': 'Popper' },
sourcemap: true,
}
const BASE_PLUGINS = [plugins.replace, plugins.resolve, plugins.json]
const pluginConfigs = {
index: [plugins.babel, ...BASE_PLUGINS],
indexMinify: [plugins.babel, ...BASE_PLUGINS, plugins.minify],
all: [plugins.babel, ...BASE_PLUGINS, plugins.css],
allMinify: [plugins.babel, ...BASE_PLUGINS, plugins.minify, plugins.css],
}
const createPluginSCSS = output => {
return sass({
output,
options: {
data: `$namespace-prefix: ${NAMESPACE_PREFIX};`,
},
processor: css =>
postcss([autoprefixer, cssnano])
.process(css, { from: undefined })
.then(result => result.css),
})
}
const createRollupConfigWithoutPlugins = input => plugins => ({
input,
plugins,
external: ['popper.js'],
})
const createPreparedOutputConfig = format => (file, { min = false } = {}) => {
const isCSS = ['css', 'themes'].includes(format)
return {
...BASE_OUTPUT_CONFIG,
format: isCSS ? 'umd' : format,
sourcemap: !isCSS,
file: format === 'css' ? 'index.js' : `./${format}/${file}`,
banner: min ? undefined : BANNER,
}
}
const getRollupConfigs = {
css: createRollupConfigWithoutPlugins('./build/css.js'),
index: createRollupConfigWithoutPlugins('./build/index.js'),
all: createRollupConfigWithoutPlugins('./build/all.js'),
}
const getOutputConfigs = {
bundle: [
createPreparedOutputConfig('umd'),
createPreparedOutputConfig('esm'),
],
css: createPreparedOutputConfig('css'),
theme: createPreparedOutputConfig('themes'),
}
const build = async () => {
console.log(blue('⏳ Building bundles...'))
const preCSSBundle = await rollup(
getRollupConfigs.css(createPluginSCSS('./index.css')),
)
await preCSSBundle.write(getOutputConfigs.css('./index.js'))
fs.unlinkSync('./index.js')
console.log('CSS done')
const bundles = {
index: await rollup(getRollupConfigs.index(pluginConfigs.index)),
indexMin: await rollup(getRollupConfigs.index(pluginConfigs.indexMinify)),
all: await rollup(getRollupConfigs.all(pluginConfigs.all)),
allMin: await rollup(getRollupConfigs.all(pluginConfigs.allMinify)),
}
// Standard UMD + ESM
for (const getOutputConfig of getOutputConfigs.bundle) {
const outputConfigs = {
index: getOutputConfig('index.js'),
indexMin: getOutputConfig('index.min.js', { min: true }),
all: getOutputConfig('index.all.js'),
allMin: getOutputConfig('index.all.min.js', { min: true }),
}
bundles.index.write(outputConfigs.index)
bundles.indexMin.write(outputConfigs.indexMin)
bundles.all.write(outputConfigs.all)
bundles.allMin.write(outputConfigs.allMin)
}
console.log(green('Bundles complete'))
console.log(blue('\n⏳ Building CSS themes...'))
for (const theme of fs.readdirSync('./build/themes')) {
const preparedThemeConfig = createRollupConfigWithoutPlugins(
`./build/themes/${theme}`,
)
const outputFile = `./themes/${theme.replace('.js', '.css')}`
const bundle = await rollup(
preparedThemeConfig(createPluginSCSS(outputFile)),
)
await bundle.write(getOutputConfigs.theme(theme))
fs.unlinkSync(`./themes/${theme}`)
}
console.log(green('✓ Themes\n'))
}
build()