Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: css cache check #262

Merged
merged 2 commits into from
May 26, 2020
Merged
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
36 changes: 15 additions & 21 deletions src/node/server/serverPluginCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from '../utils/cssUtils'
import qs from 'querystring'
import chalk from 'chalk'
import slash from 'slash'

interface ProcessedEntry {
css: string
Expand Down Expand Up @@ -51,25 +50,22 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
}
})

watcher.on('change', (file) => {
/** filter unused files */
if (
!Array.from(processedCSS.keys()).some((processed) =>
slash(file).includes(processed)
) &&
!srcImportMap.has(file)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check the file if it is a css file first.

) {
return debugCSS(
`${basename(file)} has changed, but it is not currently in use`
)
}
watcher.on('change', (filePath) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will let the mean of filePath and publicPath more have clearly and consistence.

if (filePath.endsWith('.css') || cssPreprocessLangRE.test(filePath)) {
const publicPath = resolver.fileToRequest(filePath)

if (file.endsWith('.css') || cssPreprocessLangRE.test(file)) {
if (srcImportMap.has(file)) {
/** filter unused files */
if (!processedCSS.has(publicPath) && !srcImportMap.has(filePath)) {
return debugCSS(
Copy link
Member Author

@underfin underfin May 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache key of processedCSS should be publicPath instead of before check filePath.
According to

processedCSS.set(ctx.path, {
      css: result.code,
      modules: result.modules
    })

`${basename(publicPath)} has changed, but it is not currently in use`
)
}

if (srcImportMap.has(filePath)) {
// handle HMR for <style src="xxx.css">
// it cannot be handled as simple css import because it may be scoped
const styleImport = srcImportMap.get(file)
vueCache.del(file)
const styleImport = srcImportMap.get(filePath)
vueCache.del(filePath)
const publicPath = cleanUrl(styleImport)
const index = qs.parse(styleImport.split('?', 2)[1]).index
console.log(
Expand All @@ -84,13 +80,11 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
}
// handle HMR for module.css
// it cannot process with normal css, the class which in module.css maybe removed
if (file.endsWith('.module.css')) {
watcher.handleJSReload(file, Date.now())
if (filePath.endsWith('.module.css')) {
watcher.handleJSReload(filePath, Date.now())
return
}

const publicPath = resolver.fileToRequest(file)

// bust process cache
processedCSS.delete(publicPath)

Expand Down