Skip to content

Commit

Permalink
feat: skip unneccessary style file processing with preprocessCSS
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-layne committed Aug 1, 2023
1 parent 0a274dd commit 0637101
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ export const cssUrlRE = /(?<=^|[^\w\-\u0080-\uFFFF])url\((\s*('[^']+'|"[^"]+")\s

// Assuming a function name won't be longer than 256 chars
export const cssImageSetRE = /(?<=image-set\()((?:[\w\-]{1,256}\([^)]*\)|[^)])*)(?=\))/

export const importCssRE = /@import ('[^']+\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)'|"[^"]+\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)"|[^'")]+\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss))/
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type Plugin, type ResolvedConfig, createFilter, preprocessCSS } from 'v
import { type PluginContext } from 'rollup'
import { interpolateName } from 'loader-utils'
import { checkFormats, getAssetContent, getCaptured, getFileBase64 } from './utils'
import { CSS_LANGS_RE, DEFAULT_ASSETS_RE, cssImageSetRE, cssUrlRE } from './constants'
import { CSS_LANGS_RE, DEFAULT_ASSETS_RE, cssImageSetRE, cssUrlRE, importCssRE } from './constants'

type LoaderContext = Parameters<typeof interpolateName>[0]

Expand Down Expand Up @@ -79,9 +79,12 @@ export default function VitePluginLibAssets(options: Options = {}): Plugin {
if (!content)
return []

const result = await preprocessCSS(content.toString(), id, viteConfig)
let source = content.toString()
if (importCssRE.test(source)) {
const result = await preprocessCSS(source, id, viteConfig)
source = result.code
}

const source = result.code
const cssUrlAssets = getCaptured(source, cssUrlRE)
const cssImageSetAssets = getCaptured(source, cssImageSetRE)
const assets = [...cssUrlAssets, ...cssImageSetAssets]
Expand Down

0 comments on commit 0637101

Please sign in to comment.