-
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 2 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 |
---|---|---|
|
@@ -3653,7 +3653,7 @@ Options for the prefetch service worker. | |
</tbody></table> | ||
**Returns:** | ||
|
||
[JSXNode](#jsxnode)<'script'> | ||
JSXNode<'script'> | ||
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 run |
||
|
||
[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/components/prefetch.ts) | ||
|
||
|
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']; | ||
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. we should let the dev add or configure this just in-case the extensions change. but these are good defaults of course 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. We'd need to modify the vite plugin API to do that. In which case we already plan to move the dev server to Qwik Router, so we should wait in this case before any API changes. |
||
const JS_EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx']; | ||
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. /.([mc]?[tj]sx?$/.test(ext)/ 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. there is also .mjs and everything with m 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. also we need to escape the . 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. both added as regexes now |
||
const cssModules = allModules | ||
.flatMap(([_, modules]) => Array.from(modules)) | ||
.filter((mod) => CSS_EXTENSIONS.some((ext) => mod.url.endsWith(ext))); | ||
|
||
for (const mod of cssModules) { | ||
const hasJsImporter = Array.from(mod.importers).some((importer) => { | ||
const path = importer.url || importer.file; | ||
return path && JS_EXTENSIONS.some((ext) => path.endsWith(ext)); | ||
}); | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this file :D
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.
it just adds it back every build