Debian Codegraph @

new/util/test.js

raw
1 const path = require('path');
2 const process = require('process');
3
4 /**
5 * @param {import('rollup').RollupBuild} bundle
6 * @param {import('rollup').OutputOptions} [outputOptions]
7 */
8 const getCode = async (bundle, outputOptions, allFiles = false) => {
9 const { output } = await bundle.generate(outputOptions || { format: 'cjs', exports: 'auto' });
10
11 if (allFiles) {
12 return output.map(({ code, fileName, source, map }) => {
13 return {
14 code,
15 fileName,
16 source,
17 map
18 };
19 });
20 }
21 const [{ code }] = output;
22 return code;
23 };
24
25 /**
26 * @param {import('rollup').RollupBuild} bundle
27 * @param {import('rollup').OutputOptions} [outputOptions]
28 */
29 const getFiles = async (bundle, outputOptions) => {
30 if (!outputOptions.dir && !outputOptions.file)
31 throw new Error('You must specify "output.file" or "output.dir" for the build.');
32
33 const { output } = await bundle.generate(outputOptions || { format: 'cjs', exports: 'auto' });
34
35 return output.map(({ code, fileName, source }) => {
36 const absPath = path.resolve(outputOptions.dir || path.dirname(outputOptions.file), fileName);
37 return {
38 fileName: path.relative(process.cwd(), absPath).split(path.sep).join('/'),
39 content: code || source
40 };
41 });
42 };
43
44 const getImports = async (bundle) => {
45 if (bundle.imports) {
46 return bundle.imports;
47 }
48 const { output } = await bundle.generate({ format: 'es' });
49 const [{ imports }] = output;
50 return imports;
51 };
52
53 const getResolvedModules = async (bundle) => {
54 const {
55 output: [{ modules }]
56 } = await bundle.generate({ format: 'es' });
57 return modules;
58 };
59
60 // eslint-disable-next-line no-console
61 const onwarn = (warning) => console.warn(warning.toString());
62
63 /**
64 * @param {import('ava').Assertions} t
65 * @param {import('rollup').RollupBuild} bundle
66 * @param {object} args
67 */
68 const testBundle = async (t, bundle, { inject = {}, options = {} } = {}) => {
69 const { output } = await bundle.generate({ format: 'cjs', exports: 'auto', ...options });
70 const [{ code }] = output;
71 const module = { exports: {} };
72 // as of 1/2/2020 Github Actions + Windows has changed in a way that we must now escape backslashes
73 const cwd = process.cwd().replace(/\\/g, '\\\\');
74 const params = ['module', 'exports', 'require', 't', ...Object.keys(inject)].concat(
75 `process.chdir('${cwd}'); let result;\n\n${code}\n\nreturn result;`
76 );
77
78 // eslint-disable-next-line no-new-func
79 const func = new Function(...params);
80 let error;
81 let result;
82
83 try {
84 result = func(...[module, module.exports, require, t, ...Object.values(inject)]);
85 } catch (e) {
86 error = e;
87 }
88
89 return { code, error, module, result };
90 };
91
92 const evaluateBundle = async (bundle) => {
93 const { module } = await testBundle(null, bundle);
94 return module.exports;
95 };
96
97 module.exports = {
98 evaluateBundle,
99 getCode,
100 getFiles,
101 getImports,
102 getResolvedModules,
103 onwarn,
104 testBundle
105 };