Skip to content
This repository has been archived by the owner on Dec 26, 2018. It is now read-only.

Commit

Permalink
relative import path & dependency support for stylus
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 3, 2015
1 parent b90ffef commit 96dd31c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
23 changes: 21 additions & 2 deletions lib/compilers/stylus.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
var options = require('./options')
var assign = require('object-assign')
var path = require('path')

module.exports = function (raw, cb) {
module.exports = function (raw, cb, compiler, filePath) {
try {
var stylus = require('stylus')
} catch (err) {
return cb(err)
}
stylus.render(raw, options.stylus || {}, cb)

var dir = path.dirname(filePath)
var paths = [dir, process.cwd()]
paths = options.stylus && options.stylus.paths
? options.stylus.paths.concat(paths)
: paths

var renderer = stylus(raw)
.set('filename', path.basename(filePath))
.set('paths', paths)

renderer.render(function (err, css) {
if (err) return cb(err)
renderer.deps().forEach(function (file) {
compiler.emit('dependency', file)
})
cb(null, css)
})
}
1 change: 1 addition & 0 deletions test/expects/styl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var __vueify_style__ = require("vueify-insert-css").insert("body{font:100% Helvetica,sans-serif;color:#333}")
File renamed without changes.
2 changes: 2 additions & 0 deletions test/fixtures/imports/import.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$font-stack = Helvetica, sans-serif;
$primary-color = #333
2 changes: 1 addition & 1 deletion test/fixtures/sass.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<style lang="sass">
@import './imports/import.scss';
@import './imports/import.sass';
body {
font: 100% $font-stack;
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/styl.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<style lang="stylus">
@import './imports/import.styl'
body
font 100% $font-stack
color $primary-color
</style>
23 changes: 13 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ function test (name) {
var expected = read('expects/' + name + '.js')
.replace(/\{\{id\}\}/g, '_v-' + hash(require.resolve('./' + filePath)))

// test src imports registering dependency
var addDep
var deps
if (name === 'src') {
deps = []
addDep = function (file) {
deps.push(file)
}
compiler.on('dependency', addDep)
// test registering dependency
var deps = []
function addDep (file) {
deps.push(file)
}
compiler.on('dependency', addDep)

process.env.VUEIFY_TEST = true
process.env.NODE_ENV = name === 'non-minified'
Expand All @@ -51,12 +47,19 @@ function test (name) {
setTimeout(function () {
if (err) throw err
assert.equal(result, expected, 'should compile correctly')

// check src
if (name === 'src') {
compiler.removeListener('dependency', addDep)
assert.equal(deps[0], __dirname + '/fixtures/test.html')
assert.equal(deps[1], __dirname + '/fixtures/test.styl')
assert.equal(deps[2], __dirname + '/fixtures/src/test.js')
}

if (name === 'less' || name === 'sass' || name === 'styl') {
assert.equal(deps[0], __dirname + '/fixtures/imports/import.' + name)
}

compiler.removeListener('dependency', addDep)
done()
}, 0)
}
Expand Down

0 comments on commit 96dd31c

Please sign in to comment.