Skip to content

Commit 6ad9553

Browse files
danielbastos11eddyerburgh
authored andcommitted
Styles (#50)
* Add support for <style> and the Stylus lang * Add Stylus as dev dependency * Add test for vanilla CSS * Group styles HOCs into one single HOC * Fix typo in test name * Move style definition to beforeCreate in order to allow for usage in other parts of the lifecycle * Stop importing classes for non-module CSS
1 parent 7791d8e commit 6ad9553

File tree

5 files changed

+55
-16
lines changed

5 files changed

+55
-16
lines changed

lib/process.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,20 @@ module.exports = function (src, path) {
100100

101101
if (Array.isArray(parts.styles) && parts.styles.length > 0) {
102102
const styleStr = parts.styles.map(ast => {
103-
const moduleName = ast.module || '$style'
103+
if (!module) return
104+
const moduleName = ast.module === true ? '$style' : ast.module
104105
const styleObj = processStyle(ast)
105106

106107
return '\n this[\'' + moduleName + '\'] = ' + JSON.stringify(styleObj)
107108
})
108109

109-
output += '\n;(function() {' +
110-
'\nvar render = __vue__options__.render' +
111-
'\n__vue__options__.render = function(h) {' + styleStr +
112-
'\n return render.call(this, h)' +
113-
'\n}})()'
110+
if (styleStr.length !== 0) {
111+
output += '\n;(function() {' +
112+
'\nvar beforeCreate = __vue__options__.beforeCreate' +
113+
'\nvar styleFn = function () { ' + styleStr + ' }' +
114+
'\n__vue__options__.beforeCreate = beforeCreate ? [].concat(beforeCreate, styleFn) : [styleFn]' +
115+
'\n})()'
116+
}
114117
}
115118

116119
return { code: output, map }

test/css.spec.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import { shallow } from 'vue-test-utils'
22
import Css from './resources/Css.vue'
33

4-
test('processes .vue file with Css style', () => {
5-
const wrapper = shallow(Css)
6-
expect(wrapper.classes()).toContain('testA')
7-
expect(wrapper.classes()).toContain('testB')
4+
describe('processes .vue file with Css style', () => {
5+
let wrapper
6+
beforeAll(() => {
7+
wrapper = shallow(Css)
8+
})
9+
10+
it('should bind from style tags with named module', () => {
11+
expect(wrapper.classes()).toContain('testA')
12+
})
13+
14+
it('should bind from style tags with anonymous modules', () => {
15+
expect(wrapper.classes()).toContain('testB')
16+
})
17+
18+
it('should not bind from style tags without a module', () => {
19+
expect(wrapper.vm.$style.testC).toBeFalsy()
20+
})
821
})

test/resources/Css.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ export default {
1313
background-color: red;
1414
}
1515
</style>
16-
<style>
16+
<style module>
1717
.testB {
1818
background-color: blue;
1919
}
2020
</style>
21+
<style>
22+
.testC {
23+
background-color: blue;
24+
}
25+
</style>

test/resources/Stylus.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ export default {
1313
background-color: red;
1414
}
1515
</style>
16-
<style lang="styl">
16+
<style lang="styl" module>
1717
.testB {
1818
background-color: blue;
1919
}
2020
</style>
21+
<style>
22+
.testC {
23+
background-color: blue;
24+
}
25+
</style>

test/stylus.spec.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import { shallow } from 'vue-test-utils'
22
import Stylus from './resources/Stylus.vue'
33

4-
test('processes .vue file with Stylus style', () => {
5-
const wrapper = shallow(Stylus)
6-
expect(wrapper.classes()).toContain('testA')
7-
expect(wrapper.classes()).toContain('testB')
4+
describe('processes .vue file with Stylus style', () => {
5+
let wrapper
6+
beforeAll(() => {
7+
wrapper = shallow(Stylus)
8+
})
9+
10+
it('should bind from style tags with named module', () => {
11+
expect(wrapper.classes()).toContain('testA')
12+
})
13+
14+
it('should bind from style tags with anonymous modules', () => {
15+
expect(wrapper.classes()).toContain('testB')
16+
})
17+
18+
it('should not bind from style tags without a module', () => {
19+
expect(wrapper.vm.$style.testC).toBeFalsy()
20+
})
821
})

0 commit comments

Comments
 (0)