Skip to content

Commit

Permalink
feat: recalculate import graph when import change
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored and yyx990803 committed Jul 16, 2020
1 parent d12a828 commit 7a60809
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
10 changes: 7 additions & 3 deletions src/node/server/serverPluginCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { cleanUrl, isImportRequest, readBody } from '../utils'
import { srcImportMap, vueCache } from './serverPluginVue'
import {
compileCss,
cssImportMap,
cssImporterMap,
cssPreprocessLangRE,
getCssImportBoundaries,
recordCssImportChain,
rewriteCssUrls,
isCSSRequest
} from '../utils/cssUtils'
Expand Down Expand Up @@ -44,7 +45,7 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {

/** filter unused files */
if (
!cssImportMap.has(filePath) &&
!cssImporterMap.has(filePath) &&
!processedCSS.has(publicPath) &&
!srcImportMap.has(filePath)
) {
Expand Down Expand Up @@ -135,10 +136,11 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
}

const css = (await readBody(ctx.body))!
const filePath = resolver.requestToFile(ctx.path)
const result = await compileCss(root, ctx.path, {
id: '',
source: css,
filename: resolver.requestToFile(ctx.path),
filename: filePath,
scoped: false,
modules: ctx.path.includes('.module'),
preprocessLang: ctx.path.replace(cssPreprocessLangRE, '$2') as any,
Expand All @@ -151,6 +153,8 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
return res
}

recordCssImportChain(result.dependencies, filePath)

if (result.errors.length) {
console.error(`[vite] error applying css transforms: `)
result.errors.forEach(console.error)
Expand Down
11 changes: 9 additions & 2 deletions src/node/server/serverPluginVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import { transform } from '../esbuildService'
import { InternalResolver } from '../resolver'
import { seenUrls } from './serverPluginServeStatic'
import { codegenCss } from './serverPluginCss'
import { compileCss, rewriteCssUrls } from '../utils/cssUtils'
import {
compileCss,
recordCssImportChain,
rewriteCssUrls
} from '../utils/cssUtils'
import { parse } from '../utils/babelParse'
import MagicString from 'magic-string'
import { resolveImport } from './serverPluginModuleRewrite'
Expand Down Expand Up @@ -609,9 +613,10 @@ async function compileSFCStyle(
const start = Date.now()

const { generateCodeFrame } = resolveCompiler(root)
const resource = filePath + `?type=style&index=${index}`
const result = (await compileCss(root, publicPath, {
source: style.content,
filename: filePath + `?type=style&index=${index}`,
filename: resource,
id: ``, // will be computed in compileCss
scoped: style.scoped != null,
vars: style.vars != null,
Expand All @@ -620,6 +625,8 @@ async function compileSFCStyle(
preprocessOptions
})) as SFCStyleCompileResults

recordCssImportChain(result.dependencies, resource)

if (result.errors.length) {
console.error(chalk.red(`\n[vite] SFC style compilation error: `))
result.errors.forEach((e: any) => {
Expand Down
47 changes: 34 additions & 13 deletions src/node/utils/cssUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function compileCss(
plugins: postcssPlugins
} = await resolvePostcssOptions(root, isBuild)

const res = await compileStyleAsync({
return await compileStyleAsync({
source,
filename,
id: `data-v-${id}`,
Expand All @@ -106,15 +106,6 @@ export async function compileCss(
postcssOptions,
postcssPlugins
})

res.dependencies.forEach((dependency) => {
if (cssImportMap.has(dependency)) {
cssImportMap.get(dependency)!.add(filename)
} else {
cssImportMap.set(dependency, new Set([filename]))
}
})
return res
}

// postcss-load-config doesn't expose Result type
Expand Down Expand Up @@ -156,7 +147,11 @@ export async function resolvePostcssOptions(root: string, isBuild: boolean) {
}
}

export const cssImportMap = new Map<
export const cssImporterMap = new Map<
string /*filePath*/,
Set<string /*filePath*/>
>()
export const cssImporteeMap = new Map<
string /*filePath*/,
Set<string /*filePath*/>
>()
Expand All @@ -165,13 +160,39 @@ export function getCssImportBoundaries(
filePath: string,
boundaries = new Set<string>()
) {
if (!cssImportMap.has(filePath)) {
if (!cssImporterMap.has(filePath)) {
return boundaries
}
const importers = cssImportMap.get(filePath)!
const importers = cssImporterMap.get(filePath)!
for (const importer of importers) {
boundaries.add(importer)
getCssImportBoundaries(importer, boundaries)
}
return boundaries
}

export function recordCssImportChain(dependencies: string[], filePath: string) {
const preImportees = cssImporteeMap.get(filePath)
const currentImportees = new Set(dependencies)
// if import code change, should removed unused previous importee
if (preImportees) {
for (const preImportee of preImportees) {
if (!currentImportees.has(preImportee)) {
const importers = cssImporterMap.get(preImportee)
if (importers) {
importers.delete(filePath)
}
}
}
}

currentImportees.forEach((dependency) => {
if (cssImporterMap.has(dependency)) {
cssImporterMap.get(dependency)!.add(filePath)
} else {
cssImporterMap.set(dependency, new Set([filePath]))
}
})

cssImporteeMap.set(filePath, currentImportees)
}

0 comments on commit 7a60809

Please sign in to comment.