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

Commit 5a81749

Browse files
tonimcznck
authored andcommitted
feat: preprocessor data option to prepend shared styles (#73)
* Add data option in preprocessOption to allow define env variable in styles * Add test for data preprocessOptions in scss * refactor: Use new line character
1 parent 13cd119 commit 5a81749

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/compiler.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,22 @@ export class SFCCompiler {
168168
]
169169
.concat(this.style.postcssPlugins)
170170
.filter(Boolean)
171+
const preprocessOptions =
172+
(style.lang &&
173+
this.style.preprocessOptions &&
174+
this.style.preprocessOptions[style.lang]) ||
175+
{}
176+
const source = style.src ? this.read(style.src, filename) : style.content
171177
const result = compileStyle(<any>{
172-
source: style.src ? this.read(style.src, filename) : style.content,
178+
source: preprocessOptions.data ? `${preprocessOptions.data}\n${source}` : source,
173179
filename,
174180
id: scopeId,
175181
map: style.map,
176182
scoped: style.scoped || false,
177183
postcssPlugins,
178184
postcssOptions: this.style.postcssOptions,
179185
preprocessLang: style.lang,
180-
preprocessOptions:
181-
(style.lang &&
182-
this.style.preprocessOptions &&
183-
this.style.preprocessOptions[style.lang]) ||
184-
{},
186+
preprocessOptions,
185187
trim: this.style.trim
186188
})
187189

test/compile.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {createDefaultCompiler} from "../src"
2+
3+
it('should prepend data scss option to actual style', () => {
4+
const compiler = createDefaultCompiler({
5+
style: {
6+
preprocessOptions : {
7+
scss: {
8+
data: `$testColor: red;`
9+
}
10+
}
11+
}
12+
})
13+
const result = compiler.compileStyle('foo.vue', 'foo',
14+
{type: 'style', lang: 'scss', content: '.foo_0{ color: $testColor }', map: undefined, attrs: {}, start: 1, end: 1}
15+
);
16+
17+
expect(result.code).toEqual(expect.stringContaining('color: red'))
18+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy