-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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(dev): only inject css link tags that have js importers #7267
base: main
Are you sure you want to change the base?
Changes from all commits
99948f1
e7d9823
677e768
6f2c921
8b41897
2acab11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ import clickToComponent from './click-to-component.html?raw'; | |
import errorHost from './error-host.html?raw'; | ||
import imageDevTools from './image-size-runtime.html?raw'; | ||
import perfWarning from './perf-warning.html?raw'; | ||
import { parseId, type NormalizedQwikPluginOptions } from './plugin'; | ||
import { type NormalizedQwikPluginOptions } from './plugin'; | ||
import type { QwikViteDevResponse } from './vite'; | ||
import { VITE_ERROR_OVERLAY_STYLES } from './vite-error'; | ||
import { formatError } from './vite-utils'; | ||
|
@@ -140,35 +140,42 @@ export async function configureDevServer( | |
version: '1', | ||
}; | ||
|
||
const added = new Set(); | ||
const allModules = Array.from(server.moduleGraph.fileToModulesMap.entries()); | ||
|
||
const CSS_EXTENSIONS = /\.(css|scss|sass|less|styl|stylus)$/; | ||
const JS_EXTENSIONS = /\.[mc]?[tj]sx?$/; | ||
const cssModules = allModules | ||
.flatMap(([_, modules]) => Array.from(modules)) | ||
.filter((mod) => CSS_EXTENSIONS.test(mod.url)); | ||
|
||
for (const mod of cssModules) { | ||
const hasJsImporter = Array.from(mod.importers).some((importer) => { | ||
const path = importer.url || importer.file; | ||
return path && JS_EXTENSIONS.test(path); | ||
}); | ||
|
||
if (hasJsImporter) { | ||
manifest.injections?.push({ | ||
tag: 'link', | ||
location: 'head', | ||
attributes: { | ||
rel: 'stylesheet', | ||
href: `${base}${mod.url.slice(1)}`, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
Array.from(server.moduleGraph.fileToModulesMap.entries()).forEach((entry) => { | ||
entry[1].forEach((v) => { | ||
const segment = v.info?.meta?.segment; | ||
let url = v.url; | ||
if (v.lastHMRTimestamp) { | ||
url += `?t=${v.lastHMRTimestamp}`; | ||
} | ||
if (segment) { | ||
let url = v.url; | ||
if (v.lastHMRTimestamp) { | ||
url += `?t=${v.lastHMRTimestamp}`; | ||
} | ||
manifest.mapping[segment.name] = relativeURL(url, opts.rootDir); | ||
} | ||
|
||
const { pathId, query } = parseId(v.url); | ||
if ( | ||
query === '' && | ||
['.css', '.scss', '.sass', '.less', '.styl', '.stylus'].some((ext) => | ||
pathId.endsWith(ext) | ||
) | ||
) { | ||
added.add(v.url); | ||
manifest.injections!.push({ | ||
tag: 'link', | ||
location: 'head', | ||
attributes: { | ||
rel: 'stylesheet', | ||
href: `${base}${url.slice(1)}`, | ||
}, | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
|
@@ -192,26 +199,11 @@ export async function configureDevServer( | |
|
||
const result = await render(renderOpts); | ||
|
||
// Sometimes new CSS files are added after the initial render | ||
Array.from(server.moduleGraph.fileToModulesMap.entries()).forEach((entry) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't you now ignoring newly added css modules? I think it would be better to generate the current list of css files on every update and make sure only those are injected? |
||
entry[1].forEach((v) => { | ||
const { pathId, query } = parseId(v.url); | ||
if ( | ||
!added.has(v.url) && | ||
query === '' && | ||
['.css', '.scss', '.sass', '.less', '.styl', '.stylus'].some((ext) => | ||
pathId.endsWith(ext) | ||
) | ||
) { | ||
res.write(`<link rel="stylesheet" href="${base}${v.url.slice(1)}">`); | ||
} | ||
}); | ||
}); | ||
|
||
// End stream | ||
if ('html' in result) { | ||
res.write((result as any).html); | ||
} | ||
|
||
res.write( | ||
END_SSR_SCRIPT(opts, opts.srcDir ? opts.srcDir : path.join(opts.rootDir, 'src')) | ||
); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please undo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please run
pnpm api.update
. Unfortunately the build messes with this.