@@ -8,18 +8,15 @@ import {
88 INJECT_PREFIX_FLAG ,
99 INJECT_SUFFIX_FLAG ,
1010 SUPPORT_FILE ,
11- SUPPORT_FILE_LIST ,
1211 completeSuffix ,
1312 transformSymbol ,
1413} from '@unplugin-vue-cssvars/utils'
1514import MagicString from 'magic-string'
16- import sass from 'sass'
17- import less from 'less'
18- import stylus from 'stylus'
1915import { parseImports } from '../parser'
2016import { transformQuotes } from '../transform/transform-quotes'
17+ import type { ICSSFileMap , PreProcessor , SearchGlobOptions } from '../types'
2118import type { ImportStatement } from '../parser'
22- import type { ICSSFileMap , SearchGlobOptions } from '../types'
19+
2320import type { CssNode } from 'css-tree'
2421
2522/**
@@ -85,10 +82,10 @@ export function walkCSSTree(
8582 * @param options 选项参数 Options
8683 */
8784export function preProcessCSS ( options : SearchGlobOptions ) : ICSSFileMap {
88- const { rootDir } = options
85+ const { rootDir, preprocessor , includeCompile } = options
8986
9087 // 获得文件列表
91- const files = fg . sync ( SUPPORT_FILE_LIST , {
88+ const files = fg . sync ( includeCompile ! , {
9289 ignore : FG_IGNORE_LIST ,
9390 cwd : rootDir ,
9491 } )
@@ -112,7 +109,7 @@ export function preProcessCSS(options: SearchGlobOptions): ICSSFileMap {
112109 const orgCode = fs . readFileSync ( resolve ( rootDir ! , file ) , { encoding : 'utf-8' } )
113110 const { imports } = parseImports ( orgCode , [ transformQuotes ] )
114111 const codeNoImporter = getContentNoImporter ( orgCode , imports )
115- let code = generateCSSCode ( codeNoImporter , fileSuffix )
112+ let code = generateCSSCode ( codeNoImporter , fileSuffix , preprocessor ! )
116113 code = setImportToCompileRes ( code , imports )
117114
118115 // parse css ast
@@ -154,29 +151,41 @@ export function preProcessCSS(options: SearchGlobOptions): ICSSFileMap {
154151 return cssFiles
155152}
156153
157- export function generateCSSCode ( code : string , suffix : string ) {
154+ export function generateCSSCode ( code : string , suffix : string , preprocessor : PreProcessor ) {
158155 let res = ''
159156 switch ( suffix ) {
160157 case `.${ SUPPORT_FILE . SCSS } ` : // scss
161158 // @import 有 css 和 scss的同名文件,会编译 scss
162159 // @import 编译 scss,会一直编译,一直到遇到 import 了一个 css 或没有 import 为止
163160 // 这里先分析出 imports,在根据其内容将 sass 中 import 删除
164161 // 编译 sass 为 css,再复原
165- res = sass . compileString ( code ) . css
162+ if ( ! preprocessor . sass )
163+ throw new Error ( '[unplugin-vue-cssvars]: Missing preprocessor \'sass\' dependency, please see readme to resolve this problem' )
164+
165+ res = preprocessor . sass . compileString ( code ) . css
166166 break
167167 case `.${ SUPPORT_FILE . SASS } ` : // sass
168- res = sass . compileString ( code , { syntax : 'indented' } ) . css
168+ if ( ! preprocessor . sass )
169+ throw new Error ( '[unplugin-vue-cssvars]: Missing preprocessor \'sass\' dependency, please see readme to resolve this problem' )
170+
171+ res = preprocessor . sass . compileString ( code , { syntax : 'indented' } ) . css
169172 break
170173 case `.${ SUPPORT_FILE . LESS } ` : // less
171- less . render ( code , { } , ( error , output ) => {
174+ if ( ! preprocessor . less )
175+ throw new Error ( '[unplugin-vue-cssvars]: Missing preprocessor \'less\' dependency, please see readme to resolve this problem' )
176+
177+ preprocessor . less . render ( code , { } , ( error , output ) => {
172178 if ( error )
173179 throw error
174180
175181 res = output ? output . css : ''
176182 } )
177183 break
178184 case `.${ SUPPORT_FILE . STYL } ` : // stylus
179- stylus . render ( code , { } , ( error : Error , css : string ) => {
185+ if ( ! preprocessor . stylus )
186+ throw new Error ( '[unplugin-vue-cssvars]: Missing preprocessor \'stylus\' dependency, please see readme to resolve this problem' )
187+
188+ preprocessor . stylus . render ( code , { } , ( error : Error , css : string ) => {
180189 if ( error )
181190 throw error
182191
0 commit comments