Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css): escape pattern chars from base path in postcss dir-dependency messages #7081

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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