Skip to content

Commit a31d34c

Browse files
authored
feat(ruleset-bundler): add fullOutput option to bundleRuleset (stoplightio#2194)
* feat(ruleset-bundler): add fullOutput option to bundleRuleset * chore(ruleset-bundler): use OutputChunk
1 parent 6f720e0 commit a31d34c

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

packages/ruleset-bundler/src/__tests__/index.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,79 @@ export { spectral as default };
309309
`);
310310
});
311311
});
312+
313+
it('given fullOutput set to true, should expose the entire built chunk', async () => {
314+
serveAssets({
315+
'/p/.spectral/my-fn.mjs': `import {isOdd} from './helpers/index.mjs';
316+
317+
export default (input) => {
318+
if (!isOdd(input)) {
319+
return [{ message: 'must be odd' }];
320+
}
321+
};`,
322+
323+
'https://cdn.skypack.dev/lodash.uppercase': `export * from '/-/[email protected]/dist=es2020,mode=imports/optimized/lodash.uppercase.js';
324+
export {default} from '/-/[email protected]/dist=es2020,mode=imports/optimized/lodash.uppercase.js';`,
325+
326+
'https://cdn.skypack.dev/-/[email protected]/dist=es2020,mode=imports/optimized/lodash.uppercase.js':
327+
'export default (a) => a.toUpperCase()',
328+
329+
'/p/.spectral/upper-case.mjs': `import upperCase from 'https://cdn.skypack.dev/lodash.uppercase';
330+
331+
export default (input) => {
332+
if (upperCase(input) !== input) {
333+
return [{ message: 'must be upper case' }];
334+
}
335+
};`,
336+
337+
'/p/.spectral/lower-case.mjs': `import lowerCase from 'lodash.lowercase';
338+
339+
export default (input) => {
340+
if (lowerCase(input) !== input) {
341+
return [{ message: 'must be lower case' }];
342+
}
343+
};`,
344+
345+
'/p/.spectral/helpers/index.mjs': `export * from './is-odd.mjs';`,
346+
'/p/.spectral/helpers/is-odd.mjs': `export const isOdd = (value) => value % 2 === 1`,
347+
348+
'/p/spectral.mjs': `import myFn from './.spectral/my-fn.mjs';
349+
import lowerCase from './.spectral/lower-case.mjs';
350+
import upperCase from './.spectral/upper-case.mjs';
351+
352+
export default {
353+
rules: {
354+
'odd-rule': {
355+
given: '$',
356+
then: { function: myFn },
357+
},
358+
'upper-case-rule': {
359+
given: '$',
360+
then: { function: upperCase },
361+
},
362+
'lower-case-rule': {
363+
given: '$',
364+
then: { function: lowerCase },
365+
},
366+
},
367+
};`,
368+
});
369+
370+
const bundle = await bundleRuleset('/p/spectral.mjs', {
371+
target: 'node',
372+
plugins: [...node(io), virtualFs(io)],
373+
fullOutput: true,
374+
});
375+
376+
expect(Object.keys(bundle.modules).sort()).toStrictEqual([
377+
'/p/.spectral/helpers/index.mjs',
378+
'/p/.spectral/helpers/is-odd.mjs',
379+
'/p/.spectral/lower-case.mjs',
380+
'/p/.spectral/my-fn.mjs',
381+
'/p/.spectral/upper-case.mjs',
382+
'/p/spectral.mjs',
383+
'https://cdn.skypack.dev/-/[email protected]/dist=es2020,mode=imports/optimized/lodash.uppercase.js',
384+
'https://cdn.skypack.dev/lodash.uppercase',
385+
]);
386+
});
312387
});

packages/ruleset-bundler/src/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { rollup, Plugin } from 'rollup';
1+
import { rollup, Plugin, OutputChunk } from 'rollup';
22
import { isURL } from '@stoplight/path';
33
import { isPackageImport } from './utils/isPackageImport';
44
import { dedupeRollupPlugins } from './utils/dedupeRollupPlugins';
@@ -8,14 +8,23 @@ export type BundleOptions = {
88
target: 'node' | 'browser' | 'runtime';
99
format?: 'esm' | 'commonjs' | 'iife';
1010
treeshake?: boolean; // false by default
11+
fullOutput?: boolean;
1112
};
1213

1314
export { IO } from './types';
1415

1516
export async function bundleRuleset(
1617
input: string,
17-
{ target = 'browser', plugins, format, treeshake = false }: BundleOptions,
18-
): Promise<string> {
18+
opts: Omit<BundleOptions, 'fullOutput'> | (Omit<BundleOptions, 'fullOutput'> & { fullOutput: false }),
19+
): Promise<string>;
20+
export async function bundleRuleset(
21+
input: string,
22+
opts: Omit<BundleOptions, 'fullOutput'> & { fullOutput: true },
23+
): Promise<OutputChunk>;
24+
export async function bundleRuleset(
25+
input: string,
26+
{ target = 'browser', plugins, format, treeshake = false, fullOutput = false }: BundleOptions,
27+
): Promise<string | OutputChunk> {
1928
const bundle = await rollup({
2029
input,
2130
plugins: dedupeRollupPlugins(plugins),
@@ -39,6 +48,12 @@ export async function bundleRuleset(
3948
id.startsWith('node:') || (!isURL(id) && isPackageImport(id) && (importer === void 0 || !isURL(importer))),
4049
});
4150

42-
return (await bundle.generate({ format: format ?? (target === 'runtime' ? 'iife' : 'esm'), exports: 'auto' }))
43-
.output[0].code;
51+
const chunks = (await bundle.generate({ format: format ?? (target === 'runtime' ? 'iife' : 'esm'), exports: 'auto' }))
52+
.output;
53+
54+
if (fullOutput) {
55+
return chunks[0];
56+
}
57+
58+
return chunks[0].code;
4459
}

0 commit comments

Comments
 (0)