Converted @rollup/html to a standalone package (out of the original monorepo) and made it ESM-native
This commit is contained in:
45
test/util/test.d.ts
vendored
Normal file
45
test/util/test.d.ts
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
/* eslint-disable import/no-extraneous-dependencies */
|
||||
import type { RollupBuild, OutputOptions, OutputChunk, OutputAsset } from 'rollup';
|
||||
import type { Assertions } from 'ava';
|
||||
|
||||
interface GetCode {
|
||||
(bundle: RollupBuild, outputOptions?: OutputOptions | null, allFiles?: false): Promise<string>;
|
||||
(bundle: RollupBuild, outputOptions: OutputOptions | null | undefined, allFiles: true): Promise<
|
||||
Array<{
|
||||
code: OutputChunk['code'] | undefined;
|
||||
fileName: OutputChunk['fileName'] | OutputAsset['fileName'];
|
||||
source: OutputAsset['source'] | undefined;
|
||||
}>
|
||||
>;
|
||||
}
|
||||
|
||||
export const getCode: GetCode;
|
||||
|
||||
export function getFiles(
|
||||
bundle: RollupBuild,
|
||||
outputOptions?: OutputOptions
|
||||
): Promise<
|
||||
{
|
||||
fileName: string;
|
||||
content: any;
|
||||
}[]
|
||||
>;
|
||||
|
||||
export function evaluateBundle(bundle: RollupBuild): Promise<Pick<NodeModule, 'exports'>>;
|
||||
|
||||
export function getImports(bundle: RollupBuild): Promise<string[]>;
|
||||
|
||||
export function getResolvedModules(bundle: RollupBuild): Promise<Record<string, string>>;
|
||||
|
||||
export function onwarn(warning: string | any): void;
|
||||
|
||||
export function testBundle(
|
||||
t: Assertions,
|
||||
bundle: RollupBuild,
|
||||
options: { inject: Record<string, any>; options: Record<string, any> }
|
||||
): Promise<{
|
||||
code: string;
|
||||
error?: any;
|
||||
result?: any;
|
||||
module: Pick<NodeModule, 'exports'>;
|
||||
}>;
|
||||
95
test/util/test.js
Normal file
95
test/util/test.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import path from "node:path";
|
||||
import process from "node:process";
|
||||
|
||||
/**
|
||||
* @param {import('rollup').RollupBuild} bundle
|
||||
* @param {import('rollup').OutputOptions} [outputOptions]
|
||||
*/
|
||||
export const getCode = async (bundle, outputOptions, allFiles = false) => {
|
||||
const { output } = await bundle.generate(outputOptions || { format: 'cjs', exports: 'auto' });
|
||||
|
||||
if (allFiles) {
|
||||
return output.map(({ code, fileName, source, map }) => {
|
||||
return {
|
||||
code,
|
||||
fileName,
|
||||
source,
|
||||
map
|
||||
};
|
||||
});
|
||||
}
|
||||
const [{ code }] = output;
|
||||
return code;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {import('rollup').RollupBuild} bundle
|
||||
* @param {import('rollup').OutputOptions} [outputOptions]
|
||||
*/
|
||||
export const getFiles = async (bundle, outputOptions) => {
|
||||
if (!outputOptions.dir && !outputOptions.file)
|
||||
throw new Error('You must specify "output.file" or "output.dir" for the build.');
|
||||
|
||||
const { output } = await bundle.generate(outputOptions || { format: 'cjs', exports: 'auto' });
|
||||
|
||||
return output.map(({ code, fileName, source }) => {
|
||||
const absPath = path.resolve(outputOptions.dir || path.dirname(outputOptions.file), fileName);
|
||||
return {
|
||||
fileName: path.relative(process.cwd(), absPath).split(path.sep).join('/'),
|
||||
content: code || source
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
export const getImports = async (bundle) => {
|
||||
if (bundle.imports) {
|
||||
return bundle.imports;
|
||||
}
|
||||
const { output } = await bundle.generate({ format: 'es' });
|
||||
const [{ imports }] = output;
|
||||
return imports;
|
||||
};
|
||||
|
||||
export const getResolvedModules = async (bundle) => {
|
||||
const {
|
||||
output: [{ modules }]
|
||||
} = await bundle.generate({ format: 'es' });
|
||||
return modules;
|
||||
};
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
export const onwarn = (warning) => console.warn(warning.toString());
|
||||
|
||||
/**
|
||||
* @param {import('ava').Assertions} t
|
||||
* @param {import('rollup').RollupBuild} bundle
|
||||
* @param {object} args
|
||||
*/
|
||||
export const testBundle = async (t, bundle, { inject = {}, options = {} } = {}) => {
|
||||
const { output } = await bundle.generate({ format: 'cjs', exports: 'auto', ...options });
|
||||
const [{ code }] = output;
|
||||
const module = { exports: {} };
|
||||
// as of 1/2/2020 Github Actions + Windows has changed in a way that we must now escape backslashes
|
||||
const cwd = process.cwd().replace(/\\/g, '\\\\');
|
||||
const params = ['module', 'exports', 'require', 't', ...Object.keys(inject)].concat(
|
||||
`process.chdir('${cwd}'); let result;\n\n${code}\n\nreturn result;`
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-new-func
|
||||
const func = new Function(...params);
|
||||
let error;
|
||||
let result;
|
||||
|
||||
try {
|
||||
result = func(...[module, module.exports, require, t, ...Object.values(inject)]);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
return { code, error, module, result };
|
||||
};
|
||||
|
||||
export const evaluateBundle = async (bundle) => {
|
||||
const { module } = await testBundle(null, bundle);
|
||||
return module.exports;
|
||||
};
|
||||
Reference in New Issue
Block a user