Skip to content

Commit

Permalink
fix(css): escape pattern chars from base path in postcss dir-dependen…
Browse files Browse the repository at this point in the history
…cy messages (#7081)

Co-authored-by: sapphi-red <green@sapphi.red>
  • Loading branch information
thecrypticace and sapphi-red committed Jun 14, 2022
1 parent b1baca7 commit 5151e74
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -861,7 +861,9 @@ async function compileCSS(
// https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
const { dir, glob: globPattern = '**' } = message
const pattern =
normalizePath(path.resolve(path.dirname(id), dir)) + `/` + globPattern
glob.escapePath(normalizePath(path.resolve(path.dirname(id), dir))) +
`/` +
globPattern
const files = glob.sync(pattern, {
ignore: ['**/node_modules/**']
})
Expand Down
9 changes: 9 additions & 0 deletions playground/css/__tests__/css.spec.ts
Expand Up @@ -317,9 +317,11 @@ test('treeshaken async chunk', async () => {
test('PostCSS dir-dependency', async () => {
const el1 = await page.$('.dir-dep')
const el2 = await page.$('.dir-dep-2')
const el3 = await page.$('.dir-dep-3')

expect(await getColor(el1)).toBe('grey')
expect(await getColor(el2)).toBe('grey')
expect(await getColor(el3)).toBe('grey')

if (!isBuild) {
editFile('glob-dep/foo.css', (code) =>
Expand All @@ -334,6 +336,13 @@ test('PostCSS dir-dependency', async () => {
await untilUpdated(() => getColor(el2), 'red')
expect(await getColor(el1)).toBe('blue')

editFile('glob-dep/nested (dir)/baz.css', (code) =>
code.replace('color: grey', 'color: green')
)
await untilUpdated(() => getColor(el3), 'green')
expect(await getColor(el1)).toBe('blue')
expect(await getColor(el2)).toBe('red')

// test add/remove
removeFile('glob-dep/bar.css')
await untilUpdated(() => getColor(el2), 'black')
Expand Down
3 changes: 3 additions & 0 deletions playground/css/glob-dep/nested (dir)/baz.css
@@ -0,0 +1,3 @@
.dir-dep-3 {
color: grey;
}
3 changes: 3 additions & 0 deletions playground/css/index.html
Expand Up @@ -113,6 +113,9 @@ <h1>CSS</h1>
<p class="dir-dep-2">
PostCSS dir-dependency (file 2): this should be grey too
</p>
<p class="dir-dep-3">
PostCSS dir-dependency (file 3): this should be grey too
</p>

<p class="url-separated">
URL separation preservation: should have valid background-image
Expand Down
10 changes: 9 additions & 1 deletion playground/css/postcss.config.js
Expand Up @@ -16,7 +16,7 @@ function testDirDep() {
AtRule(atRule, { result, Comment }) {
if (atRule.name === 'test') {
const pattern = normalizePath(
path.resolve(path.dirname(result.opts.from), './glob-dep/*.css')
path.resolve(path.dirname(result.opts.from), './glob-dep/**/*.css')
)
const files = glob.sync(pattern)
const text = files.map((f) => fs.readFileSync(f, 'utf-8')).join('\n')
Expand All @@ -30,6 +30,14 @@ function testDirDep() {
glob: '*.css',
parent: result.opts.from
})

result.messages.push({
type: 'dir-dependency',
plugin: 'dir-dep',
dir: './glob-dep/nested (dir)', // includes special characters in glob
glob: '*.css',
parent: result.opts.from
})
}
}
}
Expand Down

0 comments on commit 5151e74

Please sign in to comment.