-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
33 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
import { Plugin } from "vite"; | ||
import { boot, exit, transform, Options } from "../server"; | ||
|
||
/** Configures plugin behaviour. */ | ||
/** Configures vite plugin behaviour. */ | ||
export type ViteOptions = Options & { | ||
/** Extension of the source files to transform; transforms all files by default. */ | ||
ext?: string, | ||
/** Force the plugin to run either before are after other plugins. */ | ||
enforce?: "pre" | "post" | ||
enforce?: "pre" | "post"; | ||
/** Specify condition when document shouldn't be transformed by the plugin. */ | ||
skip?: (code: string, id: string, options?: { ssr?: boolean; }) => boolean; | ||
}; | ||
|
||
declare interface VitePlugin { | ||
name: string; | ||
enforce?: "pre" | "post"; | ||
buildStart?: (options: unknown) => Promise<void> | void; | ||
transform?: (code: string, id: string, options?: { ssr?: boolean; }) => Promise<string> | string; | ||
buildEnd?: (error?: Error) => void; | ||
} | ||
|
||
/** Creates imgit plugin instance for vite. */ | ||
export default function (options?: ViteOptions): Plugin { | ||
boot(options); | ||
const skip = (id: string) => options?.ext && !id.endsWith(options.ext); | ||
export default function (options?: ViteOptions): VitePlugin { | ||
return { | ||
name: "imgit", | ||
enforce: options?.enforce, | ||
transform: (code, id) => skip(id) ? code : transform(code), | ||
buildStart: _ => boot(options), | ||
transform: (code, id, opt) => options?.skip?.(code, id, opt) ? code : transform(code), | ||
buildEnd: exit | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.