Skip to content

Commit 040774f

Browse files
committed
feat(jest-runner-rollup): better hooks
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
1 parent d5087df commit 040774f

File tree

6 files changed

+134
-81
lines changed

6 files changed

+134
-81
lines changed

jest-runner.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// const nodeResolve = require('rollup-plugin-node-resolve');
33
// const commonjs = require('rollup-plugin-commonjs');
44

5-
const { exec } = require('./@tunnckocore/execa');
5+
const esmLoader = require('esm');
6+
7+
const esmRequire = esmLoader(module);
8+
9+
const { exec } = esmRequire('./@tunnckocore/execa/src/index');
610

711
const presetOptions = {
812
react: true,

jest/bundle.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const { exts, workspaces } = utils.createAliases(ROOT);
77
module.exports = {
88
rootDir: ROOT,
99
displayName: 'bundle',
10-
testMatch: workspaces.map((ws) => `<rootDir>/${ws}/*/src/**/*`),
10+
// testMatch: ['<rootDir>/@tunnckocore/execa/src/index.js'],
11+
testMatch: workspaces.map(
12+
(ws) => `<rootDir>/${ws}/*/src/index.{${exts.join(',')}}`,
13+
),
1114
testPathIgnorePatterns: [
1215
/node_modules/.toString(),
1316
/(?:__)?(?:fixtures?|supports?|shared)(?:__)?/.toString(),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"prettier-plugin-sh": "^0.2.0",
4141
"react": "^16.10.1",
4242
"rollup-plugin-commonjs": "^10.1.0",
43+
"rollup-plugin-json": "^4.0.0",
4344
"rollup-plugin-node-resolve": "^5.2.0",
4445
"semver": "^6.3.0",
4546
"typescript": "^3.7.0-beta",

packages/jest-runner-rollup/src/runner.js

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ module.exports = async function jetRunnerRollup({ testPath, config }) {
2727
);
2828
if (inputFile.hasError) return inputFile.error;
2929

30-
const { pkgHook, formatHook, ...rollupConfig } = cfg;
31-
const hooks = {
32-
pkgHook: typeof pkgHook === 'function' ? pkgHook : () => {},
33-
formatHook: typeof formatHook === 'function' ? formatHook : (x) => x,
30+
const { hooks, ...rollupConfig } = cfg;
31+
const allHooks = {
32+
onPkg: typeof hooks.onPkg === 'function' ? hooks.onPkg : () => {},
33+
onFormat: typeof hooks.onFormat === 'function' ? hooks.onFormat : (x) => x,
34+
onWrite: typeof hooks.onWrite === 'function' ? hooks.onWrite : (x) => x,
3435
};
3536

3637
/** Rull that bundle */
@@ -58,48 +59,63 @@ module.exports = async function jetRunnerRollup({ testPath, config }) {
5859

5960
const outputFile = path.join(pkgRoot, dist, path.basename(opts.file));
6061

61-
return hooks.formatHook({
62+
return hooker(allHooks.onFormat, {
6263
outputOptions: { ...opts, dist, file: outputFile },
63-
testPath,
6464
pkgRoot,
65+
testPath,
66+
inputFile,
6567
});
6668
});
6769

70+
// console.log('all outputs:', await Promise.all(outputOptions));
6871
/** Write output file for each format */
6972
const res = await tryCatch(inputFile, start, () =>
7073
Promise.all(
71-
outputOptions.map(({ outputOptions: outOpts }) =>
72-
bundle
73-
.write(outOpts)
74-
/** If bundled without problems, print the output file filename */
75-
.then(async () =>
76-
pass({
77-
start,
78-
end: Date.now(),
79-
test: {
80-
path: outOpts.file,
81-
title: 'Rollup',
82-
},
83-
}),
84-
)
85-
.catch((err) => {
86-
/** If there is problem bundling, re-throw appending output filename */
87-
err.outputFile = outOpts.file;
88-
throw err;
89-
}),
90-
),
74+
outputOptions.map(async (ctx) => {
75+
const { outputOptions: outOpts } = await ctx;
76+
77+
const opts = await hooker(allHooks.onWrite, {
78+
outputOptions: outOpts,
79+
pkgRoot,
80+
testPath,
81+
inputFile,
82+
});
83+
84+
return (
85+
bundle
86+
.write(opts.outputOptions)
87+
/** If bundled without problems, print the output file filename */
88+
.then(async () =>
89+
pass({
90+
start,
91+
end: Date.now(),
92+
test: {
93+
path: opts.outputOptions.file,
94+
title: 'Rollup',
95+
},
96+
}),
97+
)
98+
.catch((err) => {
99+
/** If there is problem bundling, re-throw appending output filename */
100+
err.outputFile = opts.outputOptions.file;
101+
throw err;
102+
})
103+
);
104+
}),
91105
)
92106
/** Bundling process for each format completed successfuly */
93107
.then(async (testRes) => {
94-
await hooks.pkgHook({
108+
await hooker(allHooks.onPkg, {
109+
jestConfig: config,
95110
rollupConfig: {
96111
...rollupConfig,
97112
output: outputOptions,
98113
},
99114
pkgRoot,
100115
testPath,
101-
jestConfig: config,
116+
inputFile,
102117
});
118+
103119
return testRes;
104120
})
105121
/** Bundling for some of the formats failed */
@@ -216,3 +232,14 @@ function tryExtensions(filepath, config) {
216232

217233
return filepath + extension;
218234
}
235+
236+
async function hooker(hookFn, options = {}) {
237+
const hookResult = await hookFn({ ...options });
238+
const res = { ...hookResult };
239+
240+
if (res.outputOptions) {
241+
return { ...options, ...res };
242+
}
243+
244+
return { ...options, outputOptions: res };
245+
}

rollup.config.js

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const builtins = require('builtin-modules');
33
const nodeResolve = require('rollup-plugin-node-resolve');
44
const commonjs = require('rollup-plugin-commonjs');
5+
// const json = require('rollup-plugin-json');
56

67
// const fs = require('fs');
78
const path = require('path');
@@ -17,60 +18,65 @@ module.exports = Object.assign(exports.default, ___exportsWithoutDefault);
1718
`;
1819

1920
module.exports = {
20-
formatHook: (ctx) => {
21-
if (path.basename(ctx.testPath).includes('runner')) {
22-
const { file } = ctx.outputOptions;
23-
24-
ctx.outputOptions.file = path.join(path.dirname(file), 'runner.js');
25-
21+
hooks: {
22+
onFormat: (ctx) => {
23+
if (path.basename(ctx.testPath).includes('runner')) {
24+
const { file } = ctx.outputOptions;
25+
ctx.outputOptions.file = path.join(path.dirname(file), 'runner.js');
26+
return ctx;
27+
}
2628
return ctx;
27-
}
28-
29-
return ctx;
30-
},
31-
// pkgHook: ({ pkgRoot, output }) => {
32-
// const {
33-
// name,
34-
// version,
35-
// description,
36-
// license,
37-
// author,
38-
// engines,
39-
// keywords,
40-
// repository,
41-
// dependencies,
42-
// peerDependencies,
43-
// } = JSON.parse(fs.readFileSync(path.join(pkgRoot, 'package.json'), 'utf8'));
29+
},
30+
onWrite: ({ outputOptions, inputFile }) => {
31+
if (/cli\..+$/.test(inputFile)) {
32+
return { ...outputOptions, banner: '#!/usr/bin/env node\n' };
33+
}
34+
return { outputOptions };
35+
},
36+
// onPkg: ({ pkgRoot, output }) => {
37+
// const {
38+
// name,
39+
// version,
40+
// description,
41+
// license,
42+
// author,
43+
// engines,
44+
// keywords,
45+
// repository,
46+
// dependencies,
47+
// peerDependencies,
48+
// } = JSON.parse(fs.readFileSync(path.join(pkgRoot, 'package.json'), 'utf8'));
4449

45-
// const pkg = {
46-
// name,
47-
// version,
48-
// description,
49-
// license,
50-
// author,
51-
// engines,
52-
// keywords,
53-
// repository,
54-
// dependencies,
55-
// peerDependencies,
56-
// files: output.map(({ format }) => format),
57-
// main: output.find(({ format }) => format === 'cjs').file,
58-
// module: output.find(({ format }) => format === 'esm').file,
59-
// typings: 'dist/types/index.d.ts',
60-
// };
50+
// const pkg = {
51+
// name,
52+
// version,
53+
// description,
54+
// license,
55+
// author,
56+
// engines,
57+
// keywords,
58+
// repository,
59+
// dependencies,
60+
// peerDependencies,
61+
// files: output.map(({ format }) => format),
62+
// main: output.find(({ format }) => format === 'cjs').file,
63+
// module: output.find(({ format }) => format === 'esm').file,
64+
// typings: 'dist/types/index.d.ts',
65+
// };
6166

62-
// const pkgJson = Object.keys(pkg).reduce((acc, key) => {
63-
// if (pkg[key]) {
64-
// acc[key] = pkg[key];
65-
// }
66-
// return acc;
67-
// }, {});
67+
// const pkgJson = Object.keys(pkg).reduce((acc, key) => {
68+
// if (pkg[key]) {
69+
// acc[key] = pkg[key];
70+
// }
71+
// return acc;
72+
// }, {});
6873

69-
// fs.writeFileSync(
70-
// path.join(pkgRoot, 'dist', 'package.json'),
71-
// JSON.stringify(pkgJson, null, 2),
72-
// );
73-
// },
74+
// fs.writeFileSync(
75+
// path.join(pkgRoot, 'dist', 'package.json'),
76+
// JSON.stringify(pkgJson, null, 2),
77+
// );
78+
// },
79+
},
7480

7581
external: builtins,
7682
inlineDynamicImports: true,
@@ -83,6 +89,11 @@ module.exports = {
8389
commonjs({
8490
// extensions,
8591
}),
92+
// json({
93+
// compact: true,
94+
// preferConst: true,
95+
// include: 'node_modules/**',
96+
// }),
8697
],
8798
output: [
8899
{

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12141,6 +12141,13 @@ rollup-plugin-commonjs@^10.1.0:
1214112141
resolve "^1.11.0"
1214212142
rollup-pluginutils "^2.8.1"
1214312143

12144+
rollup-plugin-json@^4.0.0:
12145+
version "4.0.0"
12146+
resolved "https://registry.yarnpkg.com/rollup-plugin-json/-/rollup-plugin-json-4.0.0.tgz#a18da0a4b30bf5ca1ee76ddb1422afbb84ae2b9e"
12147+
integrity sha512-hgb8N7Cgfw5SZAkb3jf0QXii6QX/FOkiIq2M7BAQIEydjHvTyxXHQiIzZaTFgx1GK0cRCHOCBHIyEkkLdWKxow==
12148+
dependencies:
12149+
rollup-pluginutils "^2.5.0"
12150+
1214412151
rollup-plugin-node-resolve@^5.2.0:
1214512152
version "5.2.0"
1214612153
resolved "https://registry.yarnpkg.com/rollup-plugin-node-resolve/-/rollup-plugin-node-resolve-5.2.0.tgz#730f93d10ed202473b1fb54a5997a7db8c6d8523"
@@ -12152,7 +12159,7 @@ rollup-plugin-node-resolve@^5.2.0:
1215212159
resolve "^1.11.1"
1215312160
rollup-pluginutils "^2.8.1"
1215412161

12155-
rollup-pluginutils@^2.8.1:
12162+
rollup-pluginutils@^2.5.0, rollup-pluginutils@^2.8.1:
1215612163
version "2.8.2"
1215712164
resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e"
1215812165
integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ==

0 commit comments

Comments
 (0)