Skip to content

Fixes styles when combining modules and SCSS #79

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 2 commits into from
Apr 10, 2018
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
2 changes: 1 addition & 1 deletion lib/compilers/scss-compiler.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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()
})
})