Repository: vercel/next.js
Pull request: perf(next-swc): Remove needless JSON conversion

### What?

Remove JSON operations that are not required.

### Why?

 - https://github.com/swc-project/swc/pull/10628
 - https://github.com/swc-project/swc/issues/10610

### How?


Closes PACK-4870

Changed files:
filename: crates/napi/src/minify.rs
patch: @@ -26,57 +26,30 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 DEALINGS IN THE SOFTWARE.
 */
 
+use anyhow::Context;
 use napi::bindgen_prelude::*;
-use rustc_hash::FxHashMap;
-use serde::Deserialize;
 use swc_core::{
     base::{TransformOutput, config::JsMinifyOptions, try_with_handler},
-    common::{FileName, GLOBALS, SourceFile, SourceMap, errors::ColorConfig, sync::Lrc},
+    common::{FileName, GLOBALS, errors::ColorConfig},
 };
 
 use crate::{get_compiler, util::MapErr};
 
 pub struct MinifyTask {
     c: swc_core::base::Compiler,
-    code: MinifyTarget,
+    code: Option<String>,
     opts: JsMinifyOptions,
 }
 
-#[derive(Deserialize)]
-#[serde(untagged)]
-enum MinifyTarget {
-    /// Code to minify.
-    Single(String),
-    /// `{ filename: code }`
-    Map(FxHashMap<String, String>),
-}
-
-impl MinifyTarget {
-    fn to_file(&self, cm: Lrc<SourceMap>) -> Lrc<SourceFile> {
-        match self {
-            MinifyTarget::Single(code) => cm.new_source_file(FileName::Anon.into(), code.clone()),
-            MinifyTarget::Map(codes) => {
-                assert_eq!(
-                    codes.len(),
-                    1,
-                    "swc.minify does not support concatenating multiple files yet"
-                );
-
-                let (filename, code) = codes.iter().next().unwrap();
-
-                cm.new_source_file(FileName::Real(filename.clone().into()).into(), code.clone())
-            }
-        }
-    }
-}
-
 #[napi]
 impl Task for MinifyTask {
     type Output = TransformOutput;
 
     type JsValue = TransformOutput;
 
     fn compute(&mut self) -> napi::Result<Self::Output> {
+        let code = self.code.take().unwrap_or_default();
+
         try_with_handler(
             self.c.cm.clone(),
             swc_core::base::HandlerOpts {
@@ -85,7 +58,7 @@ impl Task for MinifyTask {
             },
             |handler| {
                 GLOBALS.set(&Default::default(), || {
-                    let fm = self.code.to_file(self.c.cm.clone());
+                    let fm = self.c.cm.new_source_file(FileName::Anon.into(), code);
 
                     self.c.minify(fm, handler, &self.opts, Default::default())
                 })
@@ -106,24 +79,32 @@ pub fn minify(
     opts: Buffer,
     signal: Option<AbortSignal>,
 ) -> napi::Result<AsyncTask<MinifyTask>> {
-    let code = serde_json::from_slice(&input)?;
+    let code = String::from_utf8(input.into())
+        .context("failed to convert input to string")
+        .convert_err()?;
     let opts = serde_json::from_slice(&opts)?;
 
     let c = get_compiler();
 
-    let task = MinifyTask { c, code, opts };
+    let task = MinifyTask {
+        c,
+        code: Some(code),
+        opts,
+    };
 
     Ok(AsyncTask::with_optional_signal(task, signal))
 }
 
 #[napi]
 pub fn minify_sync(input: Buffer, opts: Buffer) -> napi::Result<TransformOutput> {
-    let code: MinifyTarget = serde_json::from_slice(&input)?;
+    let code = String::from_utf8(input.into())
+        .context("failed to convert input to string")
+        .convert_err()?;
     let opts = serde_json::from_slice(&opts)?;
 
     let c = get_compiler();
 
-    let fm = code.to_file(c.cm.clone());
+    let fm = c.cm.new_source_file(FileName::Anon.into(), code);
 
     try_with_handler(
         c.cm.clone(),

filename: packages/next/src/build/swc/index.ts
patch: @@ -1364,11 +1364,11 @@ function loadNative(importPath?: string) {
       },
 
       minify(src: string, options: any) {
-        return bindings.minify(toBuffer(src), toBuffer(options ?? {}))
+        return bindings.minify(Buffer.from(src), toBuffer(options ?? {}))
       },
 
       minifySync(src: string, options: any) {
-        return bindings.minifySync(toBuffer(src), toBuffer(options ?? {}))
+        return bindings.minifySync(Buffer.from(src), toBuffer(options ?? {}))
       },
 
       parse(src: string, options: any) {