-
Notifications
You must be signed in to change notification settings - Fork 157
Add support for <style> and the Stylus lang #49
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
Changes from all commits
d40645d
a201d5f
59fc3b9
225d572
8d1a037
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ const compileCoffeeScript = require('./compilers/coffee-compiler') | |
const extractPropsFromFunctionalTemplate = require('./extract-props') | ||
const fs = require('fs') | ||
const join = require('path').join | ||
const cssExtract = require('extract-from-css') | ||
|
||
const splitRE = /\r?\n/g | ||
|
||
|
@@ -27,6 +28,25 @@ function processScript (scriptPart) { | |
return compileBabel(scriptPart.content) | ||
} | ||
|
||
function processStyle (stylePart) { | ||
if (!stylePart) return {} | ||
|
||
let cssCode = stylePart.content | ||
if (/^styl|stylus$/.test(stylePart.lang)) { | ||
const stylus = require('stylus') | ||
cssCode = stylus.render(stylePart.content) | ||
} | ||
|
||
const cssNames = cssExtract.extractClasses(cssCode) | ||
const obj = {} | ||
|
||
for (let i = 0, l = cssNames.length; i < l; i++) { | ||
obj[cssNames[i]] = cssNames[i] | ||
} | ||
|
||
return obj | ||
} | ||
|
||
function changePartsIfFunctional (parts) { | ||
const isFunctional = parts.template && parts.template.attrs && parts.template.attrs.functional | ||
if (isFunctional) { | ||
|
@@ -78,5 +98,20 @@ module.exports = function (src, path) { | |
} | ||
} | ||
|
||
if (Array.isArray(parts.styles) && parts.styles.length > 0) { | ||
const styleStr = parts.styles.map(ast => { | ||
const moduleName = ast.module || '$style' | ||
const styleObj = processStyle(ast) | ||
|
||
return '\n this[\'' + moduleName + '\'] = ' + JSON.stringify(styleObj) | ||
}) | ||
|
||
output += '\n;(function() {' + | ||
'\nvar render = __vue__options__.render' + | ||
'\n__vue__options__.render = function(h) {' + styleStr + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding it to the render function, I think we should create a beforeCreate() callback that adds the property during intialization of the component. That's essentially what vue-loader does, it would look something like this: output += '\n;(function() {' +
'\nvar existing = __vue__options__.beforeCreate' +
'\nvar styleFn = function () { ' + styleStr + ' })' +
'\n__vue__options__.beforeCreate = existing ? [].concat(existing, styleFn) : [styleFn]' +
'})()' This would ensure that the style is also available before render, e.g. to be accessed from within some method or a |
||
'\n return render.call(this, h)' + | ||
'\n}})()' | ||
} | ||
|
||
return { code: output, map } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { shallow } from 'vue-test-utils' | ||
import Css from './resources/Css.vue' | ||
|
||
test('processes .vue file with Css style', () => { | ||
const wrapper = shallow(Css) | ||
expect(wrapper.classes()).toContain('testA') | ||
expect(wrapper.classes()).toContain('testB') | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<template> | ||
<div :class="[css.testA, $style.testB]"></div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'Button', | ||
} | ||
</script> | ||
|
||
<style module="css"> | ||
.testA { | ||
background-color: red; | ||
} | ||
</style> | ||
<style> | ||
.testB { | ||
background-color: blue; | ||
} | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<template> | ||
<div :class="[css.testA, $style.testB]"></div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'Button', | ||
} | ||
</script> | ||
|
||
<style lang="stylus" module="css"> | ||
.testA { | ||
background-color: red; | ||
} | ||
</style> | ||
<style lang="styl"> | ||
.testB { | ||
background-color: blue; | ||
} | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { shallow } from 'vue-test-utils' | ||
import Stylus from './resources/Stylus.vue' | ||
|
||
test('processes .vue file with Stylus style', () => { | ||
const wrapper = shallow(Stylus) | ||
expect(wrapper.classes()).toContain('testA') | ||
expect(wrapper.classes()).toContain('testB') | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should only convert style partss that acually have a "module" attribute, right?
Otherwise we would create a
$styles
object on a component that onnly has "normal" styles like<style lang="scss">
.