Skip to content

Commit e047135

Browse files
mdvorscakeddyerburgh
authored andcommitted
fix: parsing relative imports in stylus files (#54)
1 parent a46ac9d commit e047135

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

lib/process.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const compileTypescript = require('./compilers/typescript-compiler')
77
const compileCoffeeScript = require('./compilers/coffee-compiler')
88
const extractPropsFromFunctionalTemplate = require('./extract-props')
99
const fs = require('fs')
10-
const join = require('path').join
10+
const path = require('path')
11+
const join = path.join
1112
const cssExtract = require('extract-from-css')
1213
const logger = require('./logger')
1314
const splitRE = /\r?\n/g
@@ -28,13 +29,14 @@ function processScript (scriptPart) {
2829
return compileBabel(scriptPart.content)
2930
}
3031

31-
function processStyle (stylePart) {
32+
function processStyle (stylePart, filePath) {
3233
if (!stylePart) return {}
3334

3435
let cssCode = stylePart.content
3536
if (/^styl|stylus$/.test(stylePart.lang)) {
37+
const dir = path.dirname(filePath)
3638
const stylus = require('stylus')
37-
cssCode = stylus.render(stylePart.content)
39+
cssCode = stylus.render(stylePart.content, { paths: [dir, process.cwd()] })
3840
}
3941

4042
const cssNames = cssExtract.extractClasses(cssCode)
@@ -57,13 +59,13 @@ function changePartsIfFunctional (parts) {
5759
}
5860
}
5961

60-
module.exports = function (src, path) {
62+
module.exports = function (src, filePath) {
6163
var parts = vueCompiler.parseComponent(src, { pad: true })
6264

6365
changePartsIfFunctional(parts)
6466

6567
if (parts.script && parts.script.src) {
66-
parts.script.content = fs.readFileSync(join(path, '..', parts.script.src), 'utf8')
68+
parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8')
6769
}
6870

6971
const result = processScript(parts.script)
@@ -75,7 +77,7 @@ module.exports = function (src, path) {
7577
scriptSrc = parts.script.content
7678
}
7779

78-
const map = generateSourceMap(script, '', path, scriptSrc, inputMap)
80+
const map = generateSourceMap(script, '', filePath, scriptSrc, inputMap)
7981
let output = ';(function(){\n' + script + '\n})()\n' +
8082
'if (module.exports.__esModule) module.exports = module.exports.default\n' +
8183
'var __vue__options__ = (typeof module.exports === "function"' +
@@ -84,7 +86,7 @@ module.exports = function (src, path) {
8486

8587
if (parts.template) {
8688
if (parts.template.src) {
87-
parts.template.content = fs.readFileSync(join(path, '..', parts.template.src), 'utf8')
89+
parts.template.content = fs.readFileSync(join(filePath, '..', parts.template.src), 'utf8')
8890
}
8991

9092
const renderFunctions = compileTemplate(parts.template)
@@ -107,7 +109,7 @@ module.exports = function (src, path) {
107109
}
108110

109111
const moduleName = ast.module === true ? '$style' : ast.module
110-
const styleObj = processStyle(ast)
112+
const styleObj = processStyle(ast, filePath)
111113

112114
return '\n this[\'' + moduleName + '\'] = ' + JSON.stringify(styleObj)
113115
}).filter(_ => _)

test/relative/resource.styl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
standard-space = 11px

test/resources/StylusRelative.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div class="testB"></div>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'Button',
8+
}
9+
</script>
10+
11+
12+
<style lang="styl" module>
13+
@import '../relative/resource';
14+
.testB {
15+
margin: standard-space;
16+
}
17+
</style>

test/stylus.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { shallow } from 'vue-test-utils'
22
import Stylus from './resources/Stylus.vue'
3+
import StylusRelative from './resources/StylusRelative.vue'
34

45
describe('processes .vue file with Stylus style', () => {
56
let wrapper
@@ -18,4 +19,8 @@ describe('processes .vue file with Stylus style', () => {
1819
it('should not bind from style tags without a module', () => {
1920
expect(wrapper.vm.$style.testC).toBeFalsy()
2021
})
22+
23+
it('should handle relative imports', () => {
24+
expect(() => shallow(StylusRelative)).not.toThrow()
25+
})
2126
})

0 commit comments

Comments
 (0)