index.d.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { TransformOptions, ParserOptions } from '@babel/core';
  2. import { ResolvedConfig, PluginOption } from 'vite';
  3. interface Options {
  4. include?: string | RegExp | Array<string | RegExp>;
  5. exclude?: string | RegExp | Array<string | RegExp>;
  6. /**
  7. * Enable `react-refresh` integration. Vite disables this in prod env or build mode.
  8. * @default true
  9. */
  10. fastRefresh?: boolean;
  11. /**
  12. * Set this to `"automatic"` to use [vite-react-jsx](https://github.com/alloc/vite-react-jsx).
  13. * @default "automatic"
  14. */
  15. jsxRuntime?: 'classic' | 'automatic';
  16. /**
  17. * Control where the JSX factory is imported from.
  18. * This option is ignored when `jsxRuntime` is not `"automatic"`.
  19. * @default "react"
  20. */
  21. jsxImportSource?: string;
  22. /**
  23. * Set this to `true` to annotate the JSX factory with `\/* @__PURE__ *\/`.
  24. * This option is ignored when `jsxRuntime` is not `"automatic"`.
  25. * @default true
  26. */
  27. jsxPure?: boolean;
  28. /**
  29. * Toggles whether or not to throw an error if an XML namespaced tag name is used.
  30. * @default true
  31. */
  32. jsxThrowIfNamespace?: boolean;
  33. /**
  34. * Babel configuration applied in both dev and prod.
  35. */
  36. babel?: BabelOptions | ((id: string, options: {
  37. ssr?: boolean;
  38. }) => BabelOptions);
  39. }
  40. declare type BabelOptions = Omit<TransformOptions, 'ast' | 'filename' | 'root' | 'sourceFileName' | 'sourceMaps' | 'inputSourceMap'>;
  41. /**
  42. * The object type used by the `options` passed to plugins with
  43. * an `api.reactBabel` method.
  44. */
  45. interface ReactBabelOptions extends BabelOptions {
  46. plugins: Extract<BabelOptions['plugins'], any[]>;
  47. presets: Extract<BabelOptions['presets'], any[]>;
  48. overrides: Extract<BabelOptions['overrides'], any[]>;
  49. parserOpts: ParserOptions & {
  50. plugins: Extract<ParserOptions['plugins'], any[]>;
  51. };
  52. }
  53. declare type ReactBabelHook = (babelConfig: ReactBabelOptions, context: ReactBabelHookContext, config: ResolvedConfig) => void;
  54. declare type ReactBabelHookContext = {
  55. ssr: boolean;
  56. id: string;
  57. };
  58. declare module 'vite' {
  59. interface Plugin {
  60. api?: {
  61. /**
  62. * Manipulate the Babel options of `@vitejs/plugin-react`
  63. */
  64. reactBabel?: ReactBabelHook;
  65. };
  66. }
  67. }
  68. declare function viteReact(opts?: Options): PluginOption[];
  69. declare namespace viteReact {
  70. var preambleCode: string;
  71. }
  72. export { BabelOptions, Options, ReactBabelOptions, viteReact as default };