storybook/scripts/utils/compile-babel.js

114 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-01-28 19:17:13 +02:00
/* eslint-disable no-console */
const fs = require('fs-extra');
const path = require('path');
2022-07-21 15:42:01 +02:00
const { join } = require('path');
function getCommand(watch, dir) {
2019-07-22 06:33:57 +00:00
// Compile angular with tsc
2022-05-12 09:31:48 +08:00
if (process.cwd().includes(path.join('frameworks', 'angular'))) {
2019-05-20 14:05:15 +02:00
return '';
}
2020-12-09 09:42:03 +01:00
if (process.cwd().includes(path.join('addons', 'storyshots'))) {
return '';
}
2019-05-20 14:05:15 +02:00
2019-03-23 23:09:01 +01:00
const args = [
join(process.cwd(), 'src'),
`--out-dir=${dir}`,
2022-07-21 15:42:01 +02:00
`--config-file=${join(__dirname, '..', '.babelrc.js')}`,
];
// babel copying over files it did not parse is a anti-pattern
// but in the case of the CLI, it houses generators are are templates
// moving all these is a lot of work. We should make different choices when we eventually refactor / rebuild the CLI
if (process.cwd().includes(path.join('lib', 'cli'))) {
args.push('--copy-files');
}
/*
* angular needs to be compiled with tsc; a compilation with babel is possible but throws
* runtime errors because of the the babel decorators plugin
* Only transpile .js and let tsc do the job for .ts files
*/
2019-05-20 14:05:15 +02:00
if (process.cwd().includes(path.join('addons', 'storyshots'))) {
args.push(`--extensions=".js"`);
} else {
args.push(`--extensions=.js,.jsx,.ts,.tsx`);
}
if (watch) {
args.push('-w');
}
return `yarn run -T babel ${args.join(' ')}`;
2018-01-29 00:57:51 +02:00
}
function handleExit(code, stderr, errorCallback) {
if (code !== 0) {
2018-01-28 19:09:40 +02:00
if (errorCallback && typeof errorCallback === 'function') {
errorCallback(stderr);
2018-01-28 19:09:40 +02:00
}
process.exit(code);
}
}
async function run({ watch, dir, silent, errorCallback }) {
const execa = await import('execa');
return new Promise((resolve, reject) => {
const command = getCommand(watch, dir);
if (command !== '') {
const child = execa.execaCommand(command, {
2022-07-21 15:42:01 +02:00
cwd: join(__dirname, '..'),
buffer: false,
env: { BABEL_MODE: path.basename(dir) },
});
let stderr = '';
if (watch) {
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
} else {
child.stderr.on('data', (data) => {
stderr += data.toString();
});
child.stdout.on('data', (data) => {
stderr += data.toString();
});
}
child.on('exit', (code) => {
resolve();
handleExit(code, stderr, errorCallback);
});
} else {
resolve();
}
});
}
async function babelify(options = {}) {
2021-05-17 18:09:33 +08:00
const { watch = false, silent = true, errorCallback } = options;
2018-01-29 00:57:51 +02:00
if (!(await fs.pathExists('src'))) {
2019-03-04 11:56:27 +01:00
if (!silent) {
console.log('No src dir');
}
2018-01-29 00:57:51 +02:00
return;
}
const runners = [
2022-07-21 15:42:01 +02:00
run({ watch, dir: join(process.cwd(), 'dist/cjs'), silent, errorCallback }),
run({ watch, dir: join(process.cwd(), 'dist/esm'), silent, errorCallback }),
];
2021-05-17 18:09:33 +08:00
await Promise.all(runners);
2018-01-29 00:57:51 +02:00
}
module.exports = {
babelify,
};