Skip to content

Commit

Permalink
feat: expose CustomPluginOptionsVite type
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 3, 2025
1 parent f612e0f commit 8ecd7ba
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 234 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"playwright-chromium": "^1.50.1",
"premove": "^4.0.0",
"prettier": "3.5.3",
"rollup": "^4.30.1",
"rollup": "^4.34.9",
"rollup-plugin-esbuild": "^6.2.1",
"simple-git-hooks": "^2.11.1",
"tslib": "^2.8.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"dependencies": {
"esbuild": "^0.25.0",
"postcss": "^8.5.3",
"rollup": "^4.30.1"
"rollup": "^4.34.9"
},
"optionalDependencies": {
"fsevents": "~2.3.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export type {
GeneralImportGlobOptions,
KnownAsTypeMap,
} from 'types/importGlob'
export type { ChunkMetadata } from 'types/metadata'
export type { ChunkMetadata, CustomPluginOptionsVite } from 'types/metadata'

// dep types
export type {
Expand Down
18 changes: 0 additions & 18 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,24 +323,6 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
>
}

export interface CustomPluginOptionsVite {
/**
* If this is a CSS Rollup module, you can scope to its importer's exports
* so that if those exports are treeshaken away, the CSS module will also
* be treeshaken.
*
* The "importerId" must import the CSS Rollup module statically.
*
* Example config if the CSS id is `/src/App.vue?vue&type=style&lang.css`:
* ```js
* cssScopeTo: ['/src/App.vue', 'default']
* ```
*
* @experimental
*/
cssScopeTo?: [importerId: string, exportName: string | undefined]
}

export type HookHandler<T> = T extends ObjectHook<infer H> ? H : T

export type PluginWithRequiredHook<K extends keyof Plugin> = Plugin & {
Expand Down
9 changes: 3 additions & 6 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type {
TransformAttributeResult as LightningCssTransformAttributeResult,
TransformResult as LightningCssTransformResult,
} from 'lightningcss'
import type { CustomPluginOptionsVite } from 'types/metadata'
import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap'
import type { EnvironmentModuleNode } from '../server/moduleGraph'
import {
Expand All @@ -54,7 +55,7 @@ import {
SPECIAL_QUERY_RE,
} from '../constants'
import type { ResolvedConfig } from '../config'
import type { CustomPluginOptionsVite, Plugin } from '../plugin'
import type { Plugin } from '../plugin'
import { checkPublicFile } from '../publicDir'
import {
arraify,
Expand Down Expand Up @@ -650,11 +651,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// NOTE: `this.getModuleInfo` can be undefined when the plugin is called directly
// adding `?.` temporary to avoid unocss from breaking
// TODO: remove `?.` after `this.getModuleInfo` in Vite 7
(
this.getModuleInfo?.(id)?.meta?.vite as
| CustomPluginOptionsVite
| undefined
)?.cssScopeTo
this.getModuleInfo?.(id)?.meta?.vite?.cssScopeTo

// record css
if (!inlined) {
Expand Down
22 changes: 22 additions & 0 deletions packages/vite/types/metadata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,30 @@ export interface ChunkMetadata {
importedCss: Set<string>
}

export interface CustomPluginOptionsVite {
/**
* If this is a CSS Rollup module, you can scope to its importer's exports
* so that if those exports are treeshaken away, the CSS module will also
* be treeshaken.
*
* The "importerId" must import the CSS Rollup module statically.
*
* Example config if the CSS id is `/src/App.vue?vue&type=style&lang.css`:
* ```js
* cssScopeTo: ['/src/App.vue', 'default']
* ```
*
* @experimental
*/
cssScopeTo?: [importerId: string, exportName: string | undefined]
}

declare module 'rollup' {
export interface RenderedChunk {
viteMetadata?: ChunkMetadata
}

export interface CustomPluginOptions {
vite?: CustomPluginOptionsVite
}
}
Loading

0 comments on commit 8ecd7ba

Please # to comment.