Skip to content

Commit

Permalink
fix: skip hmr for unused css (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
daychongyang authored May 25, 2020
1 parent 528aad6 commit 8f7ee38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions playground/unused.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: red;
}
23 changes: 16 additions & 7 deletions src/node/server/serverPluginCss.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { basename } from 'path'
import { ServerPlugin } from '.'
import { hmrClientId } from './serverPluginHmr'
import hash_sum from 'hash-sum'
Expand All @@ -17,15 +18,11 @@ interface ProcessedEntry {
modules?: Record<string, string>
}

export const debugCSS = require('debug')('vite:css')

const processedCSS = new Map<string, ProcessedEntry>()

export const cssPlugin: ServerPlugin = ({
root,
app,
watcher,
resolver,
config
}) => {
export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
app.use(async (ctx, next) => {
await next()
// handle .css imports
Expand Down Expand Up @@ -66,6 +63,18 @@ export const cssPlugin: ServerPlugin = ({
})

watcher.on('change', (file) => {
/** filter unused files */
if (
!Array.from(processedCSS.keys()).some((processed) =>
file.includes(processed)
) &&
!srcImportMap.has(file)
) {
return debugCSS(
`${basename(file)} has changed, but it is not currently in use`
)
}

if (file.endsWith('.css') || cssPreprocessLangRE.test(file)) {
if (srcImportMap.has(file)) {
// handle HMR for <style src="xxx.css">
Expand Down

0 comments on commit 8f7ee38

Please sign in to comment.