swc-project / swc

Rust-based platform for the Web

Home Page:https://swc.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

spack's minifier does not respect `topLevel: true` for exported identifiers

evanw opened this issue · comments

Describe the bug

Code minified with spack is much larger than necessary because exported identifiers are not minified despite topLevel: true in the configuration.

Input code

// in.js
let fn = (foo, bar) => foo + bar
export let someLongVariableName1 = 123
export let someLongVariableName2 = 234
console.log(fn(someLongVariableName1, someLongVariableName2))

Config

I tried to enable full minification using this config:

// spack.config.js
module.exports = {
  mode: 'production',
  options: {
    jsc: {
      minify: {
        compress: true,
        mangle: {
          topLevel: true,
        },
        toplevel: true,
      },
    },
    minify: true,
  },
  entry: {
    web: 'in.js',
  },
  output: {
    path: 'out',
  },
};

Please let me know if that's not the right way to do it.

Expected behavior

The generated output is this (269 bytes):

var a=function(b,c){return b+c};var someLongVariableName11=123;var someLongVariableName21=234;console.log(a(someLongVariableName11,someLongVariableName21));export{someLongVariableName11 as someLongVariableName1};export{someLongVariableName21 as someLongVariableName2}

I expected something more like this where all top-level names are minified (112 bytes, which is over 2x smaller):

var l=(e,o)=>e+o,a=123,t=234;console.log(l(a,t));export{a as someLongVariableName1,t as someLongVariableName2};

The top-level name fn is being minified so it looks like the topLevel: true setting is having an effect. However, someLongVariableName1 and someLongVariableName2 are not minified even though they are top-level variables.

Version

The version of @swc/core: 1.2.98

Additional context

I filed this issue because I'm attempting to validate spack as a bundler. Please let me know if this validation isn't helpful and I can stop filing these issues. I'm asking because I don't want to create unnecessary work for you if it's not ready yet or if you aren't prioritizing work on spack at the moment, since I don't personally use spack myself and don't need these fixes for my work.

Currnetly, spack does not apply minifier.
It's ridiculous, but the only serious usecase of the bundler is deno bundle, and they can apply minifier separately if they want, so I didn't bother to apply it.