Skip to content

Commit 68b2bd9

Browse files
dev server: handle hmr and css importer persistence (#7389)
* handle hmr and css importer persistence * changeset
1 parent c51aaab commit 68b2bd9

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

.changeset/quiet-flowers-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik': patch
3+
---
4+
5+
dev server now correctly handles css and js importers, also hmr persistence

packages/qwik/src/optimizer/src/plugins/vite-dev-server.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export async function configureDevServer(
8585
const hasQwikCity = server.config.plugins?.some(
8686
(plugin) => plugin.name === 'vite-plugin-qwik-city'
8787
);
88+
89+
// to maintain css importers after HMR
90+
const cssImportedByCSS = new Set<string>();
91+
8892
// qwik middleware injected BEFORE vite internal middlewares
8993
server.middlewares.use(async (req, res, next) => {
9094
try {
@@ -159,12 +163,30 @@ export async function configureDevServer(
159163

160164
if (query === '' && CSS_EXTENSIONS.some((ext) => pathId.endsWith(ext))) {
161165
const isEntryCSS = v.importers.size === 0;
166+
const hasCSSImporter = Array.from(v.importers).some((importer) => {
167+
const importerPath = (importer as typeof v).url || (importer as typeof v).file;
168+
169+
const isCSS =
170+
importerPath && CSS_EXTENSIONS.some((ext) => importerPath.endsWith(ext));
171+
172+
if (isCSS && v.url) {
173+
cssImportedByCSS.add(v.url);
174+
}
175+
176+
return isCSS;
177+
});
178+
162179
const hasJSImporter = Array.from(v.importers).some((importer) => {
163180
const importerPath = (importer as typeof v).url || (importer as typeof v).file;
164181
return importerPath && JS_EXTENSIONS.test(importerPath);
165182
});
166183

167-
if ((isEntryCSS || hasJSImporter) && !added.has(v.url)) {
184+
if (
185+
(isEntryCSS || hasJSImporter) &&
186+
!hasCSSImporter &&
187+
!cssImportedByCSS.has(v.url) &&
188+
!added.has(v.url)
189+
) {
168190
added.add(v.url);
169191
manifest.injections!.push({
170192
tag: 'link',
@@ -214,12 +236,29 @@ export async function configureDevServer(
214236
CSS_EXTENSIONS.some((ext) => pathId.endsWith(ext))
215237
) {
216238
const isEntryCSS = v.importers.size === 0;
239+
const hasCSSImporter = Array.from(v.importers).some((importer) => {
240+
const importerPath = (importer as typeof v).url || (importer as typeof v).file;
241+
242+
const isCSS =
243+
importerPath && CSS_EXTENSIONS.some((ext) => importerPath.endsWith(ext));
244+
245+
if (isCSS && v.url) {
246+
cssImportedByCSS.add(v.url);
247+
}
248+
249+
return isCSS;
250+
});
251+
217252
const hasJSImporter = Array.from(v.importers).some((importer) => {
218253
const importerPath = (importer as typeof v).url || (importer as typeof v).file;
219254
return importerPath && JS_EXTENSIONS.test(importerPath);
220255
});
221256

222-
if (isEntryCSS || hasJSImporter) {
257+
if (
258+
(isEntryCSS || hasJSImporter) &&
259+
!hasCSSImporter &&
260+
!cssImportedByCSS.has(v.url)
261+
) {
223262
res.write(`<link rel="stylesheet" href="${base}${v.url.slice(1)}">`);
224263
added.add(v.url);
225264
}

0 commit comments

Comments
 (0)