Skip to content
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 62 additions & 52 deletions src/processors/native.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { walk } from 'estree-walker';
import type { AST } from 'svelte/compiler';
import type { PluginOptions } from '../types';
import Processor from './processor';
Expand All @@ -21,7 +20,7 @@ const updateSelectorBoundaries = (
const lastIndex = selectorBoundaries.length - 1;
if (selectorBoundaries[lastIndex]?.end === start) {
selectorBoundaries[lastIndex].end = end;
} else if (selectorBoundaries.length < 1 || selectorBoundaries[lastIndex].end < end) {
} else {
selectorBoundaries.push({ start, end });
}
return selectorBoundaries;
Expand All @@ -38,66 +37,77 @@ const parser = (processor: Processor): void => {

let selectorBoundaries: Boundaries[] = [];

walk(processor.ast.css, {
enter(baseNode) {
(baseNode as AST.CSS.StyleSheet).children?.forEach((node) => {
if (node.type === 'Atrule' && node.name === 'keyframes') {
processor.parseKeyframes(node);
this.skip();
}
if (node.type === 'Rule') {
node.prelude.children.forEach((child) => {
if (child.type === 'ComplexSelector') {
let start = 0;
let end = 0;
const visitNode = (node: AST.CSS.Node): void => {
if (node.type === 'Atrule') {
if (node.name === 'keyframes') {
processor.parseKeyframes(node);
return;
}
// Recurse into @media, @supports, @layer, etc.
(node.block as AST.CSS.Block)?.children?.forEach(visitNode);
return;
}

child.children.forEach((grandChild, index) => {
let hasPushed = false;
if (grandChild.type === 'RelativeSelector') {
grandChild.selectors.forEach((item) => {
if (
item.type === 'PseudoClassSelector' &&
(item.name === 'global' || item.name === 'local')
) {
processor.parsePseudoLocalSelectors(item);
if (start > 0 && end > 0) {
selectorBoundaries = updateSelectorBoundaries(
selectorBoundaries,
start,
end
);
hasPushed = true;
}
start = item.end + 1;
end = 0;
} else if (item.start && item.end) {
if (start === 0) {
start = item.start;
}
end = item.end;
processor.parseClassSelectors(item);
}
});
if (node.type === 'Rule') {
node.prelude.children.forEach((child) => {
if (child.type === 'ComplexSelector') {
let start = 0;
let end = 0;

if (
hasPushed === false &&
child.children &&
index === child.children.length - 1 &&
end > 0
) {
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
child.children.forEach((grandChild, index) => {
let hasPushed = false;
if (grandChild.type === 'RelativeSelector') {
grandChild.selectors.forEach((item) => {
if (
item.type === 'PseudoClassSelector' &&
(item.name === 'global' || item.name === 'local')
) {
processor.parsePseudoLocalSelectors(item);
if (start > 0 && end > 0) {
selectorBoundaries = updateSelectorBoundaries(
selectorBoundaries,
start,
end
);
hasPushed = true;
}
start = item.end + 1;
end = 0;
} else if (item.start && item.end) {
if (start === 0) {
start = item.start;
}
end = item.end;
processor.parseClassSelectors(item);
}
});

if (
hasPushed === false &&
child.children &&
index === child.children.length - 1 &&
end > 0
) {
selectorBoundaries = updateSelectorBoundaries(selectorBoundaries, start, end);
}
}
});
}
});

processor.parseBoundVariables(node.block);
processor.storeAnimationProperties(node.block);

processor.parseBoundVariables(node.block);
processor.storeAnimationProperties(node.block);
// Recurse into nested rules (CSS nesting: rules inside rule blocks)
(node.block as AST.CSS.Block)?.children?.forEach((child) => {
if (child.type === 'Rule' || child.type === 'Atrule') {
visitNode(child);
}
});
},
});
}
};

processor.ast.css?.children?.forEach(visitNode);

processor.overwriteAnimationProperties();

Expand Down
80 changes: 80 additions & 0 deletions test/globalFixtures/keyframes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,84 @@ describe('Scoped Keyframes', () => {

expect(output).toBe(expectedOutput);
});

test('Native mode: class inside @media is hashed when @keyframes is also present', async () => {
const source =
'<style module>' +
'.foo { color: red; }' +
'@media (forced-colors: active) {' +
'.foo { color: CanvasText; }' +
'}' +
'@keyframes spin {' +
'from { transform: rotate(0deg); }' +
'to { transform: rotate(360deg); }' +
'}' +
'</style>' +
'<div class="foo"></div>';

const expectedOutput =
'<style module>' +
':global(.foo-123) { color: red; }' +
'@media (forced-colors: active) {' +
':global(.foo-123) { color: CanvasText; }' +
'}' +
'@keyframes -global-spin-123 {' +
'from { transform: rotate(0deg); }' +
'to { transform: rotate(360deg); }' +
'}' +
'</style>' +
'<div class="foo-123"></div>';

const output = await compiler(
{
source,
},
{
mode: 'native',
localIdentName: '[local]-123',
}
);

expect(output).toBe(expectedOutput);
});

test('Native mode: class inside @media after @keyframes is hashed', async () => {
const source =
'<style module>' +
'.bar { color: blue; }' +
'@keyframes pulse {' +
'from { opacity: 1; }' +
'to { opacity: 0; }' +
'}' +
'@media (prefers-reduced-motion: no-preference) {' +
'.bar { animation: pulse 1s ease-in-out; }' +
'}' +
'</style>' +
'<span class="bar">Bar</span>';

const expectedOutput =
'<style module>' +
':global(.bar-123) { color: blue; }' +
'@keyframes -global-pulse-123 {' +
'from { opacity: 1; }' +
'to { opacity: 0; }' +
'}' +
'@media (prefers-reduced-motion: no-preference) {' +
':global(.bar-123) { animation: pulse-123 1s ease-in-out; }' +
'}' +
'</style>' +
'<span class="bar-123">Bar</span>';

const output = await compiler(
{
source,
},
{
mode: 'native',
localIdentName: '[local]-123',
}
);

expect(output).toBe(expectedOutput);
});
});
Loading