Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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