Skip to content

chore: stablize cssScopeTo #19592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions packages/vite/types/metadata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,28 @@ 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']
* ```
*/
cssScopeTo?: [importerId: string, exportName: string | undefined]
}

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

export interface CustomPluginOptions {
vite?: CustomPluginOptionsVite
}
}
Loading
Loading