All files / src/swc minify.ts

25% Statements 2/8
0% Branches 0/3
0% Functions 0/2
25% Lines 2/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  1x             1x                                                                
import type { Plugin } from "rollup";
import { getSwc } from "./loadSwc";
 
type MinifyOptions = {
  compress?: boolean;
  mangle?: boolean;
};
 
export function minify(input: MinifyOptions = {}): Plugin {
  const defaults: MinifyOptions = {
    compress: true,
    mangle: true,
  };
 
  const options = Object.assign(
    {
      ...defaults,
    },
    input,
  );
  return {
    name: "swc-minify",
 
    async renderChunk(
      contents,
      _,
      { format, sourcemap, sourcemapExcludeSources },
    ) {
      const { minify: minifySwc } = await getSwc();
      const { code, map } = await minifySwc(contents, {
        ...options,
        module: format === "es",
        sourceMap: !!sourcemap,
        inlineSourcesContent: !sourcemapExcludeSources,
      });
 
      return { code, map: map || null };
    },
  };
}