From 20f79f191ab583014720eb71ab8fe07de8c79ac8 Mon Sep 17 00:00:00 2001 From: Day Date: Tue, 30 Jun 2020 23:14:42 +0800 Subject: [PATCH 1/2] feat: distinguish options for different CSS preprocessing languages --- src/index.ts | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0853a7e..818069e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -187,6 +187,29 @@ export default function PluginVue(userOptions: Partial = {}): Plugin { } else if (query.type === 'style') { debug(`transform(${id})`) const block = descriptor.styles[query.index]! + + let preprocessOptions = options.preprocessOptions || {} + const preprocessLang = (options.preprocessStyles + ? block.lang + : undefined) as SFCAsyncStyleCompileOptions['preprocessLang'] + + if (preprocessLang) { + preprocessOptions = + preprocessOptions[preprocessLang] || preprocessOptions + + if ( + ['scss', 'sass'].includes(preprocessLang) && + !preprocessOptions.includePaths + ) { + preprocessOptions = { + includePaths: ['node_modules'], + ...preprocessOptions, + } + } + } else { + preprocessOptions = {} + } + const result = await compileStyleAsync({ filename: query.filename, id: `data-v-${query.id!}`, @@ -197,11 +220,9 @@ export default function PluginVue(userOptions: Partial = {}): Plugin { postcssOptions: options.postcssOptions, postcssPlugins: options.postcssPlugins, modulesOptions: options.cssModulesOptions, - preprocessLang: options.preprocessStyles - ? (block.lang as any) - : undefined, + preprocessLang, preprocessCustomRequire: options.preprocessCustomRequire, - preprocessOptions: options.preprocessOptions || {}, + preprocessOptions, }) if (result.errors.length) { From e2faa2d768677436b456584f2266adce57f42e78 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 16 Jul 2020 16:16:14 -0400 Subject: [PATCH 2/2] adjustments --- src/index.ts | 76 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/src/index.ts b/src/index.ts index 818069e..c819a6b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -196,15 +196,21 @@ export default function PluginVue(userOptions: Partial = {}): Plugin { if (preprocessLang) { preprocessOptions = preprocessOptions[preprocessLang] || preprocessOptions - - if ( - ['scss', 'sass'].includes(preprocessLang) && - !preprocessOptions.includePaths - ) { - preprocessOptions = { - includePaths: ['node_modules'], - ...preprocessOptions, - } + // include node_modules for imports by default + switch (preprocessLang) { + case 'scss': + case 'sass': + preprocessOptions = { + includePaths: ['node_modules'], + ...preprocessOptions, + } + break + case 'less': + case 'stylus': + preprocessOptions = { + paths: ['node_modules'], + ...preprocessOptions, + } } } else { preprocessOptions = {} @@ -368,20 +374,14 @@ function getDescriptor(id: string) { throw new Error(`${id} is not parsed yet`) } -function parseSFC( - code: string, - id: string, - sourceRoot: string -): { descriptor: SFCDescriptor; errors: CompilerError[] } { +function parseSFC(code: string, id: string, sourceRoot: string) { const { descriptor, errors } = parse(code, { sourceMap: true, filename: id, sourceRoot: sourceRoot, }) - cache.set(id, descriptor) - - return { descriptor, errors } + return { descriptor, errors: errors } } function transformVueSFC( @@ -558,21 +558,33 @@ function getCustomBlock( return code } -function createRollupError(id: string, error: CompilerError): RollupError { - return { - id, - plugin: 'vue', - pluginCode: String(error.code), - message: error.message, - frame: error.loc!.source, - parserError: error, - loc: error.loc - ? { - file: id, - line: error.loc.start.line, - column: error.loc.start.column, - } - : undefined, +function createRollupError( + id: string, + error: CompilerError | SyntaxError +): RollupError { + if ('code' in error) { + return { + id, + plugin: 'vue', + pluginCode: String(error.code), + message: error.message, + frame: error.loc!.source, + parserError: error, + loc: error.loc + ? { + file: id, + line: error.loc.start.line, + column: error.loc.start.column, + } + : undefined, + } + } else { + return { + id, + plugin: 'vue', + message: error.message, + parserError: error, + } } }