跳到內容
文件
組態
縮小

縮小

⚠️

此部分適用於想要將 swc 作為建置程序最後步驟的人員。如果您想將 swc 縮小器與打包器搭配使用,請參閱 swcMinify 的文件。


Webpack 文件

v1.2.67 開始,您可以透過在 .swcrc 檔案中啟用 minify 來設定 SWC 縮小您的程式碼

.swcrc
{
  // Enable minification
  "minify": true,
  // Optional, configure minification options
  "jsc": {
    "minify": {
      "compress": {
        "unused": true
      },
      "mangle": true
    }
  }
}

組態

關於註解的注意事項

如果您將 jsc.minify.compress 設定為 true{},SWC 將只保留授權註解。如果您不想要這樣,請修改 jsc.minify.format

jsc.minify.compress

類型:布林值 | 物件

類似於 terser(在新分頁中開啟)compress 選項

.swcrc
{
  "jsc": {
    "minify": {
      "compress": true // equivalent to {}
    }
  }
}
  • arguments,預設為 false
  • arrows,預設為 true
  • booleans,預設為 true
  • 布林值作為整數,預設為 false
  • 摺疊變數,預設為 true
  • 比較,預設為 true
  • 計算屬性,預設為 true
  • 條件,預設為 true
  • 無用程式碼,預設為 true
  • 預設值,預設為 true
  • 指令,預設為 true
  • 移除主控台,預設為 false
  • drop_debugger,預設為 true
  • ecma,預設為 5
  • evaluate,預設為 true
  • global_defs,預設為 {}
  • hoist_funs,預設為 false
  • hoist_props,預設為 true
  • hoist_vars,預設為 false
  • ie8,忽略。
  • if_return,預設為 true
  • inline,預設為 true
  • join_vars,預設為 true
  • keep_classnames,預設為 false
  • keep_fargs,預設為 false
  • keep_infinity,預設為 false
  • loops,預設為 true
  • negate_iife,預設為 true
  • passes,預設為 0,表示沒有限制。
  • properties,預設為 true
  • pure_getters,預設為 ``。
  • pure_funcs,預設為 []。類型為字串陣列。
  • reduce_funcs,預設為 false
  • reduce_vars,預設為 true
  • sequences,預設為 true
  • side_effects,預設為 true
  • switches,預設為 true
  • top_retain,預設為 ``。
  • toplevel,預設為 true
  • typeofs,預設為 true
  • unsafe,預設為 false
  • unsafe_arrows,預設為 false
  • unsafe_comps,預設為 false
  • unsafe_Function,預設為 false
  • unsafe_math,預設為 false
  • unsafe_symbols,預設為 false
  • unsafe_methods,預設為 false
  • unsafe_proto,預設為 false
  • unsafe_regexp,預設為 false
  • unsafe_undefined,預設為 false
  • unused,預設為 true
  • module,忽略。目前,所有檔案都視為模組。

jsc.minify.mangle

類型:布林值 | 物件

類似於

{
  "jsc": {
    "minify": {
      "mangle": true // equivalent to {}
    }
  }
}
  • props,預設為 false,且 true 等同於 {}
  • topLevel,預設為 true。別名為 toplevel,以與 terser 相容。
  • keepClassNames,預設為 false。別名為 keep_classnames,以與 terser 相容。
  • keepFnNames,預設為 false
  • keepPrivateProps,預設為 false。別名為 keep_private_props,以與 terser 相容。
  • reserved,預設為 []
  • ie8,忽略。
  • safari10,預設為 false

jsc.minify.mangle.properties

類型:object

類似於 tersermangle properties 選項(在新分頁中開啟)

.swcrc
{
  "jsc": {
    "minify": {
      "mangle":{
        "properties":{
          "reserved": ["foo", "bar"],
          "undeclared":false,
          "regex":"rust regex"
        }
      }
    }
  }
}

jsc.minify.format

這些屬性大多尚未實作,但其存在是為了支援將 terser 設定傳遞給 swc minify,而無需修改。

@swc/core 使用方式

swc.minify(code, options)

此 API 為非同步,所有解析、縮小和程式碼產生都會在背景執行緒中進行。 options 參數與 jsc.minify 物件相同。例如

import swc from "@swc/core";
 
const { code, map } = await swc.minify(
  "import foo from '@src/app'; console.log(foo)",
  {
    compress: false,
    mangle: true,
  }
);
 
expect(code).toMatchInlineSnapshot(`"import a from'@src/app';console.log(a);"`);

傳回 Promise<{ code: string, map: string }>

swc.minifySync(code, options)

此 API 存在於 @swc/core@swc/wasm@swc/wasm-web

import swc from "@swc/core";
 
const { code, map } = swc.minifySync(
  "import foo from '@src/app'; console.log(foo)",
  {
    compress: false,
    mangle: true,
    module: true
  }
);
 
expect(code).toMatchInlineSnapshot(`"import a from'@src/app';console.log(a);"`);

傳回 { code: string, map: string }

WebAssembly 的 API

取代 Terser

透過 Yarn 解析(在新分頁中開啟),您可以在不需要套件更新其相依項的情況下,減少建置時間並覆寫 Terser。範例 package.json 會包含

package.json
{
  "resolutions": { "terser": "npm:@swc/core" }
}

這將針對所有巢狀相依項使用 SWC 壓縮器,而非 Terser。請務必移除鎖定檔並重新安裝相依項。

$ rm -rf node_modules yarn.lock
$ yarn
上次更新於 2024 年 4 月 15 日