Skip to content

Commit

Permalink
fix: style update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Apr 22, 2022
1 parent 1d468c8 commit 95e1a8c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -300,6 +300,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {

const inlined = inlineRE.test(id)
const modules = cssModulesCache.get(config)!.get(id)
const isHTMLProxy = htmlProxyRE.test(id)
const modulesCode =
modules && dataToEsm(modules, { namedExports: true, preferConst: true })

Expand All @@ -311,7 +312,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
if (options?.ssr) {
return modulesCode || `export default ${JSON.stringify(css)}`
}
if (inlined) {
if (inlined || isHTMLProxy) {
return `export default ${JSON.stringify(css)}`
}

Expand Down Expand Up @@ -346,7 +347,6 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// and then use the cache replace inline-style-flag when `generateBundle` in vite:build-html plugin
const inlineCSS = inlineCSSRE.test(id)
const query = parseRequest(id)
const isHTMLProxy = htmlProxyRE.test(id)
if (inlineCSS && isHTMLProxy) {
addToHTMLProxyTransformResult(
`${cleanUrl(id)}_${Number.parseInt(query!.index)}`,
Expand Down
18 changes: 11 additions & 7 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Expand Up @@ -128,13 +128,17 @@ const devHtmlHook: IndexHtmlTransformHook = async (
if (module) {
server?.moduleGraph.invalidateModule(module)
}

s.overwrite(
node.loc.start.offset,
node.loc.end.offset,
`<script type="module" src="${modulePath}"></script>`,
{ contentOnly: true }
)
if (ext === 'js') {
s.overwrite(
node.loc.start.offset,
node.loc.end.offset,
`<script type="module" src="${modulePath}"></script>`,
{ contentOnly: true }
)
} else if (ext === 'css') {
// just use the style update hmr don't render css
s.append(`<script type="module" src="${modulePath}"></script>`)
}
}

await traverseHtml(html, htmlPath, (node) => {
Expand Down

0 comments on commit 95e1a8c

Please sign in to comment.