-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf!: 自动检测是否使用 Lightning CSS 来加快 css 处理
- Loading branch information
Showing
5 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { Alias } from './alias' | ||
export { Warmup } from './warmup' | ||
export { Restart } from './restart' | ||
export { Lightningcss } from './lightningcss' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { existsSync } from 'fs' | ||
import { gray } from 'kolorist' | ||
import type { Plugin } from 'vite' | ||
import { createConsola } from 'consola' | ||
import { isPackageExists } from 'local-pkg' | ||
|
||
const name = 'vite-plugin-fire-lightningcss' | ||
|
||
const logger = createConsola().withTag('css') | ||
|
||
/** | ||
* 智能开启 lightningcss (如果不使用预处理器,或者 postcss) | ||
*/ | ||
export function Lightningcss(): Plugin { | ||
const packages = ['less', 'sass', 'stylus'] | ||
return { | ||
name, | ||
config(config) { | ||
config.css ??= {} | ||
config.build ??= {} | ||
const hasPreprocessor = packages.some((p) => isPackageExists(p)) | ||
|
||
const { postcss, modules, transformer } = config.css | ||
const conflictConfiguration = [postcss, modules, transformer].some( | ||
(c) => !isUndefined(c), | ||
) | ||
|
||
const hasPostcssConfigFile = [ | ||
'postcss.config.js', | ||
'postcss.config.cts', | ||
'postcss.config.ts', | ||
].some((c) => existsSync(c)) | ||
|
||
// 如果有预处理器,冲突配置或者 postcss 配置文件则禁用 | ||
const disabled = | ||
hasPreprocessor || conflictConfiguration || hasPostcssConfigFile | ||
if (!disabled) { | ||
const transformer = 'lightningcss' | ||
config.css.transformer = transformer | ||
let tip = `${transformer} ${gray(transformer)}` | ||
|
||
if (isUndefined(config.build.cssMinify)) { | ||
config.build.cssMinify = 'lightningcss' | ||
tip = `${transformer} ${gray('(transformer + cssMinify)')}` | ||
} | ||
logger.success(tip) | ||
} | ||
}, | ||
} | ||
|
||
function isUndefined(v: unknown): v is undefined { | ||
return typeof v === 'undefined' | ||
} | ||
} |