{"record":{"id":"f268fc779e7ee15e","repo":"swc-project/swc","slug":"failed-to-parse-global-defs-k-of-minifier-opti","errorCode":null,"errorMessage":"failed to parse `global_defs.{k}` of minifier options: {err:?}","messagePattern":"failed to parse `global_defs\\.(.+?)` of minifier options: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_minifier/src/option/terser.rs","lineNumber":303,"sourceCode":"            evaluate: self.evaluate.unwrap_or(self.defaults),\n            expr: self.expression,\n            global_defs: self\n                .global_defs\n                .into_iter()\n                .map(|(k, v)| {\n                    let parse = |input: String| {\n                        let fm = cm.new_source_file(FileName::Anon.into(), input);\n\n                        parse_file_as_expr(\n                            &fm,\n                            Default::default(),\n                            Default::default(),\n                            None,\n                            &mut Vec::new(),\n                        )\n                        .map(drop_span)\n                        .unwrap_or_else(|err| {\n                            panic!(\"failed to parse `global_defs.{k}` of minifier options: {err:?}\")\n                        })\n                    };\n                    let key = parse(if let Some(k) = k.strip_prefix('@') {\n                        k.to_string()\n                    } else {\n                        k.to_string()\n                    });\n\n                    (\n                        key,\n                        if k.starts_with('@') {\n                            parse(\n                                v.as_str()\n                                    .unwrap_or_else(|| {\n                                        panic!(\n                                            \"Value of `global_defs.{k}` must be a string literal: \"\n                                        )\n                                    })","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_minifier/src/option/terser.rs#L285-L321","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ncompress: { global_defs: { \"my const\": true } } // panic: failed to parse `global_defs.my const`\n\n// after\ncompress: { global_defs: { \"my_const\": true } }","handlingStrategy":"validation","validationCode":"// JS: every global_defs key must be a dotted path (optional leading @)\nconst keyOk = k => /^@?[A-Za-z_$][\\w$]*(\\.[A-Za-z_$][\\w$]*)*$/.test(k);\nfor (const k of Object.keys(globalDefs)) {\n  if (!keyOk(k)) throw new Error(`invalid global_defs key: ${k}`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["minifier","terser","global-defs","config","parse-error"],"backgroundTag":"config-expression-parse-failed","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}