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

test: css addWatchFile in load hook #18103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,11 +999,20 @@ if (!isBuild) {
await untilUpdated(() => el.evaluate((it) => `${it.clientHeight}`), '40')
})

test('CSS HMR with this.addWatchFile', async () => {
test('CSS HMR with this.addWatchFile in transform hook', async () => {
await page.goto(viteTestUrl + '/css-deps/index.html')
expect(await getColor('.css-deps')).toMatch('red')
editFile('css-deps/dep.js', (code) => code.replace(`red`, `green`))
await untilUpdated(() => getColor('.css-deps'), 'green')
expect(await getColor('.css-deps-transform')).toMatch('red')
editFile('css-deps/dep-transform.js', (code) =>
code.replace(`red`, `green`),
)
await untilUpdated(() => getColor('.css-deps-transform'), 'green')
})

test('CSS HMR with this.addWatchFile in load hook', async () => {
await page.goto(viteTestUrl + '/css-deps/index.html')
expect(await getColor('.css-deps-load')).toMatch('red')
editFile('css-deps/dep-load.js', (code) => code.replace(`red`, `green`))
await untilUpdated(() => getColor('.css-deps-load'), 'green')
})

test('hmr should happen after missing file is created', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file is depended by main.css via this.addWatchFile
// This file is depended by main-load.css via this.addWatchFile
export const color = 'red'

// Self-accept so that updating this file would not trigger a page reload.
Expand Down
8 changes: 8 additions & 0 deletions playground/hmr/css-deps/dep-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is depended by main-transform.css via this.addWatchFile
export const color = 'red'

// Self-accept so that updating this file would not trigger a page reload.
// We only want to observe main.css updating itself.
if (import.meta.hot) {
import.meta.hot.accept()
}
9 changes: 6 additions & 3 deletions playground/hmr/css-deps/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<div class="css-deps">should be red</div>
<div class="css-deps-transform">should be red</div>
<div class="css-deps-load">should be red</div>

<script type="module">
import './main.css'
import './main-transform.css'
import 'virtual:css-deps-load.css'
// Import dep.js so that not only the CSS depends on dep.js, as Vite will do
// a full page reload if the only importers are CSS files.
import './dep.js'
import './dep-transform.js'
import './dep-load.js'
</script>
3 changes: 3 additions & 0 deletions playground/hmr/css-deps/main-transform.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.css-deps-transform {
color: replaced;
}
3 changes: 0 additions & 3 deletions playground/hmr/css-deps/main.css

This file was deleted.

44 changes: 39 additions & 5 deletions playground/hmr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default defineConfig({
},
virtualPlugin(),
transformCountPlugin(),
watchCssDepsPlugin(),
watchCssDepsTransformPlugin(),
watchCssDepsLoadPlugin(),
],
})

Expand Down Expand Up @@ -76,14 +77,15 @@ function transformCountPlugin(): Plugin {
}
}

function watchCssDepsPlugin(): Plugin {
// `addWatchFile` called from `transform` hook
function watchCssDepsTransformPlugin(): Plugin {
return {
name: 'watch-css-deps',
name: 'watch-css-deps-transform',
async transform(code, id) {
// replace the `replaced` identifier in the CSS file with the adjacent
// `dep.js` file's `color` variable.
if (id.includes('css-deps/main.css')) {
const depPath = path.resolve(__dirname, './css-deps/dep.js')
if (id.includes('css-deps/main-transform.css')) {
const depPath = path.resolve(__dirname, './css-deps/dep-transform.js')
const dep = await fs.readFile(depPath, 'utf-8')
const color = dep.match(/color = '(.+?)'/)[1]
this.addWatchFile(depPath)
Expand All @@ -92,3 +94,35 @@ function watchCssDepsPlugin(): Plugin {
},
}
}

// `addWatchFile` called from `load` hook
function watchCssDepsLoadPlugin(): Plugin {
return {
name: 'watch-css-deps-load',
resolveId(id) {
if (id === 'virtual:css-deps-load.css') {
return '\0virtual:css-deps-load.css'
}
},
async load(id) {
if (id === '\0virtual:css-deps-load.css') {
const depPath = path.resolve(__dirname, './css-deps/dep.js')
this.addWatchFile(depPath)
return `\
.css-deps-load {
color: replaced;
}`
}
},
async transform(code, id) {
// replace the `replaced` identifier in the CSS file with the adjacent
// `dep.js` file's `color` variable.
if (id === '\0virtual:css-deps-load.css') {
const depPath = path.resolve(__dirname, './css-deps/dep-load.js')
const dep = await fs.readFile(depPath, 'utf-8')
const color = dep.match(/color = '(.+?)'/)[1]
return code.replace('replaced', color)
}
},
}
}
Loading