Skip to content

Commit

Permalink
fix: support styles when combining modules and SCSS (#79)
Browse files Browse the repository at this point in the history
Fixes #63
  • Loading branch information
jeremyzahner authored and eddyerburgh committed Apr 10, 2018
1 parent 3f65646 commit aa94fef
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/compilers/scss-compiler.js
Expand Up @@ -27,7 +27,7 @@ module.exports = (content, filePath, config) => {
}
let result
try {
sass.renderSync({
result = sass.renderSync({
data: scssResources + rewriteImports(content, filePath),
outputStyle: 'compressed'
}).css.toString()
Expand Down
31 changes: 19 additions & 12 deletions lib/process.js
Expand Up @@ -84,22 +84,29 @@ module.exports = function (src, filePath, jestConfig) {
}

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

const styleStr = parts.styles.map(ast => {
if (!module) return

const styleObj = (/^sass|less|pcss|postcss/.test(ast.lang))
? {}
: processStyle(ast, filePath, config)

const moduleName = ast.module === true ? '$style' : ast.module

return '\n this[\'' + moduleName + '\'] = ' + JSON.stringify(styleObj)
}).filter(_ => _)
const styleStr = parts.styles
.filter(ast => module && ast.module)
.map(ast => {
const styleObj = (/^sass|less|pcss|postcss/.test(ast.lang))
? {}
: processStyle(ast, filePath, config)

const moduleName = ast.module === true ? '$style' : ast.module

return `
if(!this['${moduleName}']) {
this['${moduleName}'] = {};
}
this['${moduleName}'] = Object.assign(this['${moduleName}'], ${JSON.stringify(styleObj)});
`
})
.filter(_ => _)
.join('')

if (styleStr.length !== 0) {
output += '\n;(function() {' +
Expand Down
3 changes: 1 addition & 2 deletions test/resources/Sass.vue
Expand Up @@ -4,8 +4,7 @@

<style lang="scss">
@import "./styles/transitions";
@import '~@design'
@import "~@design";
.testA {
background-color: red;
Expand Down
10 changes: 9 additions & 1 deletion test/resources/SassModule.vue
@@ -1,5 +1,10 @@
<template>
<div :class="$style.testA"></div>
<div>
<div :class="$style.testA"></div>
<div :class="[ $style.testA ]"></div>
<div :class="{[$style.testB]: true }"></div>
<div :class="[$style.testA, { [$style.testB]: true }]"></div>
</div>
</template>

<style module lang="scss">
Expand All @@ -8,6 +13,9 @@
.testA {
background-color: red;
}
.testB {
background-color: blue;
}
</style>

<style module lang="sass">
Expand Down
16 changes: 12 additions & 4 deletions test/scss.spec.js
Expand Up @@ -3,12 +3,20 @@ import Sass from './resources/Sass.vue'
import SassModule from './resources/SassModule.vue'

describe('processes .vue file with scss style', () => {
it('does not error on scss or sass', () => {
const wrapper = shallow(Sass)
expect(wrapper.classes()).toContain('testA')
it('does not error on scss/sass', () => {
expect(() => shallow(Sass)).not.toThrow()
})

it('does not error on scss/sass module', () => {
expect(() => shallow(SassModule)).not.toThrow()
})
})

describe('processes .vue files which combine scss/sass and modules', () => {
it('does inject classes to $style', () => {
const wrapper = shallow(SassModule)
expect(wrapper.exists()).toBe(true)
expect(wrapper.vm.$style).toBeDefined()
expect(wrapper.vm.$style.testA).toBeDefined()
expect(wrapper.vm.$style.testB).toBeDefined()
})
})

0 comments on commit aa94fef

Please sign in to comment.