From 5b455542fec82fa9edfb41c0da0ddceb4e72c485 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Fri, 26 Jul 2024 12:33:21 +0200 Subject: [PATCH] fix: always create package.json with type regardless of esm option (#598) This will provide more flexibilty to library authors such as specifying `type: module` in their project's `package.json` without breaking the commonjs output for the published code. --- docs/pages/esm.md | 7 ++----- packages/react-native-builder-bob/src/utils/compile.ts | 9 +++------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/docs/pages/esm.md b/docs/pages/esm.md index 2c02ea8c7..23c65acb3 100644 --- a/docs/pages/esm.md +++ b/docs/pages/esm.md @@ -16,12 +16,9 @@ You can verify whether ESM support is enabled by checking the configuration for } ``` -The `"esm": true` option enables ESM-compatible output. Here's what it does: +The `"esm": true` option enables ESM-compatible output by adding the `.js` extension to the import statements in the generated files. -- It adds the `.js` extension to the import statements in the generated files. -- It creates a `package.json` file in the output directory with the content: `{ "type": "module" }` - -In addition, it's necessary to specify `"moduleResolution": "Bundler"` in your `tsconfig.json` file: +It's recommended to specify `"moduleResolution": "Bundler"` in your `tsconfig.json` file as well: ```json { diff --git a/packages/react-native-builder-bob/src/utils/compile.ts b/packages/react-native-builder-bob/src/utils/compile.ts index d1de94261..a3401fc1e 100644 --- a/packages/react-native-builder-bob/src/utils/compile.ts +++ b/packages/react-native-builder-bob/src/utils/compile.ts @@ -68,12 +68,9 @@ export default async function compile({ } await fs.mkdirp(output); - - if (esm) { - await fs.writeJSON(path.join(output, 'package.json'), { - type: modules === 'commonjs' ? 'commonjs' : 'module', - }); - } + await fs.writeJSON(path.join(output, 'package.json'), { + type: modules === 'commonjs' ? 'commonjs' : 'module', + }); await Promise.all( files.map(async (filepath) => {