Skip to content

Commit

Permalink
perf: avoid unnecessary css processing when source did not change
Browse files Browse the repository at this point in the history
close #383
  • Loading branch information
yyx990803 committed Jun 11, 2020
1 parent 88f411e commit a792610
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/node/server/serverPluginCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../utils/cssUtils'
import qs from 'querystring'
import chalk from 'chalk'
import { InternalResolver } from '../resolver'

interface ProcessedEntry {
css: string
Expand Down Expand Up @@ -78,14 +79,14 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
// handle HMR for module.css
// it cannot be handled as normal css because the js exports may change
if (filePath.endsWith('.module.css')) {
moduleCssUpdate(filePath)
moduleCssUpdate(filePath, resolver)
}

const boundaries = getCssImportBoundaries(filePath)
if (boundaries.size) {
for (let boundary of boundaries) {
if (boundary.includes('.module')) {
moduleCssUpdate(boundary)
moduleCssUpdate(boundary, resolver)
} else if (boundary.includes('.vue')) {
vueCache.del(cleanUrl(boundary))
vueStyleUpdate(resolver.fileToRequest(boundary))
Expand All @@ -111,7 +112,10 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
})
}

function moduleCssUpdate(filePath: string) {
function moduleCssUpdate(filePath: string, resolver: InternalResolver) {
// bust process cache
processedCSS.delete(resolver.fileToRequest(filePath))

watcher.handleJSReload(filePath)
}

Expand All @@ -127,6 +131,12 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
}

async function processCss(root: string, ctx: Context) {
// source didn't change (marker added by cachedRead)
// just use previously cached result
if (ctx.__notModified && processedCSS.has(ctx.path)) {
return
}

const css = (await readBody(ctx.body))!
const result = await compileCss(root, ctx.path, {
id: '',
Expand Down
2 changes: 2 additions & 0 deletions src/node/utils/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export async function cachedRead(
}
if (cached && cached.lastModified === lastModified) {
if (ctx) {
// a private marker in case the user ticks "disable cache" during dev
ctx.__notModified = true
ctx.etag = cached.etag
ctx.lastModified = new Date(cached.lastModified)
if (
Expand Down

0 comments on commit a792610

Please sign in to comment.