Skip to content

Commit 199b4b2

Browse files
committed
feat: babel runner support sourcemaps generation
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
1 parent 2f3c549 commit 199b4b2

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

@tunnckocore/jest-runner-babel/src/runner.js

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,47 @@ const explorer = cosmiconfig('jest-runner');
1010

1111
const isWin32 = os.platform() === 'win32';
1212

13+
/* eslint max-statements: ["error", 25] */
1314
export default async function jetRunnerBabel({ testPath, config }) {
1415
const start = new Date();
1516
let options = normalizeRunnerConfig(explorer.searchSync());
1617
const cfgs = [].concat(options.babel).filter(Boolean);
1718

1819
const testResults = await Promise.all(
19-
cfgs.map(({ config: cfg, ...opts }) => {
20+
cfgs.reduce((acc, { config: cfg, ...opts }) => {
2021
options = { ...options, ...opts };
22+
2123
let result = null;
22-
// index += 1;
2324

25+
const babelConfig = { ...cfg };
26+
27+
// sasa;
2428
try {
25-
result = transformFileSync(testPath, cfg);
29+
result = transformFileSync(testPath, babelConfig);
2630
} catch (err) {
27-
return fail({
28-
start,
29-
end: new Date(),
30-
test: { path: testPath, title: 'Babel', errorMessage: err.message },
31-
});
31+
return acc.concat(
32+
fail({
33+
start,
34+
end: new Date(),
35+
test: { path: testPath, title: 'Babel', errorMessage: err.message },
36+
}),
37+
);
3238
}
3339

3440
// Classics in the genre! Yes, it's possible, sometimes.
3541
// Old habit for ensurance
3642
if (!result) {
37-
return fail({
38-
start,
39-
end: new Date(),
40-
test: {
41-
path: testPath,
42-
title: 'Babel',
43-
errorMessage: `Babel runner fails...`,
44-
},
45-
});
43+
return acc.concat(
44+
fail({
45+
start,
46+
end: new Date(),
47+
test: {
48+
path: testPath,
49+
title: 'Babel',
50+
errorMessage: `Babel runner fails...`,
51+
},
52+
}),
53+
);
4654
}
4755

4856
let relativeTestPath = path.relative(config.rootDir, testPath);
@@ -53,15 +61,15 @@ export default async function jetRunnerBabel({ testPath, config }) {
5361

5462
// if not in monorepo, the `outs.dir` will be empty
5563
const outs = relativeTestPath.split('/').reduce(
56-
(acc, item, idx) => {
64+
(accumulator, item, idx) => {
5765
// only if we are in monorepo we want to get the first 2 items
5866
if (options.isMonorepo(config.cwd) && idx < 2) {
59-
return { ...acc, dir: acc.dir.concat(item) };
67+
return { ...accumulator, dir: accumulator.dir.concat(item) };
6068
}
6169

6270
return {
63-
...acc,
64-
file: acc.file
71+
...accumulator,
72+
file: accumulator.file
6573
.concat(item === options.srcDir ? null : item)
6674
.filter(Boolean),
6775
};
@@ -85,12 +93,32 @@ export default async function jetRunnerBabel({ testPath, config }) {
8593
fs.mkdirSync(outDir, { recursive: true });
8694
fs.writeFileSync(outFile, result.code);
8795

88-
return pass({
89-
start,
90-
end: new Date(),
91-
test: { path: outFile, title: 'Babel' },
92-
});
93-
}),
96+
if (result.map || babelConfig.sourceMaps === true) {
97+
const mapFile = `${outFile}.map`;
98+
fs.writeFileSync(mapFile, JSON.stringify(result.map));
99+
100+
return acc.concat(
101+
pass({
102+
start,
103+
end: new Date(),
104+
test: { path: outFile, title: 'Babel' },
105+
}),
106+
pass({
107+
start,
108+
end: new Date(),
109+
test: { path: mapFile, title: 'Babel' },
110+
}),
111+
);
112+
}
113+
114+
return acc.concat(
115+
pass({
116+
start,
117+
end: new Date(),
118+
test: { path: outFile, title: 'Babel' },
119+
}),
120+
);
121+
}, []),
94122
);
95123

96124
return testResults;

jest-runner.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
config: {
1919
presets: [['@tunnckocore/babel-preset', presetOptions]],
2020
comments: false,
21+
sourceMaps: 'both',
2122
},
2223
outDir: 'dist/main',
2324
},
@@ -27,6 +28,7 @@ module.exports = {
2728
['@tunnckocore/babel-preset', { ...presetOptions, modules: false }],
2829
],
2930
comments: false,
31+
sourceMaps: 'both',
3032
},
3133
outDir: 'dist/module',
3234
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"build": "jest -c jest.build.config.js",
2323
"release": "lerna version && lerna publish from-package",
2424
"cr": "node create-package.js",
25-
"resque": "lerna exec 'babel src -d dist/main --presets @tunnckocore' --stream"
25+
"resque": "lerna exec 'babel src -d dist/main --presets @tunnckocore --source-maps both' --stream"
2626
},
2727
"dependencies": {
2828
"@babel/cli": "^7.6.0",

0 commit comments

Comments
 (0)