marmotte / vite/lib/context
vite/lib/context
Classes
DefaultVitePluginContext
Defined in: src/vite/lib/context.ts:185
Extends
VitePluginContext<{sourceDir:string; }, { },this>
Constructors
Constructor
new DefaultVitePluginContext(
input):DefaultVitePluginContext
Defined in: src/vite/lib/context.ts:163
Parameters
input
ContextInput<{ sourceDir: string; }, { }>
Returns
Inherited from
contextFactory( {}, { paths: { sourceDir: "./src" }, }, ).constructor
Properties
generator
generator:
object
Defined in: src/vite/lib/context.ts:64
Defines the generator info used when calling VitePluginContext.writeAutoGeneratedFile and VitePluginContext.writeDefaultFile You can (and should) override this when extending context
name
name:
string
version
version:
string
Inherited from
options
readonlyoptions:ContextResolved<{sourceDir:string; }, { }>
Defined in: src/vite/lib/context.ts:58
Inherited from
Methods
resolve()
resolve(
base, ...paths):string
Defined in: src/vite/lib/context.ts:79
Parameters
base
"sourceDir" | "root"
paths
...string[]
Returns
string
Absolute path to desired resource
Inherited from
resolveBase()
resolveBase(
base):string
Defined in: src/vite/lib/context.ts:68
resolves a base path using options or defaults
Parameters
base
"sourceDir" | "root"
Returns
string
Inherited from
resolvePackageJson()
resolvePackageJson():
Promise<{[key:string]:unknown;dependencies:Record<string,string>;description:string;name:string;version:string; }>
Defined in: src/vite/lib/context.ts:84
Returns
Promise<{[key: string]: unknown; dependencies: Record<string, string>; description: string; name: string; version: string; }>
Inherited from
VitePluginContext.resolvePackageJson
writeAutoGeneratedFile()
writeAutoGeneratedFile(
file,content,ctx?):Promise<void>
Defined in: src/vite/lib/context.ts:92
Parameters
file
string
content
string
ctx?
Partial<CodeGenContext>
Returns
Promise<void>
Inherited from
VitePluginContext.writeAutoGeneratedFile
writeDefaultFile()
writeDefaultFile(
file,content,options?,ctx?):Promise<void>
Defined in: src/vite/lib/context.ts:99
Parameters
file
string
content
string
options?
force?
boolean
still writes even if file exists
noHeader?
boolean
don't write the default header
ctx?
Partial<CodeGenContext>
Returns
Promise<void>
Inherited from
VitePluginContext.writeDefaultFile
Interfaces
VitePluginContext
Defined in: src/vite/lib/context.ts:57
Extended by
Type Parameters
Paths
Paths
Opts
Opts
Properties
generator
generator:
object
Defined in: src/vite/lib/context.ts:64
Defines the generator info used when calling VitePluginContext.writeAutoGeneratedFile and VitePluginContext.writeDefaultFile You can (and should) override this when extending context
name
name:
string
version
version:
string
options
readonlyoptions:ContextResolved<Paths,Opts>
Defined in: src/vite/lib/context.ts:58
Methods
resolve()
resolve(
base, ...paths):string
Defined in: src/vite/lib/context.ts:79
Parameters
base
"root" | keyof Paths & string
paths
...string[]
Returns
string
Absolute path to desired resource
resolveBase()
resolveBase(
base):string
Defined in: src/vite/lib/context.ts:68
resolves a base path using options or defaults
Parameters
base
"root" | keyof Paths & string
Returns
string
resolvePackageJson()
resolvePackageJson():
Promise<{[key:string]:unknown;dependencies:Record<string,string>;description:string;name:string;version:string; }>
Defined in: src/vite/lib/context.ts:84
Returns
Promise<{[key: string]: unknown; dependencies: Record<string, string>; description: string; name: string; version: string; }>
writeAutoGeneratedFile()
writeAutoGeneratedFile(
file,content,ctx?):Promise<void>
Defined in: src/vite/lib/context.ts:92
Parameters
file
string
content
string
ctx?
Partial<CodeGenContext>
Returns
Promise<void>
writeDefaultFile()
writeDefaultFile(
file,content,options?,ctx?):Promise<void>
Defined in: src/vite/lib/context.ts:99
Parameters
file
string
content
string
options?
force?
boolean
still writes even if file exists
noHeader?
boolean
don't write the default header
ctx?
Partial<CodeGenContext>
Returns
Promise<void>
Type Aliases
ContextInput
ContextInput<
Paths,Opts> =object&{ [K in keyof Paths]?: string }&{ [K in keyof Opts]?: Opts[K] }
Defined in: src/vite/lib/context.ts:23
What the user passes to the context constructor. All path and option keys are optional overrides.
Type Declaration
root
root:
string
Type Parameters
Paths
Paths
Opts
Opts
contextOptions
contextOptions<
C> =CextendsVitePluginContext<infer _Paths, infer _Opts> ?C["options"] :never
Defined in: src/vite/lib/context.ts:181
Extracts the resolved options type (ctx.options) from a VitePluginContext instance or class.
Type Parameters
C
C
ContextResolved
ContextResolved<
Paths,Opts> =object&{ [K in keyof Paths]?: string }&Opts
Defined in: src/vite/lib/context.ts:28
What ctx.options holds after construction. Path overrides are still optional; option defaults have been applied.
Type Declaration
root
root:
string
Type Parameters
Paths
Paths
Opts
Opts
getPathMap
getPathMap<
C> =CextendsVitePluginContext<infer Paths, infer _Opts> ? keyofPaths&string:never
Defined in: src/vite/lib/context.ts:177
Extracts the union of path names from a VitePluginContext instance or class.
Type Parameters
C
C
Functions
contextFactory()
contextFactory<
Opts,Paths>(opts,config): (input) =>VitePluginContext<Paths,Opts>
Defined in: src/vite/lib/context.ts:157
Creates a typed VitePluginContext subclass with custom options and named paths.
Type Parameters
Opts
Opts extends Record<string, unknown>
Paths
Paths extends PathDefaults<keyof Paths & string>
Parameters
opts
Opts
Plugin option defaults. Keys become non-optional on ctx.options with their inferred type (e.g. string | false). Pass {} when you have no options.
config
object & NoOverlap<Opts, Paths>
Factory configuration.
Returns
(input) => VitePluginContext<Paths, Opts>
Example
const MyContext = contextFactory(
{ serve: "/docs/" as string | false },
{ paths: { sourceDir: "./src", docsDir: "./docs" } },
);
class Context extends MyContext {
generator = { name: "my-plugin", version: "1.0.0" };
}
const ctx = new Context({ root: config.root, serve: false });
ctx.options.serve // false
ctx.options.sourceDir // string | undefined
ctx.resolve("docsDir") // "<root>/docs"