Skip to content

Commit

Permalink
Copy *.cjs bundles to *.cjs.native.js so React Native can find them.
Browse files Browse the repository at this point in the history
May help with issue #9437, by making the suggested metro.config.js
workaround unnecessary.
  • Loading branch information
benjamn committed May 13, 2022
1 parent 6f71000 commit 0b5cc42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export function eachFile(dir: string, callback: (
if (relPath.startsWith("../")) return;

// Avoid re-transforming CommonJS bundle files.
if (relPath.endsWith(".cjs.js")) return;
if (relPath.endsWith(".cjs")) return;
if (relPath.endsWith(".cjs.js")) return;
if (relPath.endsWith(".cjs.native.js")) return;

// Avoid re-transforming CommonJS bundle files.
if (relPath.endsWith(".min.js")) return;
Expand Down
21 changes: 18 additions & 3 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';
import { promises as fs } from "fs";

import nodeResolve from '@rollup/plugin-node-resolve';
import { terser as minify } from 'rollup-plugin-terser';
import path from 'path';

const entryPoints = require('./entryPoints');
const distDir = './dist';
Expand Down Expand Up @@ -102,20 +104,33 @@ function prepareBundle({
extensions,
}) {
const dir = path.join(distDir, ...dirs);
const inputFile = `${dir}/index.js`;
const outputFile = `${dir}/${bundleName}.cjs`;

return {
input: `${dir}/index.js`,
input: inputFile,
external(id, parentId) {
return isExternal(id, parentId, true);
},
output: {
file: `${dir}/${bundleName}.cjs`,
file: outputFile,
format: 'cjs',
sourcemap: true,
exports: 'named',
externalLiveBindings: false,
},
plugins: [
extensions ? nodeResolve({ extensions }) : nodeResolve(),
{
name: "copy *.cjs to *.cjs.native.js",
async writeBundle({ file }) {
const buffer = await fs.readFile(file);
await fs.writeFile(
file + ".native.js",
buffer,
);
},
},
],
};
}
Expand Down

0 comments on commit 0b5cc42

Please sign in to comment.