Skip to content

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

Merged
merged 5 commits into from
Jan 24, 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
35 changes: 35 additions & 0 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) {
Expand Down Expand Up @@ -78,5 +98,20 @@ module.exports = function (src, path) {
}
}

if (Array.isArray(parts.styles) && parts.styles.length > 0) {
const styleStr = parts.styles.map(ast => {
Copy link
Member

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">.

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 +
Copy link
Member

@LinusBorg LinusBorg Jan 24, 2018

Choose a reason for hiding this comment

The 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 created() hook.

'\n return render.call(this, h)' +
'\n}})()'
}

return { code: output, map }
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"jade": "^1.11.0",
"jest": "^20.0.4",
"pug": "^2.0.0-rc.3",
"stylus": "^0.54.5",
"typescript": "^2.5.2",
"vue": "^2.4.2",
"vue-template-compiler": "^2.4.2",
Expand All @@ -58,6 +59,7 @@
"dependencies": {
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"chalk": "^2.1.0",
"extract-from-css": "^0.4.4",
"find-babel-config": "^1.1.0",
"js-beautify": "^1.6.14",
"node-cache": "^4.1.1",
Expand Down
8 changes: 8 additions & 0 deletions test/css.spec.js
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')
})
20 changes: 20 additions & 0 deletions test/resources/Css.vue
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>
20 changes: 20 additions & 0 deletions test/resources/Stylus.vue
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>
8 changes: 8 additions & 0 deletions test/stylus.spec.js
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')
})