oblador / react-native-esbuild

Fast bundler and dev server for react-native using esbuild
MIT License
595 stars 11 forks source link

Don't work with esbuild 0.17+ (with patch) #27

Closed vovkasm closed 1 year ago

vovkasm commented 1 year ago

Hi! esbuild has changed interface for watch/serve/incremental stuff, also some other changes.

My patch to make it work with 0.17+ (but it contains also additional option to create metafile for analyzing with Bundle Size Analyzer)

diff --git a/node_modules/react-native-esbuild/src/commands/bundle.js b/node_modules/react-native-esbuild/src/commands/bundle.js
index 40e3168..ef0f0f8 100644
--- a/node_modules/react-native-esbuild/src/commands/bundle.js
+++ b/node_modules/react-native-esbuild/src/commands/bundle.js
@@ -1,4 +1,5 @@
 const esbuild = require('esbuild');
+const fs = require('fs/promises');
 const { emptyDefaultCacheDir } = require('../cache');

 module.exports = (getBundleConfig) => async (_, config, args) => {
@@ -6,11 +7,13 @@ module.exports = (getBundleConfig) => async (_, config, args) => {
     emptyDefaultCacheDir();
   }
   const esbuildConfig = getBundleConfig(config, args);
-  await esbuild.build({
+  const result = await esbuild.build({
     ...esbuildConfig,
     bundle: true,
-    watch: false,
-    incremental: false,
+    metafile: Boolean(args.writeMetafile),
     write: true,
   });
+  if (result.metafile) {
+    await fs.writeFile(args.writeMetafile, JSON.stringify(result.metafile), {encoding: 'utf8'})
+  }
 };
diff --git a/node_modules/react-native-esbuild/src/commands/index.js b/node_modules/react-native-esbuild/src/commands/index.js
index 2448526..01f7f60 100644
--- a/node_modules/react-native-esbuild/src/commands/index.js
+++ b/node_modules/react-native-esbuild/src/commands/index.js
@@ -58,6 +58,11 @@ function createEsbuildCommands(
           description: 'Removes cached files',
           default: false,
         },
+        {
+          name: '--write-metafile <string>',
+          description:
+            'This option tells esbuild to produce some metadata about the build in JSON format. It can be used in Bundle Size Analyzer',
+        },
       ],
       description: 'builds the javascript bundle for offline use',
     },
diff --git a/node_modules/react-native-esbuild/src/esbuild-config.js b/node_modules/react-native-esbuild/src/esbuild-config.js
index 1e8c5b0..7deefdc 100644
--- a/node_modules/react-native-esbuild/src/esbuild-config.js
+++ b/node_modules/react-native-esbuild/src/esbuild-config.js
@@ -57,7 +57,7 @@ function getEsbuildConfig(config, args) {
     minify: typeof minify === 'boolean' ? minify : !dev,
     resolveExtensions,
     define: {
-      __DEV__: dev,
+      __DEV__: JSON.stringify(dev),
       global: 'window',
       'process.env.NODE_ENV': JSON.stringify(
         dev ? 'development' : 'production'
diff --git a/node_modules/react-native-esbuild/src/server/bundler.js b/node_modules/react-native-esbuild/src/server/bundler.js
index eba85b0..9f0b8d1 100644
--- a/node_modules/react-native-esbuild/src/server/bundler.js
+++ b/node_modules/react-native-esbuild/src/server/bundler.js
@@ -56,6 +56,7 @@ function createBundler(getBundleConfig, onBuild, logger) {
             const resolved = await build.resolve(
               entryFile.startsWith('/') ? entryFile : `./${localPath}`,
               {
+                kind: 'entry-point',
                 resolveDir: build.initialOptions.sourceRoot,
               }
             );
@@ -115,14 +116,13 @@ function createBundler(getBundleConfig, onBuild, logger) {
         sourcemapOutput: `${bundleOutput}.map`,
       });

-      await esbuild.build({
+      const ctx = await esbuild.context({
         ...buildOptions,
         bundle: true,
-        watch: true,
-        incremental: true,
         write: false,
         plugins: (buildOptions.plugins || []).concat(buildStatusPlugin),
       });
+      await ctx.watch();
     }
     return promiseMap[bundleOutput];
   };
oblador commented 1 year ago

Cool, would you mind making a PR out of this?

vovkasm commented 1 year ago

Cool, would you mind making a PR out of this?

I can, but not so fast, sorry. PR would require to deal with deps and testing, but I will have spare time only at the end of the week hopefully. So to help others prefer to just drop my patch (which I use with patch-package currently).

alxroyer commented 1 year ago

For the info, it seems that things went wrong from esbuild 0.16+.

With esbuild@0.16.7: