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 committed Jun 1, 2020
1 parent fd275b1 commit 65afca8
Show file tree
Hide file tree
Showing 3 changed files with 51 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 @@ -7,9 +7,10 @@ import { srcImportMap, vueCache } from './serverPluginVue'
import {
codegenCss,
compileCss,
cssImportMap,
cssImporterMap,
cssPreprocessLangRE,
getCssImportBoundaries,
recordCssImportChain,
rewriteCssUrls
} from '../utils/cssUtils'
import qs from 'querystring'
Expand Down Expand Up @@ -58,7 +59,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 @@ -129,10 +130,11 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {

async function processCss(root: string, ctx: Context) {
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 @@ -143,6 +145,8 @@ export const cssPlugin: ServerPlugin = ({ root, app, watcher, resolver }) => {
return
}

recordCssImportChain(result.dependencies, filePath)

if (result.errors.length) {
console.error(`[vite] error applying css transforms: `)
result.errors.forEach(console.error)
Expand Down
12 changes: 10 additions & 2 deletions src/node/server/serverPluginVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import { Context } from 'koa'
import { transform } from '../esbuildService'
import { InternalResolver } from '../resolver'
import { seenUrls } from './serverPluginServeStatic'
import { codegenCss, compileCss, rewriteCssUrls } from '../utils/cssUtils'
import {
codegenCss,
compileCss,
recordCssImportChain,
rewriteCssUrls
} from '../utils/cssUtils'
import { parse } from '../utils/babelParse'
import MagicString from 'magic-string'
import { resolveImport } from './serverPluginModuleRewrite'
Expand Down Expand Up @@ -514,15 +519,18 @@ 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,
modules: style.module != null,
preprocessLang: style.lang as any
})) 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 @@ -72,7 +72,7 @@ export async function compileCss(
// parser plain css @import
postcssPlugins.unshift(require('postcss-import')())

const res = await compileStyleAsync({
return await compileStyleAsync({
source,
filename,
id: `data-v-${id}`,
Expand All @@ -91,15 +91,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
}

export function codegenCss(
Expand Down Expand Up @@ -144,7 +135,11 @@ async function loadPostcssConfig(
}
}

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 @@ -153,13 +148,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 65afca8

Please sign in to comment.