forked from toss/es-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
189 lines (175 loc) · 5.02 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// @ts-check
import fs from 'node:fs';
import { createRequire } from 'node:module';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import terserPlugin from '@rollup/plugin-terser';
import tsPlugin from '@rollup/plugin-typescript';
import dtsPlugin from 'rollup-plugin-dts';
// eslint-disable-next-line @typescript-eslint/naming-convention
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* @type {{
* exports: Record<string, string>;
* publishConfig: { browser: string };
* }}
*/
const packageJson = createRequire(import.meta.url)('./package.json');
const testPatterns = ['**/*.bench.ts', '**/*.spec.ts', '**/*.test.ts'];
export default () => {
clearDir('dist');
const entrypoints = Object.values(packageJson.exports).filter(f => /^(\.\/)?src\//.test(f) && f.endsWith('.ts'));
return [
libBuildOptions({
format: 'esm',
extension: 'mjs',
entrypoints,
outDir: 'dist',
sourcemap: false,
}),
libBuildOptions({
format: 'cjs',
extension: 'js',
entrypoints,
outDir: 'dist',
sourcemap: false,
}),
declarationOptions({
entrypoints,
outDir: 'dist',
}),
browserBuildConfig({
inputFile: './src/compat/index.ts',
outFile: packageJson.publishConfig.browser,
name: '_',
sourcemap: true,
}),
];
};
/**
* @type {(options: {
* entrypoints: string[];
* format: 'esm' | 'cjs';
* extension: 'js' | 'cjs' | 'mjs';
* outDir: string;
* sourcemap: boolean;
* }) => import('rollup').RollupOptions}
*/
function libBuildOptions({ entrypoints, extension, format, outDir, sourcemap }) {
const isESM = format === 'esm';
return {
input: mapInputs(entrypoints),
plugins: [
tsPlugin({
exclude: [...testPatterns],
compilerOptions: {
sourceMap: sourcemap,
inlineSources: sourcemap || undefined,
removeComments: !sourcemap,
declaration: false,
},
}),
],
output: {
format,
dir: outDir,
...fileNames(extension),
// Using preserveModules disables bundling and the creation of chunks,
// leading to a result that is a mirror of the input module graph.
preserveModules: isESM,
sourcemap,
generatedCode: 'es2015',
// Hoisting transitive imports adds bare imports in modules,
// which can make imports by JS runtimes slightly faster,
// but makes the generated code harder to follow.
hoistTransitiveImports: false,
},
};
}
/**
* @type {(options: {inputFile: string; outFile: string; name: string, sourcemap: boolean}) => import('rollup').RollupOptions}
*/
function browserBuildConfig({ inputFile, outFile, name, sourcemap }) {
return {
input: inputFile,
plugins: [
tsPlugin({
exclude: [...testPatterns],
compilerOptions: {
sourceMap: sourcemap,
inlineSources: sourcemap || undefined,
removeComments: true,
declaration: false,
},
}),
],
output: {
plugins: [
// Minify with terser, but with a configuration that optimizes for
// readability in browser DevTools (after re-indenting by DevTools).
terserPlugin({
// Terser defaults to ES5 for syntax it adds or rewrites
ecma: 2020,
// Readable function names (not just in final export)
keep_fnames: true,
// Turn off compress.sequences to keep the assignments to the toolkit
// object readable, instead of turning them into a huge list of
// comma-separated expressions.
compress: { sequences: false },
}),
],
format: 'iife',
name,
file: outFile,
sourcemap,
generatedCode: 'es2015',
},
};
}
/**
* @type {(options: {entrypoints: string[]; outDir: string}) => import('rollup').RollupOptions}
*/
function declarationOptions({ entrypoints, outDir }) {
return {
plugins: [dtsPlugin()],
input: mapInputs(entrypoints),
output: [
{
format: 'esm',
dir: outDir,
generatedCode: 'es2015',
...fileNames('d.mts'),
preserveModules: true,
preserveModulesRoot: 'src',
},
{
format: 'cjs',
dir: outDir,
generatedCode: 'es2015',
...fileNames('d.ts'),
preserveModules: true,
preserveModulesRoot: 'src',
},
],
};
}
/** @type {(srcFiles: string[]) => Record<string, string>} */
function mapInputs(srcFiles) {
return Object.fromEntries(
srcFiles.map(file => [file.replace(/^(\.\/)?src\//, '').replace(/\.[cm]?(js|ts)$/, ''), join(__dirname, file)])
);
}
function fileNames(extension = 'js') {
return {
entryFileNames: `[name].${extension}`,
chunkFileNames: `_chunk/[name]-[hash:6].${extension}`,
};
}
/** @type {(dir: string) => void} */
function clearDir(dir) {
const dirPath = join(__dirname, dir);
if (dir && fs.existsSync(dirPath)) {
fs.rmSync(dirPath, { recursive: true, force: true });
console.log(`cleared: ${dir}`);
}
}