swc-project/swc · error

failed to parse `pure_funcs` of minifier options: {err:?}

Error message

failed to parse `pure_funcs` of minifier options: {err:?}

What it means

Each entry in the terser-compatible `pure_funcs` compress option is parsed as a JavaScript expression (parse_file_as_expr) when converting TerserCompressOptions into CompressOptions. An entry with invalid expression syntax panics with the parser error.

Source

Thrown at crates/swc_ecma_minifier/src/option/terser.rs:416

            unused: self.unused.unwrap_or(self.defaults),
            const_to_let: self.const_to_let.unwrap_or(self.defaults),
            pristine_globals: self.pristine_globals.unwrap_or(self.defaults),
            pure_funcs: self
                .pure_funcs
                .into_iter()
                .map(|input| {
                    let fm = cm.new_source_file(FileName::Anon.into(), input);

                    parse_file_as_expr(
                        &fm,
                        Default::default(),
                        Default::default(),
                        None,
                        &mut Vec::new(),
                    )
                    .map(drop_span)
                    .unwrap_or_else(|err| {
                        panic!("failed to parse `pure_funcs` of minifier options: {err:?}")
                    })
                })
                .collect(),
            experimental: self
                .experimental
                .map(|experimental| {
                    CompressExperimentalOptions::from_terser_with_defaults(
                        experimental,
                        self.defaults,
                    )
                })
                .unwrap_or(CompressExperimentalOptions::from_defaults(self.defaults)),
        }
    }
}

impl From<TerserTopLevelOptions> for TopLevelOptions {
    fn from(c: TerserTopLevelOptions) -> Self {

View on GitHub (pinned to 5176682b65)

Solutions

  1. Use plain dotted identifier references: "Object.freeze", "_track", "MyClass.method"
  2. Remove empty strings and entries with statement syntax; pure_funcs marks call targets as side-effect free, it only needs the callee path
  3. Validate every entry against ^[A-Za-z_$][A-Za-z0-9_$.]*$ (allowing quoted segments) before constructing the options

Example fix

// before
compress: { pure_funcs: ["function f(){}", ""] } // panic: failed to parse `pure_funcs`

// after
compress: { pure_funcs: ["_track", "Object.freeze"] }
Defensive patterns

Strategy: validation

Validate before calling

// JS: pure_funcs entries must be dotted member paths
const pureOk = s => /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)*$/.test(s);
const bad = pureFuncs.filter(s => !pureOk(s));
if (bad.length) throw new Error(`invalid pure_funcs entries: ${bad.join(', ')}`);

Prevention

When it happens

Trigger: pure_funcs entries like "foo bar", "a(", "", "function f(){}" (statement-level text), or strings with stray punctuation that fail expression parsing.

Common situations: Passing function declarations instead of references ("function f(){}" instead of "f"), trailing semicolons/commas from templated config, empty strings produced by YAML/JSON templating, copy-paste of terser CLI quoting artifacts.

Understand the failure class

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/aacc2f6ef43f6926. Report an issue: GitHub.