Skip to content

feat(compiler-sfc): additionalData support for css preprocessors #2126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/compiler-sfc/__tests__/compileStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ describe('SFC style preprocessors', () => {
])
})

test('scss respect user-defined options.additionalData', () => {
test('scss respect user-defined string options.additionalData', () => {
const res = compileStyle({
preprocessOptions: {
additionalData: `
Expand All @@ -358,4 +358,32 @@ describe('SFC style preprocessors', () => {

expect(res.errors.length).toBe(0)
})

test('scss respect user-defined function options.additionalData', () => {
const source = `
.square {
@include square(100px);
}
`
const filename = path.resolve(__dirname, './fixture/test.scss')
const res = compileStyle({
preprocessOptions: {
additionalData: (s: string, f: string) => {
expect(s).toBe(source)
expect(f).toBe(filename)
return `
@mixin square($size) {
width: $size;
height: $size;
}`
}
},
source,
filename,
id: '',
preprocessLang: 'scss'
})

expect(res.errors.length).toBe(0)
})
})
18 changes: 16 additions & 2 deletions packages/compiler-sfc/src/stylePreprocessors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import merge from 'merge-source-map'
import { RawSourceMap } from 'source-map'
import { SFCStyleCompileOptions } from './compileStyle'
import { isFunction } from '@vue/shared'

export type StylePreprocessor = (
source: string,
map: RawSourceMap | undefined,
options: {
[key: string]: any
additionalData?: string | ((source: string, filename: string) => string)
filename: string
},
customRequire: SFCStyleCompileOptions['preprocessCustomRequire']
Expand All @@ -24,7 +26,7 @@ const scss: StylePreprocessor = (source, map, options, load = require) => {
const nodeSass = load('sass')
const finalOptions = {
...options,
data: (options.additionalData || '') + source,
data: getSource(source, options.filename, options.additionalData),
file: options.filename,
outFile: options.filename,
sourceMap: !!map
Expand Down Expand Up @@ -66,7 +68,7 @@ const less: StylePreprocessor = (source, map, options, load = require) => {
let result: any
let error: Error | null = null
nodeLess.render(
source,
getSource(source, options.filename, options.additionalData),
{ ...options, syncImport: true },
(err: Error | null, output: any) => {
error = err
Expand Down Expand Up @@ -117,6 +119,18 @@ const styl: StylePreprocessor = (source, map, options, load = require) => {
}
}

function getSource(
source: string,
filename: string,
additionalData?: string | ((source: string, filename: string) => string)
) {
if (!additionalData) return source
if (isFunction(additionalData)) {
return additionalData(source, filename)
}
return additionalData + source
}

export type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus'

export const processors: Record<PreprocessLang, StylePreprocessor> = {
Expand Down