Skip to content

Fix issue parsing relative imports in stylus files #54

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 4 commits into from
Feb 8, 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
18 changes: 10 additions & 8 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const compileTypescript = require('./compilers/typescript-compiler')
const compileCoffeeScript = require('./compilers/coffee-compiler')
const extractPropsFromFunctionalTemplate = require('./extract-props')
const fs = require('fs')
const join = require('path').join
const path = require('path')
const join = path.join
const cssExtract = require('extract-from-css')
const logger = require('./logger')
const splitRE = /\r?\n/g
Expand All @@ -28,13 +29,14 @@ function processScript (scriptPart) {
return compileBabel(scriptPart.content)
}

function processStyle (stylePart) {
function processStyle (stylePart, filePath) {
if (!stylePart) return {}

let cssCode = stylePart.content
if (/^styl|stylus$/.test(stylePart.lang)) {
const dir = path.dirname(filePath)
const stylus = require('stylus')
cssCode = stylus.render(stylePart.content)
cssCode = stylus.render(stylePart.content, { paths: [dir, process.cwd()] })
}

const cssNames = cssExtract.extractClasses(cssCode)
Expand All @@ -57,13 +59,13 @@ function changePartsIfFunctional (parts) {
}
}

module.exports = function (src, path) {
module.exports = function (src, filePath) {
var parts = vueCompiler.parseComponent(src, { pad: true })

changePartsIfFunctional(parts)

if (parts.script && parts.script.src) {
parts.script.content = fs.readFileSync(join(path, '..', parts.script.src), 'utf8')
parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8')
}

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

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

if (parts.template) {
if (parts.template.src) {
parts.template.content = fs.readFileSync(join(path, '..', parts.template.src), 'utf8')
parts.template.content = fs.readFileSync(join(filePath, '..', parts.template.src), 'utf8')
}

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

const moduleName = ast.module === true ? '$style' : ast.module
const styleObj = processStyle(ast)
const styleObj = processStyle(ast, filePath)

return '\n this[\'' + moduleName + '\'] = ' + JSON.stringify(styleObj)
}).filter(_ => _)
Expand Down
1 change: 1 addition & 0 deletions test/relative/resource.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
standard-space = 11px
17 changes: 17 additions & 0 deletions test/resources/StylusRelative.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<template>
<div class="testB"></div>
</template>

<script>
export default {
name: 'Button',
}
</script>


<style lang="styl" module>
@import '../relative/resource';
.testB {
margin: standard-space;
}
</style>
5 changes: 5 additions & 0 deletions test/stylus.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { shallow } from 'vue-test-utils'
import Stylus from './resources/Stylus.vue'
import StylusRelative from './resources/StylusRelative.vue'

describe('processes .vue file with Stylus style', () => {
let wrapper
Expand All @@ -18,4 +19,8 @@ describe('processes .vue file with Stylus style', () => {
it('should not bind from style tags without a module', () => {
expect(wrapper.vm.$style.testC).toBeFalsy()
})

it('should handle relative imports', () => {
expect(() => shallow(StylusRelative)).not.toThrow()
})
})