transform.d.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { ServerPlugin } from './server';
  2. import { Plugin as RollupPlugin } from 'rollup';
  3. import { SourceMap } from './server/serverPluginSourceMap';
  4. import { InternalResolver } from './resolver';
  5. declare type ParsedQuery = Record<string, string | string[] | undefined>;
  6. interface TransformTestContext {
  7. /**
  8. * Full specifier of the transformed module, including query parameters
  9. */
  10. id: string;
  11. /**
  12. * Path without query (use this to check for file extensions)
  13. */
  14. path: string;
  15. /**
  16. * Parsed query object
  17. */
  18. query: ParsedQuery;
  19. /**
  20. * Indicates whether this is a request made by js import(), or natively by
  21. * the browser (e.g. `<img src="...">`).
  22. */
  23. isImport: boolean;
  24. isBuild: boolean;
  25. /**
  26. * Indicates that the file for this request was not modified since last call.
  27. */
  28. notModified?: true;
  29. }
  30. export interface TransformContext extends TransformTestContext {
  31. code: string;
  32. }
  33. export interface TransformResult {
  34. code: string;
  35. map?: SourceMap;
  36. }
  37. export declare type TransformFn = (ctx: TransformContext) => string | TransformResult | Promise<string | TransformResult>;
  38. export interface Transform {
  39. test: (ctx: TransformTestContext) => boolean;
  40. transform: TransformFn;
  41. }
  42. export interface IndexHtmlTransformContext {
  43. code: string;
  44. isBuild: boolean;
  45. }
  46. export declare type IndexHtmlTransformFn = (ctx: IndexHtmlTransformContext) => string | Promise<string>;
  47. export declare type IndexHtmlTransform = IndexHtmlTransformFn | {
  48. /**
  49. * Timing for applying the transform.
  50. * @default: 'post'
  51. */
  52. apply?: 'pre' | 'post';
  53. transform: IndexHtmlTransformFn;
  54. };
  55. export declare type CustomBlockTransform = TransformFn;
  56. export declare function createServerTransformPlugin(transforms: Transform[], customBlockTransforms: Record<string, CustomBlockTransform>, resolver: InternalResolver): ServerPlugin;
  57. export declare function createBuildJsTransformPlugin(transforms: Transform[], customBlockTransforms: Record<string, CustomBlockTransform>): RollupPlugin;
  58. export {};