-
Notifications
You must be signed in to change notification settings - Fork 4
/
rollup.config.js
161 lines (155 loc) · 4.14 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { resolve } from 'path';
import dts from 'rollup-plugin-dts';
import shebang from 'rollup-plugin-preserve-shebang';
import esbuild from 'rollup-plugin-esbuild';
import fs from 'fs';
export default [
...createConfigs({
packageRoot: 'utils',
entryFile: 'src/index.ts',
plugins: [],
types: true,
}),
...createConfigs({
packageRoot: 'plugins/shared',
entryFile: 'src/index.ts',
plugins: [],
types: true,
}),
...createConfigs({
packageRoot: 'plugins/eslint',
entryFile: 'src/index.ts',
plugins: [],
types: true,
}),
...createConfigs({
packageRoot: 'plugins/es-start',
entryFile: 'src/index.ts',
plugins: [],
types: true,
}),
...createConfigs({
packageRoot: 'plugins/es-serve',
entryFile: 'src/index.ts',
plugins: [],
types: true,
}),
...createConfigs({
packageRoot: 'packages/es-exec',
entryFile: 'src/index.ts',
plugins: [],
types: true,
}),
...createConfigs({
packageRoot: 'packages/cli',
entryFile: 'src/main.ts',
plugins: [shebang()],
types: false,
}),
];
/**
* Creates rollup configurations based off of the {@see options}
*
* @param options Options to use to create the options.
* @param {string} options.packageRoot The root folder for the package.
* @param {string} options.entryFile The entry file inside of the package root
* to use as the bundle input.
* @param {Object[]} options.plugins List of additional plugins to include.
* @param {boolean} options.skipEsm If true, does not return an esm config.
* @param {boolean} options.skipCjs If true, does not return a commonjs config.
* @param {boolean} options.types If true, generates typedefs for the bundle.
*
* @returns The specified rollup configurations.
*/
function createConfigs({
packageRoot,
entryFile,
plugins,
skipEsm = false,
skipCjs = false,
types = false,
clean = true,
}) {
const input = resolve(packageRoot, entryFile);
const outDir = resolve(packageRoot, 'dist');
const options = {
input,
outDir,
plugins,
};
const config = { input, plugins: [esbuild(), ...plugins], output: [] };
if (clean) cleanDir(outDir);
if (!skipEsm) config.output.push(esmConfig(options));
if (!skipCjs) config.output.push(cjsConfig(options));
if (types) return [config, typesConfig(options)];
return [config];
}
/**
* Creates an esm configuration for rollup.
*
* @param options The options to use to create the esm package.
* @param {string} input The input file for the bundle.
* @param {string} packageRoot The root directory of the package.
* @param {Object[]} plugins The additional plugins.
*
* @returns The esm rollup configuration.
*/
function esmConfig({ outDir }) {
return {
// ESM
format: 'esm',
dir: outDir,
sourcemap: true,
preserveModules: true,
exports: 'named',
entryFileNames: '[name].mjs',
};
}
/**
* Creates a commonjs bundle configuration for rollup.
*
* @param options The options to use to create the esm package.
* @param {string} input The input file for the bundle.
* @param {string} packageRoot The root directory of the package.
* @param {Object[]} plugins The additional plugins.
*
* @returns The commonjs rollup configuration.
*/
function cjsConfig({ outDir }) {
return {
// CJS
format: 'cjs',
dir: outDir,
sourcemap: true,
preserveModules: true,
exports: 'named',
entryFileNames: '[name].cjs',
};
}
/**
* Creates a configuration for rollup to create type definitions for the input
* bundle.
*
* @param @param options The options to use to create the types package.
* @param {string} input The input file for the bundle.
* @param {string} packageRoot The root directory of the package.
* @param {Object[]} plugins The additional plugins.
*
* @returns The type definitions rollup configuration.
*/
function typesConfig({ input, outDir, plugins }) {
return {
input,
output: {
dir: outDir,
format: 'es',
preserveModules: true,
exports: 'named',
entryFileNames: '[name].d.ts',
},
plugins: [dts(), ...plugins],
};
}
function cleanDir(path) {
if (fs.existsSync(path)) fs.rmSync(path, { recursive: true });
}