(config: ResolvedConfig)
| 276 | } |
| 277 | |
| 278 | export function esbuildPlugin(config: ResolvedConfig): Plugin { |
| 279 | const options = config.esbuild as ESBuildOptions |
| 280 | const { jsxInject, include, exclude, ...esbuildTransformOptions } = options |
| 281 | |
| 282 | const filter = createFilter(include || /\.(m?ts|[jt]sx)$/, exclude || /\.js$/) |
| 283 | |
| 284 | // Remove optimization options for dev as we only need to transpile them, |
| 285 | // and for build as the final optimization is in `buildEsbuildPlugin` |
| 286 | const transformOptions: EsbuildTransformOptions = { |
| 287 | target: 'esnext', |
| 288 | ...esbuildTransformOptions, |
| 289 | minify: false, |
| 290 | minifyIdentifiers: false, |
| 291 | minifySyntax: false, |
| 292 | minifyWhitespace: false, |
| 293 | treeShaking: false, |
| 294 | // keepNames is not needed when minify is disabled. |
| 295 | // Also transforming multiple times with keepNames enabled breaks |
| 296 | // tree-shaking. (#9164) |
| 297 | keepNames: false, |
| 298 | supported: { |
| 299 | ...defaultEsbuildSupported, |
| 300 | ...esbuildTransformOptions.supported, |
| 301 | }, |
| 302 | } |
| 303 | |
| 304 | let server: ViteDevServer | undefined |
| 305 | |
| 306 | return { |
| 307 | name: 'vite:esbuild', |
| 308 | configureServer(_server) { |
| 309 | server = _server |
| 310 | }, |
| 311 | async transform(code, id) { |
| 312 | if (filter(id) || filter(cleanUrl(id))) { |
| 313 | const result = await transformWithEsbuild( |
| 314 | code, |
| 315 | id, |
| 316 | transformOptions, |
| 317 | undefined, |
| 318 | config, |
| 319 | server?.watcher, |
| 320 | ) |
| 321 | if (result.warnings.length) { |
| 322 | result.warnings.forEach((m) => { |
| 323 | this.warn(prettifyMessage(m, code)) |
| 324 | }) |
| 325 | } |
| 326 | if (jsxInject && jsxExtensionsRE.test(id)) { |
| 327 | result.code = jsxInject + ';' + result.code |
| 328 | } |
| 329 | return { |
| 330 | code: result.code, |
| 331 | map: result.map, |
| 332 | moduleType: 'js', |
| 333 | } |
| 334 | } |
| 335 | }, |
nothing calls this directly
no test coverage detected