Skip to content

Commit d522793

Browse files
authored
feat: support mts, cts, mjs and cts files in source code (#595)
### Summary currently these files get treated as assets and are copied over as is. however this is not the correct behavior. this changes them to be trated as source code. in addition, the module syntax in .mts or .mjs files is preserved as is. ### Test plan tested in a generated project by creating `.mts` and `.cts` files.
1 parent 2a62bed commit d522793

File tree

1 file changed

+27
-17
lines changed
  • packages/react-native-builder-bob/src/utils

1 file changed

+27
-17
lines changed

packages/react-native-builder-bob/src/utils/compile.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type Options = Input & {
1515
exclude: string;
1616
};
1717

18+
const sourceExt = /\.([cm])?[jt]sx?$/;
19+
1820
export default async function compile({
1921
root,
2022
source,
@@ -73,17 +75,15 @@ export default async function compile({
7375
});
7476
}
7577

76-
const outputExtension = '.js';
77-
7878
await Promise.all(
7979
files.map(async (filepath) => {
8080
const outputFilename = path
8181
.join(output, path.relative(source, filepath))
82-
.replace(/\.(jsx?|tsx?)$/, outputExtension);
82+
.replace(sourceExt, '.$1js');
8383

8484
await fs.mkdirp(path.dirname(outputFilename));
8585

86-
if (!/\.(jsx?|tsx?)$/.test(filepath)) {
86+
if (!sourceExt.test(filepath)) {
8787
// Copy files which aren't source code
8888
fs.copy(filepath, outputFilename);
8989
return;
@@ -102,7 +102,15 @@ export default async function compile({
102102
? null
103103
: {
104104
presets: [
105-
[require.resolve('../../babel-preset'), { modules, esm }],
105+
[
106+
require.resolve('../../babel-preset'),
107+
{
108+
modules:
109+
// If a file is explicitly marked as ESM, then preserve the syntax
110+
/\.m[jt]s$/.test(filepath) ? 'preserve' : modules,
111+
esm,
112+
},
113+
],
106114
],
107115
}),
108116
});
@@ -136,18 +144,20 @@ export default async function compile({
136144

137145
const getGeneratedEntryPath = async () => {
138146
if (pkg.source) {
139-
const indexName =
140-
path.basename(pkg.source).replace(/\.(jsx?|tsx?)$/, '') +
141-
outputExtension;
142-
143-
const potentialPath = path.join(
144-
output,
145-
path.dirname(path.relative(source, path.join(root, pkg.source))),
146-
indexName
147-
);
148-
149-
if (await fs.pathExists(potentialPath)) {
150-
return path.relative(root, potentialPath);
147+
for (const ext of ['.js', '.cjs', '.mjs']) {
148+
const indexName =
149+
// The source field may not have an extension, so we add it instead of replacing directly
150+
path.basename(pkg.source).replace(sourceExt, '') + ext;
151+
152+
const potentialPath = path.join(
153+
output,
154+
path.dirname(path.relative(source, path.join(root, pkg.source))),
155+
indexName
156+
);
157+
158+
if (await fs.pathExists(potentialPath)) {
159+
return path.relative(root, potentialPath);
160+
}
151161
}
152162
}
153163

0 commit comments

Comments
 (0)