Skip to content

Commit 0c5582b

Browse files
committed
fix: use an alternative approach to support ESM
1 parent 376c5bf commit 0c5582b

File tree

6 files changed

+22
-20
lines changed

6 files changed

+22
-20
lines changed

docs/pages/build.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ yarn add --dev react-native-builder-bob
7474

7575
```json
7676
"source": "./src/index.tsx",
77-
"main": "./lib/commonjs/index.cjs",
78-
"module": "./lib/module/index.mjs",
77+
"main": "./lib/commonjs/index.js",
78+
"module": "./lib/module/index.js",
7979
"types": "./lib/typescript/src/index.d.ts",
8080
"exports": {
8181
".": {
8282
"types": "./typescript/src/index.d.ts",
83-
"import": "./module/index.mjs",
84-
"require": "./commonjs/index.cjs"
83+
"import": "./module/index.js",
84+
"require": "./commonjs/index.js"
8585
}
8686
},
8787
"files": [
@@ -101,8 +101,6 @@ yarn add --dev react-native-builder-bob
101101

102102
Make sure to change specify correct files according to the targets you have enabled.
103103

104-
> The `exports` field also requires the `esm` option to be enabled for the [`commonjs`](#commonjs) and [`module`](#module) targets. In addition, the file extensions of the generated files will be `.js` instead of `.cjs` and `.mjs` if the `esm` option is not enabled.
105-
106104
> If you're building TypeScript definition files, also make sure that the `types` field points to a correct path. Depending on the project configuration, the path can be different for you than the example snippet (e.g. `lib/typescript/index.d.ts` if you have only the `src` directory and `rootDir` is not set).
107105
108106
1. Add the output directory to `.gitignore` and `.eslintignore`
@@ -168,7 +166,7 @@ In addition, the following options are supported:
168166

169167
Setting this option to `true` will output ES modules compatible code for Node.js 12+, modern browsers and other tools that support `package.json`'s `exports` field.
170168

171-
This mainly adds file extensions to the imports and exports. Note that file extensions are not added when importing a file that may have platform-specific extensions (e.g. `.android.ts`) to avoid breaking tools. In addition, the generated files will have `.cjs` (CommonJS) and `.mjs` (ES modules) extensions instead of `.js` - this is necessary to disambiguate between the two formats.
169+
This mainly adds file extensions to the imports and exports. Note that file extensions are not added when importing a file that may have platform-specific extensions (e.g. `.android.ts`) to avoid breaking tools.
172170

173171
If you use TypeScript, also make sure to set `"moduleResolution": "Bundler"` in your `tsconfig.json` file.
174172

packages/create-react-native-library/templates/common/$package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"version": "0.1.0",
44
"description": "<%- project.description %>",
55
"source": "./src/index.tsx",
6-
"main": "./lib/commonjs/index.cjs",
7-
"module": "./lib/module/index.mjs",
6+
"main": "./lib/commonjs/index.js",
7+
"module": "./lib/module/index.js",
88
"types": "./lib/typescript/src/index.d.ts",
99
"exports": {
1010
".": {
1111
"types": "./lib/typescript/src/index.d.ts",
12-
"import": "./lib/module/index.mjs",
13-
"require": "./lib/commonjs/index.cjs"
12+
"import": "./lib/module/index.js",
13+
"require": "./lib/commonjs/index.js"
1414
}
1515
},
1616
"files": [

packages/react-native-builder-bob/babel-preset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = function (api, options, cwd) {
4747
[
4848
require.resolve('./lib/babel'),
4949
{
50-
extension: options.esm ? (cjs ? 'cjs' : 'mjs') : undefined,
50+
extension: options.esm ? 'js' : undefined,
5151
},
5252
],
5353
],

packages/react-native-builder-bob/src/babel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Options = {
1414
* NodeJS requires explicit extension for esm
1515
* The `cjs` extension avoids disambiguity when package.json has "type": "module"
1616
*/
17-
extension?: 'cjs' | 'mjs';
17+
extension?: 'js' | 'cjs' | 'mjs';
1818
/**
1919
* Out of tree platforms to support
2020
* For `import './file'`, we skip adding extension if `file.${platform}.ts` exists

packages/react-native-builder-bob/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ yargs
159159
esm = true;
160160

161161
if (targets.includes('commonjs')) {
162-
entries.main = `./${path.join(output, 'commonjs', 'index.cjs')}`;
162+
entries.main = `./${path.join(output, 'commonjs', 'index.js')}`;
163163
}
164164

165-
entries.module = `./${path.join(output, 'module', 'index.mjs')}`;
165+
entries.module = `./${path.join(output, 'module', 'index.js')}`;
166166
} else if (targets.includes('commonjs')) {
167167
entries.main = `./${path.join(output, 'commonjs', 'index.js')}`;
168168
} else {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@ export default async function compile({
6565
}
6666
}
6767

68-
const outputExtension = esm
69-
? modules === 'commonjs'
70-
? '.cjs'
71-
: '.mjs'
72-
: '.js';
68+
await fs.mkdirp(output);
69+
70+
if (esm) {
71+
await fs.writeJSON(path.join(output, 'package.json'), {
72+
type: modules === 'commonjs' ? 'commonjs' : 'module',
73+
});
74+
}
75+
76+
const outputExtension = '.js';
7377

7478
await Promise.all(
7579
files.map(async (filepath) => {

0 commit comments

Comments
 (0)