Repository: swc-project/swc (rust)

Issue:
swc.minify() always assumes strict mode
### Describe the bug

`swc.minify()` always behaves as if the passed-in code is in strict mode, regardless of whether `JsMinifyOptions["module"]` is `true` or `false`. You can successfully configure `swc.transform()` in this way with `Options["isModule"]`, but there doesn't seem to be a way to pass this configuration directly to `minify` _or_ to have `minify` look for and use a `.swcrc` file.

### Input code

```typescript
swc.minify('const f = () => "\240";', {
    compress: {
        defaults: false,

        arguments: false,
        arrows: false,
        booleans: false,
        booleans_as_integers: false,
        collapse_vars: false,
        comparisons: false,
        computed_props: false,
        conditionals: true,
        dead_code: true,
        directives: false,
        drop_console: false,
        drop_debugger: false,
        ecma: 5,
        evaluate: false,
        expression: false,
        hoist_funs: false,
        hoist_props: false,
        hoist_vars: false,
        ie8: false,
        if_return: false,
        inline: 0,
        join_vars: false,
        keep_classnames: false,
        keep_fargs: false,
        keep_fnames: false,
        keep_infinity: false,
        loops: false,
        module: false,
        negate_iife: false,
        passes: 1,
        properties: false,
        reduce_vars: false,
        sequences: false,
        side_effects: false,
        switches: false,
        toplevel: false,
        typeofs: false,
        unused: false,
    },
    format: { comments: false, semicolons: true },
    mangle: { toplevel: false },
    module: false,
    toplevel: false,
    ecma: 5,
});
```


### Config

```json
// see object passed to `minify` above
```


### Playground link

_No response_

### Expected behavior

Because `JsMinifyOptions["module"]` is set to `false` in the configuration object passed to `swc.minify()`, I expect the following output:
```js
const f=()=>"\xa0";
```

### Actual behavior

Instead, I receive the following error:
```
× Legacy octal escape is not permitted in strict mode

    ╭────
01 │ const f = () => "\240";
    ·                  ──
    ╰────
Caused by:
    0: failed to parse input file
    1: error was recoverable, but proceeding would result in wrong codegen
    2: Syntax Error
```

### Version

1.3.6

### Additional context

I can get this to work in the playground, but only because the playground allows a full `.swcrc` where I can set `isModule` to `true`. I'm assuming the playground uses `transform` instead of `minify`.

Pull request description:
fix(es/minifier): Parse a file as a module only if `opts.module` is `true`
**Description:**

Previously it was impossible to minify non-strict code (e.g. `delete identifier;`) because `minify()` unconditionally parsed the input as a module regardless of the `module` setting.

**BREAKING CHANGE:**

`minify()` now respects the `module` parameter instead of ignoring it, so users who had been processing modules with `minify()` without passing `{ module: true }` will now need to pass it.

**Related issue:**

 - Closes #6130.

Patch:
diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs
index dfeee46128..9843855f58 100644
--- a/crates/swc/src/lib.rs
+++ b/crates/swc/src/lib.rs
@@ -1089,7 +1089,7 @@ impl Compiler {
                         import_attributes: true,
                         ..Default::default()
                     }),
-                    IsModule::Bool(true),
+                    IsModule::Bool(opts.module),
                     Some(&comments),
                 )
                 .context("failed to parse input file")?;


Test patch:
diff --git a/bindings/binding_core_wasm/__tests__/simple.js b/bindings/binding_core_wasm/__tests__/simple.js
index ca1b1afcfd..71335fd2eb 100644
--- a/bindings/binding_core_wasm/__tests__/simple.js
+++ b/bindings/binding_core_wasm/__tests__/simple.js
@@ -195,7 +195,7 @@ describe("minify", () => {
 
         expect(output).toMatchInlineSnapshot(`
             Object {
-              "code": "const somename=1;console.log(1);",
+              "code": "let somename=1;console.log(1);",
             }
         `);
     });
@@ -207,7 +207,7 @@ describe("minify", () => {
 
         expect(output).toMatchInlineSnapshot(`
             Object {
-              "code": "const somename=1;console.log(1);",
+              "code": "let somename=1;console.log(1);",
             }
         `);
     });
diff --git a/crates/swc/tests/projects.rs b/crates/swc/tests/projects.rs
index 3f3c578d9f..e49497cf37 100644
--- a/crates/swc/tests/projects.rs
+++ b/crates/swc/tests/projects.rs
@@ -1091,6 +1091,7 @@ fn issue_7513_2() {
                     fm,
                     handler,
                     &JsMinifyOptions {
+                        module: true,
                         compress: BoolOrDataConfig::from_bool(true),
                         mangle: BoolOrDataConfig::from_obj(MangleOptions {
                             props: None,
diff --git a/node-swc/__tests__/minify_test.mjs b/node-swc/__tests__/minify_test.mjs
index f090a2d554..fe4646402a 100644
--- a/node-swc/__tests__/minify_test.mjs
+++ b/node-swc/__tests__/minify_test.mjs
@@ -1,10 +1,15 @@
 import swc from "../..";
 
 it("should compress", async () => {
-    const { code } = await swc.minify(`
+    const { code } = await swc.minify(
+        `
     import foo from '@src/app';
     console.log(foo)
-    `);
+    `,
+        {
+            module: true,
+        }
+    );
 
     expect(code).toMatchInlineSnapshot(
         `"import o from\\"@src/app\\";console.log(o);"`
@@ -17,7 +22,9 @@ it("should accept object", async () => {
     import foo from '@src/app';
     console.log(foo)
     `,
-        {}
+        {
+            module: true,
+        }
     );
 
     expect(code).toMatchInlineSnapshot(
@@ -32,6 +39,7 @@ it("should accept { mangle = true }", async () => {
     console.log(foo)
     `,
         {
+            module: true,
             compress: false,
             mangle: true,
         }
@@ -49,6 +57,7 @@ it("should accept { mangle = object }", async () => {
     console.log(foo)
     `,
         {
+            module: true,
             compress: false,
             mangle: {
                 topLevel: true,
@@ -245,3 +254,13 @@ describe("should remove comments", () => {
         `);
     });
 });
+
+it("should accept non-strict code", async () => {
+    const { code } = await swc.minify(`
+    a = 1;
+    delete a;
+    console.log(a);
+    `);
+
+    expect(code).toMatchInlineSnapshot(`"a=1,delete a,console.log(a);"`);
+});