From f613c97997c85284c96a112cce9816a917f6a727 Mon Sep 17 00:00:00 2001 From: daychongyang Date: Mon, 25 May 2020 12:10:00 +0800 Subject: [PATCH 1/2] fix: unused css --- playground/unused.css | 3 +++ src/node/server/serverPluginCss.ts | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 playground/unused.css diff --git a/playground/unused.css b/playground/unused.css new file mode 100644 index 00000000000000..aa1634c255034b --- /dev/null +++ b/playground/unused.css @@ -0,0 +1,3 @@ +body { + background-color: red; +} diff --git a/src/node/server/serverPluginCss.ts b/src/node/server/serverPluginCss.ts index d78a64d9f05fd2..a4264c734ff56d 100644 --- a/src/node/server/serverPluginCss.ts +++ b/src/node/server/serverPluginCss.ts @@ -1,3 +1,4 @@ +import { basename } from 'path' import { ServerPlugin } from '.' import { hmrClientId } from './serverPluginHmr' import hash_sum from 'hash-sum' @@ -17,15 +18,11 @@ interface ProcessedEntry { modules?: Record } +export const debugCSS = require('debug')('vite:css') + const processedCSS = new Map() -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 @@ -66,6 +63,17 @@ export const cssPlugin: ServerPlugin = ({ }) watcher.on('change', (file) => { + /** filter unused files */ + if ( + !Array.from(processedCSS.keys()).some((processed) => + file.includes(processed) + ) + ) { + 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