{"id":"1d60a752eb687dd9","repo":"vitejs/vite","slug":"oxc-transform-error-1d60a7","errorCode":null,"errorMessage":"oxc transform error","messagePattern":"oxc transform error","errorType":"exception","errorClass":"AggregateError","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/plugins/define.ts","lineNumber":191,"sourceCode":"  id: string,\n  define: Record<string, string>,\n): {\n  code: string\n  map: ReturnType<typeof transformSync>['map'] | null\n} {\n  const result = transformSync(id, code, {\n    lang: 'js',\n    sourceType: 'module',\n    define,\n    sourcemap:\n      environment.config.command === 'build'\n        ? !!environment.config.build.sourcemap\n        : true,\n    tsconfig: false,\n  })\n\n  if (result.errors.length > 0) {\n    throw new AggregateError(result.errors, 'oxc transform error')\n  }\n\n  return {\n    code: result.code,\n    map: result.map || null,\n  }\n}\n\n/**\n * Like `JSON.stringify` but keeps raw string values as a literal\n * in the generated code. For example: `\"window\"` would refer to\n * the global `window` object directly.\n */\nexport function serializeDefine(define: Record<string, any>): string {\n  let res = `{`\n  const keys = Object.keys(define).sort()\n  for (let i = 0; i < keys.length; i++) {\n    const key = keys[i]","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/plugins/define.ts#L173-L209","documentation":"Thrown by `replaceDefine` in the define plugin when oxc's `transformSync` reports errors while substituting `define` values into JS. oxc performs the actual text replacement/parse; if the input code or a `define` substitution produces invalid JS, oxc emits diagnostics wrapped in an `AggregateError`.","triggerScenarios":"`transformSync(id, code, { lang: 'js', sourceType: 'module', define, ... })` returns a non-empty `errors` array — e.g. a `define` value that, when substituted, breaks syntax, or source code that is unparseable as a module.","commonSituations":"A `define` entry whose replacement value contains invalid JS (e.g. unbalanced quotes, a raw JSON object not wrapped properly), define keys that overlap with syntax, or source files with syntax incompatible with `sourceType: 'module'`.","solutions":["Inspect `AggregateError.errors` for the exact oxc diagnostic (line/column and message).","Ensure every `define` value is valid JS — wrap strings in quotes (`JSON.stringify`) and objects as literals.","Use Vite's `loadEnv` + `JSON.stringify(process.env.X)` pattern for env-based defines rather than raw string injection.","Fix the underlying syntax error in the source file if the define itself is fine."],"exampleFix":"// before\nexport default defineConfig({\n  define: { 'import.meta.env.CFG': '{ debug: true }' }, // invalid bare object\n});\n// after\nexport default defineConfig({\n  define: { 'import.meta.env.CFG': JSON.stringify({ debug: true }) },\n});","handlingStrategy":"validation","validationCode":"function validateDefine(define) {\n  for (const [k, v] of Object.entries(define)) {\n    // values must be valid JS expressions; strings must be quoted\n    if (typeof v === 'string' && v.length && !/^[\"'`].*[\"'`]$/.test(v) && /[^-+\\w.,\\s\\[\\]{}():?!<>=&|/*%]/.test(v)) {\n      throw new Error(`define['${k}'] value may be invalid JS: ${v}. Use JSON.stringify for strings/objects.`);\n    }\n  }\n}\n// validateDefine(config.define);","typeGuard":null,"tryCatchPattern":"try {\n  await build();\n} catch (e) {\n  if (e instanceof AggregateError && e.message === 'oxc transform error') {\n    for (const inner of e.errors) console.error(inner);\n    console.error('Check config.define values are valid JS literals');\n  }\n  throw e;\n}","preventionTips":["Always wrap env string/object define values with `JSON.stringify(...)`.","Avoid define keys that collide with real syntax tokens.","Typecheck source files so unparseable modules surface before the define transform."],"tags":["define","oxc","transform","config","syntax"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}