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

Commit 50a7d78

Browse files
committed
feat: add preprocessStyles option (defaults to false)
1 parent 4cdd94e commit 50a7d78

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

Diff for: src/index.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export interface Options
3939
target: 'node' | 'browser'
4040
exposeFilename: boolean
4141

42+
// if true, handle preprocessors directly instead of delegating to other
43+
// rollup plugins
44+
preprocessStyles?: boolean
45+
4246
// TODO this will be exposed via SFCAsyncStyleCompileOptions which we forgot
4347
// to export in @vue/compiler-sfc
4448
cssModulesOptions?: {
@@ -194,7 +198,9 @@ export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
194198
scoped: block.scoped,
195199
modules: !!block.module,
196200
modulesOptions: options.cssModulesOptions,
197-
preprocessLang: block.lang as any,
201+
preprocessLang: options.preprocessStyles
202+
? (block.lang as any)
203+
: undefined,
198204
preprocessCustomRequire: options.preprocessCustomRequire,
199205
})
200206

@@ -355,7 +361,12 @@ function transformVueSFC(
355361
hasScoped
356362
)
357363
const scriptImport = getScriptCode(descriptor, resourcePath)
358-
const stylesCode = getStyleCode(descriptor, resourcePath, id)
364+
const stylesCode = getStyleCode(
365+
descriptor,
366+
resourcePath,
367+
id,
368+
options.preprocessStyles
369+
)
359370
const output = [
360371
scriptImport,
361372
templateImport,
@@ -412,7 +423,8 @@ function getScriptCode(descriptor: SFCDescriptor, resourcePath: string) {
412423
function getStyleCode(
413424
descriptor: SFCDescriptor,
414425
resourcePath: string,
415-
id: string
426+
id: string,
427+
preprocessStyles?: boolean
416428
) {
417429
let stylesCode = ``
418430
let hasCSSModules = false
@@ -421,7 +433,7 @@ function getStyleCode(
421433
const src = style.src || resourcePath
422434
// do not include module in default query, since we use it to indicate
423435
// that the module needs to export the modules json
424-
const attrsQuery = attrsToQuery(style.attrs, 'css')
436+
const attrsQuery = attrsToQuery(style.attrs, 'css', preprocessStyles)
425437
const attrsQueryWithoutModule = attrsQuery.replace(/&module(=true)?/, '')
426438
// make sure to only pass id when necessary so that we don't inject
427439
// duplicate tags when multiple components import the same css file
@@ -471,7 +483,11 @@ function createRollupError(id: string, error: CompilerError): RollupError {
471483
// these are built-in query parameters so should be ignored
472484
// if the user happen to add them as attrs
473485
const ignoreList = ['id', 'index', 'src', 'type']
474-
function attrsToQuery(attrs: SFCBlock['attrs'], langFallback?: string): string {
486+
function attrsToQuery(
487+
attrs: SFCBlock['attrs'],
488+
langFallback?: string,
489+
forceLangFallback = false
490+
): string {
475491
let query = ``
476492
for (const name in attrs) {
477493
const value = attrs[name]
@@ -482,7 +498,12 @@ function attrsToQuery(attrs: SFCBlock['attrs'], langFallback?: string): string {
482498
}
483499
}
484500
if (langFallback) {
485-
query += `lang` in attrs ? `.${langFallback}` : `&lang.${langFallback}`
501+
query +=
502+
`lang` in attrs
503+
? forceLangFallback
504+
? `.${langFallback}`
505+
: ``
506+
: `&lang.${langFallback}`
486507
}
487508
return query
488509
}

0 commit comments

Comments
 (0)