Skip to content

Commit

Permalink
feat: set nameLayer option to postcss-import
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Aug 31, 2022
1 parent 682a746 commit 064b5f6
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -848,6 +848,9 @@ async function compileCSS(
return path.resolve(resolved)
}
return id
},
nameLayer(index) {
return `anon-layer-${getHash(id)}-${index}`

This comment has been minimized.

Copy link
@patak-dev

patak-dev Aug 31, 2022

Member

Should we use something like --vite-anon-layer-... here? (or vite--anon-layer-...)

This comment has been minimized.

Copy link
@sapphi-red

sapphi-red Aug 31, 2022

Author Member

That sounds better.
While it seems it's valid to use --vite-anon-layer-..., I prefer vite--anon-layer-... and will change to it.

https://www.w3.org/TR/css-cascade-5/#layer-names
https://www.w3.org/TR/css-syntax-3/#ident-token-diagram

}
})
)
Expand Down
1 change: 1 addition & 0 deletions packages/vite/types/shims.d.ts
Expand Up @@ -47,6 +47,7 @@ declare module 'postcss-import' {
basedir: string,
importOptions: any
) => string | string[] | Promise<string | string[]>
nameLayer: (index: number, rootFilename: string) => string
}) => Plugin
export = plugin
}
Expand Down
5 changes: 5 additions & 0 deletions playground/css/__tests__/css.spec.ts
Expand Up @@ -259,6 +259,11 @@ test.runIf(isBuild)('@charset hoist', async () => {
})
})

test('layers', async () => {
expect(await getColor('.layers-blue')).toMatch('blue')
expect(await getColor('.layers-green')).toMatch('green')
})

test('@import dependency w/ style entry', async () => {
expect(await getColor('.css-dep')).toBe('purple')
})
Expand Down
6 changes: 6 additions & 0 deletions playground/css/index.html
Expand Up @@ -99,6 +99,12 @@ <h1>CSS</h1>
<p>CSS with @charset:</p>
<pre class="charset-css"></pre>

<p>
@import with layers:
<span class="layers-blue">blue</span>
<span class="layers-green">green</span>
</p>

<p class="css-dep">
@import dependency w/ style entrypoints: this should be purple
</p>
Expand Down
5 changes: 5 additions & 0 deletions playground/css/layered/blue.css
@@ -0,0 +1,5 @@
@media screen {
.layers-blue {
color: blue;
}
}
5 changes: 5 additions & 0 deletions playground/css/layered/green.css
@@ -0,0 +1,5 @@
@media screen {
.layers-green {
color: green;
}
}
13 changes: 13 additions & 0 deletions playground/css/layered/index.css
@@ -0,0 +1,13 @@
@layer base;

@import './blue.css' layer;
@import './green.css' layer;

@layer base {
.layers-blue {
color: black;
}
.layers-green {
color: black;
}
}
2 changes: 2 additions & 0 deletions playground/css/main.js
Expand Up @@ -44,6 +44,8 @@ text('.modules-inline', inlineMod)
import charset from './charset.css'
text('.charset-css', charset)

import './layered/index.css'

import './dep.css'
import './glob-dep.css'

Expand Down

0 comments on commit 064b5f6

Please sign in to comment.