Skip to content

Commit 12b257d

Browse files
committed
chore: improve condition and add comment
1 parent bc7ed34 commit 12b257d

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

packages/vite-plugin-svelte/src/utils/compile.js

+22-21
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,30 @@ export function createCompileSvelte() {
133133
let compiled;
134134
try {
135135
compiled = svelte.compile(finalCode, { ...finalCompileOptions, filename });
136-
if (compiled.css) {
137-
if (finalCode.includes(':global') && compiled.ast.css) {
138-
walk(
139-
compiled.ast.css,
140-
{},
141-
{
142-
Selector(node, { stop }) {
143-
if (
144-
node.children?.[0].type === 'PseudoClassSelector' &&
145-
node.children[0].name === 'global'
146-
) {
147-
Object.defineProperty(compiled.css, '__meta', {
148-
value: { hasUnscopedGlobalCss: true },
149-
writable: false,
150-
enumerable: false,
151-
configurable: false
152-
});
153-
stop();
154-
}
136+
137+
// check if css has unscoped :global.
138+
// This is later used to decide if css output can be scoped to the js module for treeshaking
139+
if (compiled.css && compiled.ast.css && finalCode.includes(':global')) {
140+
walk(
141+
compiled.ast.css,
142+
{},
143+
{
144+
Selector(node, { stop }) {
145+
if (
146+
node.children?.[0].type === 'PseudoClassSelector' &&
147+
node.children[0].name === 'global'
148+
) {
149+
Object.defineProperty(compiled.css, '__meta', {
150+
value: { hasUnscopedGlobalCss: true },
151+
writable: false,
152+
enumerable: false,
153+
configurable: false
154+
});
155+
stop();
155156
}
156157
}
157-
);
158-
}
158+
}
159+
);
159160
}
160161

161162
// patch output with partial accept until svelte does it

0 commit comments

Comments
 (0)