Skip to content

Commit 5151e74

Browse files
thecrypticacesapphi-red
andauthoredJun 14, 2022
fix(css): escape pattern chars from base path in postcss dir-dependency messages (#7081)
Co-authored-by: sapphi-red <green@sapphi.red>
1 parent b1baca7 commit 5151e74

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed
 

‎packages/vite/src/node/plugins/css.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,9 @@ async function compileCSS(
861861
// https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
862862
const { dir, glob: globPattern = '**' } = message
863863
const pattern =
864-
normalizePath(path.resolve(path.dirname(id), dir)) + `/` + globPattern
864+
glob.escapePath(normalizePath(path.resolve(path.dirname(id), dir))) +
865+
`/` +
866+
globPattern
865867
const files = glob.sync(pattern, {
866868
ignore: ['**/node_modules/**']
867869
})

‎playground/css/__tests__/css.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,11 @@ test('treeshaken async chunk', async () => {
317317
test('PostCSS dir-dependency', async () => {
318318
const el1 = await page.$('.dir-dep')
319319
const el2 = await page.$('.dir-dep-2')
320+
const el3 = await page.$('.dir-dep-3')
320321

321322
expect(await getColor(el1)).toBe('grey')
322323
expect(await getColor(el2)).toBe('grey')
324+
expect(await getColor(el3)).toBe('grey')
323325

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

339+
editFile('glob-dep/nested (dir)/baz.css', (code) =>
340+
code.replace('color: grey', 'color: green')
341+
)
342+
await untilUpdated(() => getColor(el3), 'green')
343+
expect(await getColor(el1)).toBe('blue')
344+
expect(await getColor(el2)).toBe('red')
345+
337346
// test add/remove
338347
removeFile('glob-dep/bar.css')
339348
await untilUpdated(() => getColor(el2), 'black')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.dir-dep-3 {
2+
color: grey;
3+
}

‎playground/css/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ <h1>CSS</h1>
113113
<p class="dir-dep-2">
114114
PostCSS dir-dependency (file 2): this should be grey too
115115
</p>
116+
<p class="dir-dep-3">
117+
PostCSS dir-dependency (file 3): this should be grey too
118+
</p>
116119

117120
<p class="url-separated">
118121
URL separation preservation: should have valid background-image

‎playground/css/postcss.config.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function testDirDep() {
1616
AtRule(atRule, { result, Comment }) {
1717
if (atRule.name === 'test') {
1818
const pattern = normalizePath(
19-
path.resolve(path.dirname(result.opts.from), './glob-dep/*.css')
19+
path.resolve(path.dirname(result.opts.from), './glob-dep/**/*.css')
2020
)
2121
const files = glob.sync(pattern)
2222
const text = files.map((f) => fs.readFileSync(f, 'utf-8')).join('\n')
@@ -30,6 +30,14 @@ function testDirDep() {
3030
glob: '*.css',
3131
parent: result.opts.from
3232
})
33+
34+
result.messages.push({
35+
type: 'dir-dependency',
36+
plugin: 'dir-dep',
37+
dir: './glob-dep/nested (dir)', // includes special characters in glob
38+
glob: '*.css',
39+
parent: result.opts.from
40+
})
3341
}
3442
}
3543
}

0 commit comments

Comments
 (0)
Please sign in to comment.