Commit 6f749ab
committed
refactor handling of full reload logic
When you use the `@source` directive in your CSS file, then internally
we will call `addWatchFile` for each of the matching files. This way
Vite will watch these files and trigger the `@tailwindcss/vite` plugin
when things change.
However, with external files we need to make sure that we trigger a full
page reload to see any effect. For example when a .php file changed. The
default behavior in Vite would be to trigger a hot update of the
index.css file. Which is correct for updating the CSS, but since the
.php file is just an asset it won't have any effect on the current page.
The original logic assumed that "asset" modules (any module with type of
asset and/or id=undefined) are not in the module graph, but in my
testing it looks like they are part of the module graph (because we call
`addWatchFile` on these files).
This means that we need another way of knowing whether or not the module
should result in a full page reload or not.
To do this, the `handleHotUpdate` receives an array of modules which we
need to analyze. For example a .js file will have a type of `js` in one
module, but it will also have a type of `asset` in the other module.
This is because of the `addWatchFile` that we added.
Now, the `handleHotUpdate` is going to be deprecated soon, so we also
updated to the `hotUpdate` API instead.
If only some of the modules are asset modules, then we can let Vite do
its job, aka do a hot module replacement. If all of the modules are
assets, then we have to determine whether the file is part of any of the
CSS file sources.
We already have a concept of a "root" which maps to a CSS file handled
by Tailwind CSS and we now also expose the scanned files so we can
match these against the file that was updated.
These roots are indexed by environment, which is one reason why we
switched to the `hotUpdate` API because `this.environment` is available
there.
To be 100% sure, we check the current file and walk up the tree
(importers) to see if any of its files is part of a Tailwind CSS root.
Once we reach a root and if that root has a scanned file of the current
file that changed, then we know to do a full page reload.
I don't think it matters _which_ root it belonged to because we will
perform a full reload anyway. I guess in theory if we changed a file
where its contents is currently not on the page then maybe we shouldn't
reload, but we can improve that if this is causing issues later.1 parent e2cbf8b commit 6f749ab
1 file changed
+98
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | 99 | | |
122 | 100 | | |
123 | 101 | | |
| |||
174 | 152 | | |
175 | 153 | | |
176 | 154 | | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
177 | 213 | | |
178 | 214 | | |
179 | 215 | | |
| |||
294 | 330 | | |
295 | 331 | | |
296 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
297 | 337 | | |
298 | 338 | | |
299 | 339 | | |
| |||
475 | 515 | | |
476 | 516 | | |
477 | 517 | | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
0 commit comments