Skip to content
95 changes: 61 additions & 34 deletions apps/react-sdk/src/transpiler/transpiler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Path from 'path';
import log from 'loglevel';

import { rollup } from 'rollup';
import { rollup, RollupWarning } from 'rollup';
import babel from '@rollup/plugin-babel';

import { getStatsInDir } from '../utils';
Expand All @@ -17,48 +18,74 @@ const ROOT_DIR = Path.resolve(__dirname, '../..');
*/
export async function transpileFiles(directory: string, outputDir: string, options?: TranspileFilesOptions) {
const { files, dirs } = await getStatsInDir(directory);
if (files.length) {
const warnings: RollupWarning[] = [];
const debug = process.argv.includes('--debug');
const originalStderr = process.stderr.write;

process.stderr.write = ((chunk: any, enc?: any, cb?: any): boolean => {
const msg = chunk.toString();

if (msg.includes('[BABEL]')) {
if (debug) {
log.debug(msg.trim());
}
if (typeof cb === 'function') cb();
return true;
}
return originalStderr.call(process.stderr, chunk, enc, cb);
}) as any;

try {
if (files.length) {
/**
* WHEN ADDING PLUGINS to transform the input keep in mind that
* IF any of these changes the actual original content in some way
* the output is not able to produce accurate source map to the original file.
*
* An example of this is using the `sourceMaps: 'inline'` configuration for the babel plugin.
*/
const bundles = await rollup({
input: files,
onwarn: () => {},
plugins: [
babel({
cwd: ROOT_DIR,
babelHelpers: "bundled",
plugins: [
"source-map-support",
],
presets: [
["@babel/preset-env", {
targets: { node: "12.16" },
}],
["@babel/preset-react", {
runtime: "automatic",
}],
],
})
],
})
await bundles.write({
format: "commonjs",
sourcemap: true,
dir: outputDir,
exports: "auto",
paths: {
'react/jsx-runtime': require.resolve('react/cjs/react-jsx-runtime.production.min').replace(/\\/g, '/'),
},
sanitizeFileName: false,
})
const bundles = await rollup({
input: files,
onwarn: (warning: RollupWarning) =>{
warnings.push(warning);
},
plugins: [
babel({
cwd: ROOT_DIR,
babelHelpers: "bundled",
plugins: [
"source-map-support",
],
presets: [
["@babel/preset-env", {
targets: { node: "12.16" },
}],
["@babel/preset-react", {
runtime: "automatic",
}],
],
})
],
})
await bundles.write({
format: "commonjs",
sourcemap: true,
dir: outputDir,
exports: "auto",
paths: {
'react/jsx-runtime': require.resolve('react/cjs/react-jsx-runtime.production.min').replace(/\\/g, '/'),
},
sanitizeFileName: false,
});
}
} finally {
process.stderr.write = originalStderr;
}

if (debug && warnings.length > 0) {
warnings.forEach(w => log.debug(w.message));
}

// Check if we should transpile all subdirs
if (options?.recursive === true && dirs.length > 0) {
for (const subdir of dirs) {
const subdirPath = Path.parse(subdir);
Expand Down