/
index.ts
59 lines (52 loc) · 1.84 KB
/
index.ts
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
import { rollup, Plugin, OutputChunk } from 'rollup';
import { isURL } from '@stoplight/path';
import { isPackageImport } from './utils/isPackageImport';
import { dedupeRollupPlugins } from './utils/dedupeRollupPlugins';
export type BundleOptions = {
plugins: Plugin[];
target: 'node' | 'browser' | 'runtime';
format?: 'esm' | 'commonjs' | 'iife';
treeshake?: boolean; // false by default
fullOutput?: boolean;
};
export { IO } from './types';
export async function bundleRuleset(
input: string,
opts: Omit<BundleOptions, 'fullOutput'> | (Omit<BundleOptions, 'fullOutput'> & { fullOutput: false }),
): Promise<string>;
export async function bundleRuleset(
input: string,
opts: Omit<BundleOptions, 'fullOutput'> & { fullOutput: true },
): Promise<OutputChunk>;
export async function bundleRuleset(
input: string,
{ target = 'browser', plugins, format, treeshake = false, fullOutput = false }: BundleOptions,
): Promise<string | OutputChunk> {
const bundle = await rollup({
input,
plugins: dedupeRollupPlugins(plugins),
treeshake,
watch: false,
perf: false,
onwarn(e, fn) {
if (e.code === 'MISSING_NAME_OPTION_FOR_IIFE_EXPORT') {
return;
}
fn(e);
},
external:
// the iife output is meant to be evaluated as a script type at the runtime, therefore it must not contain any import/exports, we must have the entire code ready to execute
target === 'runtime'
? []
: target === 'browser'
? id => isURL(id)
: (id, importer) =>
id.startsWith('node:') || (!isURL(id) && isPackageImport(id) && (importer === void 0 || !isURL(importer))),
});
const chunks = (await bundle.generate({ format: format ?? (target === 'runtime' ? 'iife' : 'esm'), exports: 'auto' }))
.output;
if (fullOutput) {
return chunks[0];
}
return chunks[0].code;
}