Skip to content

Styles #50

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 8 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add support for <style> and the Stylus lang
  • Loading branch information
danielbastos11 committed Jan 23, 2018
commit d40645d9a42b32b6ac7356bb2cd8758398ee72dd
34 changes: 34 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,19 @@ module.exports = function (src, path) {
}
}

if (Array.isArray(parts.styles)) {
parts.styles.forEach(ast => {
const moduleName = ast.module || '$style'
const styleObj = processStyle(ast)

output += '\n;(function() {' +
'\nvar render = __vue__options__.render' +
'\n__vue__options__.render = function(h) {' +
'\n this[\'' + moduleName + '\'] = ' + JSON.stringify(styleObj) +
'\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 @@ -52,12 +52,14 @@
},
"peerDependencies": {
"babel-core": "^6.25.0 || ^7.0.0-0",
"stylus": "^0.54.5",
"vue": "^2.x",
"vue-template-compiler": "^2.x"
},
"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
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')
})