swc-project/swc · error
failed to parse `global_defs.{k}` of minifier options: {err:
Error message
failed to parse `global_defs.{k}` of minifier options: {err:?} What it means
While converting terser-compatible compress options (TerserCompressOptions::into_config), every `global_defs` key is parsed as a JavaScript expression with parse_file_as_expr. Keys are meant to be dotted identifier paths (optionally @-prefixed, e.g. "process.env.NODE_ENV" or "@clones"); a key that is not a valid expression panics with the parser error.
Source
Thrown at crates/swc_ecma_minifier/src/option/terser.rs:303
evaluate: self.evaluate.unwrap_or(self.defaults),
expr: self.expression,
global_defs: self
.global_defs
.into_iter()
.map(|(k, v)| {
let parse = |input: String| {
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 `global_defs.{k}` of minifier options: {err:?}")
})
};
let key = parse(if let Some(k) = k.strip_prefix('@') {
k.to_string()
} else {
k.to_string()
});
(
key,
if k.starts_with('@') {
parse(
v.as_str()
.unwrap_or_else(|| {
panic!(
"Value of `global_defs.{k}` must be a string literal: "
)
})View on GitHub (pinned to 5176682b65)
Solutions
- Make every global_defs key a valid dotted identifier path: "process.env.NODE_ENV", "defines.some.value"
- If the key started with '@', remember only the value semantics change (parsed as expression); the key after the prefix must still parse as an expression
- Validate keys against an identifier(.identifier)* pattern before building the minifier options
- Check the {err:?} payload in the panic for the exact syntax error position
Example fix
// before
compress: { global_defs: { "my const": true } } // panic: failed to parse `global_defs.my const`
// after
compress: { global_defs: { "my_const": true } } Defensive patterns
Strategy: validation
Validate before calling
// JS: every global_defs key must be a dotted path (optional leading @)
const keyOk = k => /^@?[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)*$/.test(k);
for (const k of Object.keys(globalDefs)) {
if (!keyOk(k)) throw new Error(`invalid global_defs key: ${k}`);
} Prevention
- Treat global_defs keys as expressions, not opaque strings
- Unit-test your minifier config object against a sample build in CI before deploying
- Generate configs from typed schemas that constrain key shape
When it happens
Trigger: Setting global_defs with keys containing spaces, brackets, keywords, or stray punctuation (e.g. "my const", "a[0]", "let x"); passing terser CLI --define arguments verbatim as keys.
Common situations: Porting a terser config where keys were treated as opaque strings, JSON configs generated from templates that inject whitespace, typos or smart quotes in define names.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Value of `global_defs.{k}` must be a string literal:
- failed to parse `pure_funcs` of minifier options: {err:?}
- `{raw}` is not a valid ecmascript version
- failed to parse jsx option {}: '{}' is not an expression
- Error occurred while loading config file at ${config}: ${e}
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/f268fc779e7ee15e.
Report an issue: GitHub.