Skip to content

Commit aa94fef

Browse files
jeremyzahnereddyerburgh
authored andcommitted
fix: support styles when combining modules and SCSS (#79)
Fixes #63
1 parent 3f65646 commit aa94fef

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

lib/compilers/scss-compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = (content, filePath, config) => {
2727
}
2828
let result
2929
try {
30-
sass.renderSync({
30+
result = sass.renderSync({
3131
data: scssResources + rewriteImports(content, filePath),
3232
outputStyle: 'compressed'
3333
}).css.toString()

lib/process.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,29 @@ module.exports = function (src, filePath, jestConfig) {
8484
}
8585

8686
if (Array.isArray(parts.styles) && parts.styles.length > 0) {
87-
if ((parts.styles.some(ast => /^scss|sass|less|pcss|postcss/.test(ast.lang))) && logger.shouldLogStyleWarn) {
87+
if ((parts.styles.some(ast => /^sass|less|pcss|postcss/.test(ast.lang))) && logger.shouldLogStyleWarn) {
8888
!config.hideStyleWarn && logger.warn('Sass, Less and PostCSS are not currently compiled by vue-jest')
8989
logger.shouldLogStyleWarn = false
9090
}
9191

92-
const styleStr = parts.styles.map(ast => {
93-
if (!module) return
94-
95-
const styleObj = (/^sass|less|pcss|postcss/.test(ast.lang))
96-
? {}
97-
: processStyle(ast, filePath, config)
98-
99-
const moduleName = ast.module === true ? '$style' : ast.module
100-
101-
return '\n this[\'' + moduleName + '\'] = ' + JSON.stringify(styleObj)
102-
}).filter(_ => _)
92+
const styleStr = parts.styles
93+
.filter(ast => module && ast.module)
94+
.map(ast => {
95+
const styleObj = (/^sass|less|pcss|postcss/.test(ast.lang))
96+
? {}
97+
: processStyle(ast, filePath, config)
98+
99+
const moduleName = ast.module === true ? '$style' : ast.module
100+
101+
return `
102+
if(!this['${moduleName}']) {
103+
this['${moduleName}'] = {};
104+
}
105+
this['${moduleName}'] = Object.assign(this['${moduleName}'], ${JSON.stringify(styleObj)});
106+
`
107+
})
108+
.filter(_ => _)
109+
.join('')
103110

104111
if (styleStr.length !== 0) {
105112
output += '\n;(function() {' +

test/resources/Sass.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
<style lang="scss">
66
@import "./styles/transitions";
7-
@import '~@design'
8-
7+
@import "~@design";
98
109
.testA {
1110
background-color: red;

test/resources/SassModule.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<template>
2-
<div :class="$style.testA"></div>
2+
<div>
3+
<div :class="$style.testA"></div>
4+
<div :class="[ $style.testA ]"></div>
5+
<div :class="{[$style.testB]: true }"></div>
6+
<div :class="[$style.testA, { [$style.testB]: true }]"></div>
7+
</div>
38
</template>
49

510
<style module lang="scss">
@@ -8,6 +13,9 @@
813
.testA {
914
background-color: red;
1015
}
16+
.testB {
17+
background-color: blue;
18+
}
1119
</style>
1220

1321
<style module lang="sass">

test/scss.spec.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import Sass from './resources/Sass.vue'
33
import SassModule from './resources/SassModule.vue'
44

55
describe('processes .vue file with scss style', () => {
6-
it('does not error on scss or sass', () => {
7-
const wrapper = shallow(Sass)
8-
expect(wrapper.classes()).toContain('testA')
6+
it('does not error on scss/sass', () => {
7+
expect(() => shallow(Sass)).not.toThrow()
98
})
10-
119
it('does not error on scss/sass module', () => {
1210
expect(() => shallow(SassModule)).not.toThrow()
1311
})
1412
})
13+
14+
describe('processes .vue files which combine scss/sass and modules', () => {
15+
it('does inject classes to $style', () => {
16+
const wrapper = shallow(SassModule)
17+
expect(wrapper.exists()).toBe(true)
18+
expect(wrapper.vm.$style).toBeDefined()
19+
expect(wrapper.vm.$style.testA).toBeDefined()
20+
expect(wrapper.vm.$style.testB).toBeDefined()
21+
})
22+
})

0 commit comments

Comments
 (0)