Skip to content

Commit ca053ec

Browse files
committed
Don't layer onchainkit
1 parent d3902e2 commit ca053ec

File tree

2 files changed

+56
-35
lines changed

2 files changed

+56
-35
lines changed

packages/onchainkit/plugins/__tests__/postcss-create-scoped-styles.test.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ describe('postcssCreateScopedStyles', () => {
9191
`;
9292

9393
const output = await runPlugin(input, { consolidateLayers: true });
94-
expect(output).toContain('@layer onchainkit');
94+
expect(output).not.toContain('@layer onchainkit');
95+
expect(output).not.toContain('@layer theme');
96+
expect(output).not.toContain('@layer base');
9597
expect(output).toContain('Theme section');
9698
expect(output).toContain('Base section');
9799
});
@@ -378,7 +380,9 @@ describe('postcssCreateScopedStyles', () => {
378380
`;
379381

380382
const output = await runPlugin(input, { consolidateLayers: true });
381-
expect(output).toContain('@layer onchainkit');
383+
expect(output).not.toContain('@layer onchainkit');
384+
expect(output).not.toContain('@layer theme, base, utilities');
385+
expect(output).toContain('Theme section');
382386
});
383387

384388
it('should handle multiple imports', async () => {
@@ -508,7 +512,28 @@ describe('postcssCreateScopedStyles', () => {
508512
`;
509513

510514
const output = await runPlugin(input, { consolidateLayers: true });
511-
expect(output).toContain('@layer onchainkit');
515+
expect(output).not.toContain('@layer onchainkit');
516+
expect(output).not.toContain('@layer properties');
512517
expect(output).toContain('Properties section');
513518
});
519+
520+
it('should insert layers after imports when consolidating', async () => {
521+
const input = `
522+
@import "first.css";
523+
@layer theme {
524+
.theme { color: blue; }
525+
}
526+
@layer base {
527+
.base { margin: 0; }
528+
}
529+
`;
530+
531+
const output = await runPlugin(input, { consolidateLayers: true });
532+
const lines = output.trim().split('\n');
533+
expect(lines[0]).toContain('@import');
534+
expect(output).toContain('Theme section');
535+
expect(output).toContain('Base section');
536+
expect(output).not.toContain('@layer theme');
537+
expect(output).not.toContain('@layer base');
538+
});
514539
});

packages/onchainkit/plugins/postcss-create-scoped-styles.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -101,32 +101,11 @@ function moveImportsToTop(root: postcss.Root) {
101101
atRule.remove();
102102
});
103103

104-
// Add imports at the beginning of the file with proper formatting
104+
// Add imports at the beginning of the file
105105
if (imports.length > 0) {
106106
// Process imports in reverse order since we're prepending
107107
for (let i = imports.length - 1; i >= 0; i--) {
108-
const importRule = imports[i];
109-
110-
// Set the raws for this specific import
111-
if (i === 0) {
112-
// First import (will be at the top after prepend)
113-
importRule.raws.before = '';
114-
importRule.raws.after = ';'; // Ensure semicolon
115-
} else {
116-
// Other imports
117-
importRule.raws.before = '';
118-
importRule.raws.after = ';\n'; // Semicolon + newline
119-
}
120-
121-
root.prepend(importRule);
122-
}
123-
124-
// Add spacing after all imports by modifying the next node
125-
if (root.nodes.length > imports.length) {
126-
const firstNonImport = root.nodes[imports.length];
127-
if (firstNonImport) {
128-
firstNonImport.raws.before = '\n\n';
129-
}
108+
root.prepend(imports[i]);
130109
}
131110
}
132111
}
@@ -160,31 +139,48 @@ function consolidateAllLayers(root: postcss.Root) {
160139
}
161140
});
162141

163-
// Create single consolidated layer with all content in correct order
142+
// Insert layer contents directly into root without wrapper
164143
if (Object.keys(layerContents).length > 0) {
165-
const consolidatedLayer = postcss.atRule({
166-
name: 'layer',
167-
params: 'onchainkit',
144+
// Find where to insert content (after imports)
145+
let insertAfterNode: postcss.ChildNode | null = null;
146+
root.each((node) => {
147+
if (node.type === 'atrule' && node.name === 'import') {
148+
insertAfterNode = node;
149+
}
168150
});
169151

170-
// Add contents in the correct order with section comments
152+
// Build all nodes to insert
153+
const nodesToInsert: postcss.ChildNode[] = [];
154+
171155
layerOrder.forEach((layerName) => {
172156
if (layerContents[layerName]) {
173157
// Add section comment
174158
const comment = postcss.comment({
175159
text: ` ${layerName.charAt(0).toUpperCase() + layerName.slice(1)} section `,
176160
});
177-
consolidatedLayer.append(comment);
161+
162+
nodesToInsert.push(comment);
178163

179164
// Add all rules from this layer
180165
layerContents[layerName].forEach((node) => {
181-
consolidatedLayer.append(node);
166+
nodesToInsert.push(node);
182167
});
183168
}
184169
});
185170

186-
// Insert the consolidated layer at the beginning of the file
187-
root.prepend(consolidatedLayer);
171+
// Insert all nodes after imports (or at beginning if no imports)
172+
if (insertAfterNode) {
173+
nodesToInsert.reverse().forEach((node) => {
174+
insertAfterNode!.after(node);
175+
});
176+
} else {
177+
nodesToInsert.reverse().forEach((node) => {
178+
root.prepend(node);
179+
});
180+
}
181+
182+
// Format the inserted content by cleaning up the root
183+
root.cleanRaws();
188184
}
189185
}
190186

0 commit comments

Comments
 (0)