Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
feat: preprocessor data option to prepend shared styles (#73)
Browse files Browse the repository at this point in the history
* Add data option in preprocessOption to allow define env variable in styles

* Add test for data preprocessOptions in scss

* refactor: Use new line character
  • Loading branch information
tonimc authored and znck committed Jun 24, 2018
1 parent 13cd119 commit 5a81749
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,22 @@ export class SFCCompiler {
]
.concat(this.style.postcssPlugins)
.filter(Boolean)
const preprocessOptions =
(style.lang &&
this.style.preprocessOptions &&
this.style.preprocessOptions[style.lang]) ||
{}
const source = style.src ? this.read(style.src, filename) : style.content
const result = compileStyle(<any>{
source: style.src ? this.read(style.src, filename) : style.content,
source: preprocessOptions.data ? `${preprocessOptions.data}\n${source}` : source,
filename,
id: scopeId,
map: style.map,
scoped: style.scoped || false,
postcssPlugins,
postcssOptions: this.style.postcssOptions,
preprocessLang: style.lang,
preprocessOptions:
(style.lang &&
this.style.preprocessOptions &&
this.style.preprocessOptions[style.lang]) ||
{},
preprocessOptions,
trim: this.style.trim
})

Expand Down
18 changes: 18 additions & 0 deletions test/compile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {createDefaultCompiler} from "../src"

it('should prepend data scss option to actual style', () => {
const compiler = createDefaultCompiler({
style: {
preprocessOptions : {
scss: {
data: `$testColor: red;`
}
}
}
})
const result = compiler.compileStyle('foo.vue', 'foo',
{type: 'style', lang: 'scss', content: '.foo_0{ color: $testColor }', map: undefined, attrs: {}, start: 1, end: 1}
);

expect(result.code).toEqual(expect.stringContaining('color: red'))
})

0 comments on commit 5a81749

Please sign in to comment.