| 168 | } |
| 169 | |
| 170 | export function replaceDefine( |
| 171 | environment: Environment, |
| 172 | code: string, |
| 173 | id: string, |
| 174 | define: Record<string, string>, |
| 175 | ): { |
| 176 | code: string |
| 177 | map: ReturnType<typeof transformSync>['map'] | null |
| 178 | } { |
| 179 | const result = transformSync(id, code, { |
| 180 | lang: 'js', |
| 181 | sourceType: 'module', |
| 182 | define, |
| 183 | sourcemap: |
| 184 | environment.config.command === 'build' |
| 185 | ? !!environment.config.build.sourcemap |
| 186 | : true, |
| 187 | tsconfig: false, |
| 188 | }) |
| 189 | |
| 190 | if (result.errors.length > 0) { |
| 191 | throw new AggregateError(result.errors, 'oxc transform error') |
| 192 | } |
| 193 | |
| 194 | return { |
| 195 | code: result.code, |
| 196 | map: result.map || null, |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Like `JSON.stringify` but keeps raw string values as a literal |