import assert from 'node:assert/strict'; import test from 'node:test'; import vm from 'node:vm'; import legacyCssLayers, { flattenCssLayers, } from '../scripts/vite/legacy-css-layers.js'; // ---------- flattenCssLayers ---------- test('layers are unwrapped and unlayered rules are left out', () => { const css = flattenCssLayers([ 'body{color:red}@layer utils{.w-full{width:100%}}.card{padding:0}', ]); assert.equal(css, '.w-full{width:100%}'); }); test('output follows layer order, not block position', () => { const css = flattenCssLayers([ '@layer base,semi,utils;@layer utils{.u{top:0}}@layer base{.b{top:0}}@layer semi{.s{top:0}}', ]); assert.equal(css, '.b{top:0}.s{top:0}.u{top:0}'); }); // 线上的真实情形:semi-ui.css 的 排在 index.css 之前,semi 层因此先于 // index.css 里 `@layer tailwind-base, semi, ...;` 的声明出现,成为最低优先级层 test('layer order is decided by first appearance across stylesheets', () => { const css = flattenCssLayers([ '@layer semi{.s1{top:0}}@layer semi{.s2{top:0}}', '@layer base,semi,utils;@layer base{.b{top:0}}@layer utils{.u{top:0}}', ]); assert.equal(css, '.s1{top:0}.s2{top:0}.b{top:0}.u{top:0}'); }); test('anonymous layers stay separate and keep their position', () => { const css = flattenCssLayers([ '@layer{.a{top:0}}@layer x{.x{top:0}}@layer{.b{top:0}}', ]); assert.equal(css, '.a{top:0}.x{top:0}.b{top:0}'); }); test('at-rules inside a layer are carried over intact', () => { const body = '@media (min-width:1px){.m{top:0}}@keyframes spin{to{top:0}}.last{top:0}'; assert.equal(flattenCssLayers([`@layer utils{${body}}`]), body); }); test('braces inside strings and escaped selectors do not break block matching', () => { const rule = ".content-\\[\\'\\}\\'\\]{content:'}'}"; assert.equal( flattenCssLayers([`@layer utils{${rule}.after{top:0}}`]), `${rule}.after{top:0}`, ); }); test('stylesheets without layers produce nothing', () => { assert.equal(flattenCssLayers(['body{color:red}', '']), ''); }); // Semi 的 @layer 副本与层外全量引入的 semi.css 逐字重复,是兜底表体积的大头 test('layered rules that also exist verbatim outside any layer are dropped', () => { const css = flattenCssLayers([ '.semi-a{top:0}@media (min-width:1px){.semi-m{top:0}}@layer semi{.semi-a{top:0}.semi-only{top:0}@media (min-width:1px){.semi-m{top:0}}}', '@layer utils{.semi-late{top:0}.u{top:0}}.semi-late{top:0}', ]); assert.equal(css, '.semi-only{top:0}.u{top:0}'); }); // 线上 semi-icons 的层内规则正是靠另一份入口表里的副本剔掉的,先后顺序无关 test('the verbatim copy may live in a different stylesheet, before or after', () => { const layered = '@layer semi{.s{top:0}.t{top:0}}'; assert.equal(flattenCssLayers(['.s{top:0}', layered]), '.t{top:0}'); assert.equal(flattenCssLayers([layered, '.s{top:0}']), '.t{top:0}'); }); test('a layered rule is kept when the unlayered one only looks similar', () => { const css = flattenCssLayers([ '.a{top:0}@layer x{.a{top:1px}.a:hover{top:0}}', ]); assert.equal(css, '.a{top:1px}.a:hover{top:0}'); }); // 剔重只比较顶层节点:条件块里的同文规则只在条件成立时生效,不能算副本; // 声明多一个 !important 也不是同一条规则 test('a rule inside a conditional block or differing in !important is not a copy', () => { assert.equal( flattenCssLayers(['@media print{.a{top:0}}@layer x{.a{top:0}}']), '.a{top:0}', ); assert.equal( flattenCssLayers(['.a{top:0}@layer x{.a{top:0!important}}']), '.a{top:0!important}', ); }); // at 关键字大小写不敏感,@LAYER 与 @layer 是同一个东西 test('at-rule names are matched case-insensitively', () => { assert.equal( flattenCssLayers(['@LAYER a{.x{top:0}}@Layer b{.y{top:0}}']), '.x{top:0}.y{top:0}', ); }); // 这些写法的优先级规则各不相同,压平逻辑没有覆盖;宁可构建失败也不悄悄排错顺序 test('layer shapes the flattener does not understand fail loudly', () => { for (const css of [ '@layer a{.direct{top:0}@layer b{.nested{top:0}}}', '@layer a.b{.x{top:0}}', '@layer a.b,c;', '@media (min-width:1px){@layer a{.x{top:0}}}', '@supports (top:0){@layer a{.x{top:0}}}', '@import url(theme.css) layer(theme);', '@import "theme.css" layer;', '@layer a{@LAYER b{.nested{top:0}}}', '@IMPORT url(theme.css) LAYER(theme);', ]) { assert.throws(() => flattenCssLayers([css]), /legacy-css-layers/, css); } }); test('an @import whose file name merely contains "layer" is not rejected', () => { assert.equal( flattenCssLayers([ '@import url(https://cdn.example/layer.css);@import "layer/base.css";@layer x{.x{top:0}}', ]), '.x{top:0}', ); }); // ---------- 插件:往 index.html 注入兜底 ---------- const LEGACY_FILE = 'assets/legacy-layers-HASH.css'; const link = (href) => ``; const page = (...links) => `