-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathrollup.config.mjs
123 lines (107 loc) · 2.91 KB
/
rollup.config.mjs
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
import replace from '@rollup/plugin-replace'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import ts from 'rollup-plugin-typescript2'
import alias from '@rollup/plugin-alias'
import terser from '@rollup/plugin-terser'
import path from 'path'
import { rimraf } from 'rimraf'
import camelCase from 'camelcase'
import pkg from './package.json' with { type: 'json' }
const cwd = process.cwd()
rimraf.sync(path.join(cwd, './dist'))
const banner = `/*!
* ${pkg.name} v${pkg.version}
* (c) ${new Date().getFullYear()} Eduardo San Martin Morote
* @license MIT
*/`
const exportName = camelCase(pkg.name, { pascalCase: true })
function createEntry(
{
format, // Rollup format (iife, umd, cjs, es)
external, // Rollup external option
input = 'src/index.ts', // entry point
env = 'development', // NODE_ENV variable
minify = false,
isBrowser = false, // produce a browser module version or not
} = {
input: 'src/index.ts',
env: 'development',
minify: false,
isBrowser: false,
}
) {
// force production mode when minifying
if (minify) env = 'production'
const config = {
input,
plugins: [
replace({
preventAssignment: true,
values: {
__VERSION__: pkg.version,
'process.env.NODE_ENV': `'${env}'`,
},
}),
alias({
// resolve: ['.ts', '.js'],
// entries: [{ find: 'firebase', replacement: path.join(__dirname, './stub') }],
}),
],
output: {
banner,
file: `dist/${pkg.name}.UNKNOWN.js`,
format,
},
}
if (format === 'iife') {
config.output.file = pkg.unpkg
config.output.name = exportName
} else if (format === 'es') {
config.output.file = pkg.module
} else if (format === 'cjs') {
config.output.file = pkg.module.replace('js', 'cjs')
}
if (!external) {
config.plugins.push(resolve(), commonjs())
} else {
config.external = external
}
config.plugins.push(
ts({
// only check once, during the es version with browser (it includes external libs)
check: !tsChecked,
tsconfigOverride: {
exclude: ['__tests__'],
compilerOptions: {
// same for d.ts files
declaration: !tsChecked,
target: format === 'es' && !isBrowser ? 'esnext' : 'es2015',
},
},
})
)
tsChecked = true
if (minify) {
config.plugins.push(
terser({
module: format === 'es',
})
)
config.output.file = config.output.file.replace(/\.([mc]?js)$/i, '.prod.$1')
}
return config
}
let tsChecked = false
const builds = [
createEntry({ format: 'cjs' }),
createEntry({ format: 'es' }),
createEntry({ format: 'cjs', minify: true }),
]
if (pkg.unpkg)
builds.push(
createEntry({ format: 'iife' }),
createEntry({ format: 'iife', minify: true }),
createEntry({ format: 'es', minify: true })
)
export default builds