Skip to content

Commit

Permalink
fix: CSS dependencies are tracked incorrectly when base is set (#4592)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Aug 17, 2021
1 parent b572f57 commit 633c03a
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 11 deletions.
Expand Up @@ -32,15 +32,28 @@ if (isBuild) {
expect(htmlEntry.assets.length).toEqual(1)
})
} else {
test('preserve the base in CSS HMR', async () => {
await untilUpdated(() => getColor('body'), 'black') // sanity check
editFile('frontend/entrypoints/global.css', (code) =>
code.replace('black', 'red')
)
await untilUpdated(() => getColor('body'), 'red') // successful HMR
describe('CSS HMR', () => {
test('preserve the base in CSS HMR', async () => {
await untilUpdated(() => getColor('body'), 'black') // sanity check
editFile('frontend/entrypoints/global.css', (code) =>
code.replace('black', 'red')
)
await untilUpdated(() => getColor('body'), 'red') // successful HMR

// Verify that the base (/dev/) was added during the css-update
const link = await page.$('link[rel="stylesheet"]')
expect(await link.getAttribute('href')).toContain('/dev/global.css?t=')
// Verify that the base (/dev/) was added during the css-update
const link = await page.$('link[rel="stylesheet"]')
expect(await link.getAttribute('href')).toContain('/dev/global.css?t=')
})

test('CSS dependencies are tracked for HMR', async () => {
const el = await page.$('h1')
browserLogs.length = 0

editFile('frontend/entrypoints/main.ts', (code) =>
code.replace('text-black', 'text-[rgb(204,0,0)]')
)
await untilUpdated(() => getColor(el), 'rgb(204, 0, 0)')
expect(browserLogs).toContain('[vite] css hot updated: /global.css')
})
})
}
@@ -1,4 +1,5 @@
@import '~/styles/background.css';
@import '~/styles/tailwind.css';
@import '../../references.css';

html,
Expand Down
Expand Up @@ -2,7 +2,7 @@

<link rel="stylesheet" href="/global.css" />

<h1>Backend Integration</h1>
<h1 class="text-black">Backend Integration</h1>

<p>
This test configures the <code>root</code> to simulate a Laravel/Rails setup.
Expand All @@ -27,6 +27,7 @@ <h2>CSS Asset References</h2>
</li>
</ul>

<script type="module" src="./main.ts"></script>
<script type="module">
import './global.css'

Expand Down
@@ -0,0 +1,11 @@
export const colorClass = 'text-black'

export function colorHeading() {
document.querySelector('h1').className = colorClass
}

colorHeading()

if (import.meta.hot) {
import.meta.hot.accept()
}
@@ -0,0 +1 @@
@tailwind utilities;
3 changes: 3 additions & 0 deletions packages/playground/backend-integration/package.json
Expand Up @@ -7,5 +7,8 @@
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"serve": "vite preview"
},
"dependencies": {
"tailwindcss": "^2.2.4"
}
}
6 changes: 6 additions & 0 deletions packages/playground/backend-integration/postcss.config.js
@@ -0,0 +1,6 @@
// postcss.config.js
module.exports = {
plugins: {
tailwindcss: { config: __dirname + '/tailwind.config.js' }
}
}
12 changes: 12 additions & 0 deletions packages/playground/backend-integration/tailwind.config.js
@@ -0,0 +1,12 @@
module.exports = {
mode: 'jit',
purge: [__dirname + '/frontend/**/*.{css,html,ts,js}'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {}
},
variants: {
extend: {}
},
plugins: []
}
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -215,7 +215,9 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
isCSSRequest(file)
? moduleGraph.createFileOnlyEntry(file)
: await moduleGraph.ensureEntryFromUrl(
await fileToUrl(file, config, this)
(
await fileToUrl(file, config, this)
).replace(config.base, '/')
)
)
}
Expand Down

0 comments on commit 633c03a

Please sign in to comment.