Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 860595e

Browse files
author
Day
authored
feat: distinguish options for different CSS preprocessing languages (#366)
1 parent 2a7f92e commit 860595e

File tree

1 file changed

+60
-27
lines changed

1 file changed

+60
-27
lines changed

src/index.ts

+60-27
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,35 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
187187
} else if (query.type === 'style') {
188188
debug(`transform(${id})`)
189189
const block = descriptor.styles[query.index]!
190+
191+
let preprocessOptions = options.preprocessOptions || {}
192+
const preprocessLang = (options.preprocessStyles
193+
? block.lang
194+
: undefined) as SFCAsyncStyleCompileOptions['preprocessLang']
195+
196+
if (preprocessLang) {
197+
preprocessOptions =
198+
preprocessOptions[preprocessLang] || preprocessOptions
199+
// include node_modules for imports by default
200+
switch (preprocessLang) {
201+
case 'scss':
202+
case 'sass':
203+
preprocessOptions = {
204+
includePaths: ['node_modules'],
205+
...preprocessOptions,
206+
}
207+
break
208+
case 'less':
209+
case 'stylus':
210+
preprocessOptions = {
211+
paths: ['node_modules'],
212+
...preprocessOptions,
213+
}
214+
}
215+
} else {
216+
preprocessOptions = {}
217+
}
218+
190219
const result = await compileStyleAsync({
191220
filename: query.filename,
192221
id: `data-v-${query.id!}`,
@@ -197,11 +226,9 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
197226
postcssOptions: options.postcssOptions,
198227
postcssPlugins: options.postcssPlugins,
199228
modulesOptions: options.cssModulesOptions,
200-
preprocessLang: options.preprocessStyles
201-
? (block.lang as any)
202-
: undefined,
229+
preprocessLang,
203230
preprocessCustomRequire: options.preprocessCustomRequire,
204-
preprocessOptions: options.preprocessOptions || {},
231+
preprocessOptions,
205232
})
206233

207234
if (result.errors.length) {
@@ -347,20 +374,14 @@ function getDescriptor(id: string) {
347374
throw new Error(`${id} is not parsed yet`)
348375
}
349376

350-
function parseSFC(
351-
code: string,
352-
id: string,
353-
sourceRoot: string
354-
): { descriptor: SFCDescriptor; errors: CompilerError[] } {
377+
function parseSFC(code: string, id: string, sourceRoot: string) {
355378
const { descriptor, errors } = parse(code, {
356379
sourceMap: true,
357380
filename: id,
358381
sourceRoot: sourceRoot,
359382
})
360-
361383
cache.set(id, descriptor)
362-
363-
return { descriptor, errors }
384+
return { descriptor, errors: errors }
364385
}
365386

366387
function transformVueSFC(
@@ -537,21 +558,33 @@ function getCustomBlock(
537558
return code
538559
}
539560

540-
function createRollupError(id: string, error: CompilerError): RollupError {
541-
return {
542-
id,
543-
plugin: 'vue',
544-
pluginCode: String(error.code),
545-
message: error.message,
546-
frame: error.loc!.source,
547-
parserError: error,
548-
loc: error.loc
549-
? {
550-
file: id,
551-
line: error.loc.start.line,
552-
column: error.loc.start.column,
553-
}
554-
: undefined,
561+
function createRollupError(
562+
id: string,
563+
error: CompilerError | SyntaxError
564+
): RollupError {
565+
if ('code' in error) {
566+
return {
567+
id,
568+
plugin: 'vue',
569+
pluginCode: String(error.code),
570+
message: error.message,
571+
frame: error.loc!.source,
572+
parserError: error,
573+
loc: error.loc
574+
? {
575+
file: id,
576+
line: error.loc.start.line,
577+
column: error.loc.start.column,
578+
}
579+
: undefined,
580+
}
581+
} else {
582+
return {
583+
id,
584+
plugin: 'vue',
585+
message: error.message,
586+
parserError: error,
587+
}
555588
}
556589
}
557590

0 commit comments

Comments
 (0)