Skip to content

Commit c342141

Browse files
committed
satisfy scss spec
1 parent 782a9c9 commit c342141

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed

lib/process-style/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
const path = require('path')
21
const cssExtract = require('extract-from-css')
32

43
module.exports = function processStyle (stylePart, filePath, config) {
54
if (!stylePart) return {}
65

7-
const dir = path.dirname(filePath)
8-
const processStyleByLang = lang => require('./' + lang)(stylePart.content, dir, config)
6+
const processStyleByLang = lang => require('./' + lang)(stylePart.content, filePath, config)
97

10-
let cssCode = ''
8+
let cssCode = stylePart.content
119
switch (stylePart.lang) {
1210
case 'styl':
1311
case 'stylus':

lib/process-style/scss.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
const path = require('path')
22
const fs = require('fs')
33
const sass = require('node-sass')
4-
const cwd = process.cwd()
54

6-
module.exports = (content, dir, config) => {
7-
const getRelativeImportPath = (oldImportPath, absoluteImportPath) => (/^\~/.test(oldImportPath))
8-
? oldImportPath
9-
: path.relative(cwd, absoluteImportPath)
5+
const rewriteImports = (content, filePath) => content.replace(/@import\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g, (entire, single, double, unquoted) => {
6+
const oldImportPath = single || double || unquoted
7+
const absoluteImportPath = path.join(path.dirname(filePath), oldImportPath)
8+
const lastCharacter = entire[entire.length - 1]
9+
const quote = lastCharacter === "'" || lastCharacter === '"' ? lastCharacter : ''
10+
const importPath = path.relative(process.cwd(), absoluteImportPath)
11+
return '@import ' + quote + importPath + quote
12+
})
1013

14+
module.exports = (content, filePath, config) => {
1115
let scssResources = ''
1216
if (config.resources && config.resources.scss) {
1317
scssResources = config.resources.scss
14-
.map(scssResource => path.resolve(cwd, scssResource))
18+
.map(scssResource => path.resolve(process.cwd(), scssResource))
1519
.filter(scssResourcePath => fs.existsSync(scssResourcePath))
16-
.map(scssResourcePath => fs.readFileSync(scssResourcePath).toString()
17-
.replace(/@import\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g, (entire, single, double, unquoted) => {
18-
const oldImportPath = single || double || unquoted
19-
const absoluteImportPath = path.join(path.dirname(scssResourcePath), oldImportPath)
20-
const relImportPath = getRelativeImportPath(oldImportPath, absoluteImportPath)
21-
const newImportPath = relImportPath.split(path.sep).join('/')
22-
const lastCharacter = entire[entire.length - 1]
23-
const quote = lastCharacter === "'" || lastCharacter === '"' ? lastCharacter : ''
24-
return '@import ' + quote + newImportPath + quote
25-
})
26-
)
20+
.map(scssResourcePath => rewriteImports(fs.readFileSync(scssResourcePath).toString(), scssResourcePath))
2721
.join('\n')
2822
}
2923

3024
return sass.renderSync({
31-
data: scssResources + content,
25+
data: scssResources + rewriteImports(content, filePath),
3226
outputStyle: 'compressed'
3327
}).css.toString()
3428
}

lib/process-style/stylus.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
const stylus = require('stylus')
2-
module.exports = (content, dir, config) => stylus.render(content, { paths: [dir, process.cwd()] })
2+
const path = require('path')
3+
4+
module.exports = (content, filePath, config) => stylus.render(
5+
content, {
6+
paths: [path.dirname(filePath), process.cwd()]
7+
}
8+
)

lib/process.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ module.exports = function (src, filePath) {
8888

8989
const styleStr = parts.styles.map(ast => {
9090
if (!module) return
91+
9192
const styleObj = (/^sass|less|pcss|postcss/.test(ast.lang))
9293
? {}
9394
: processStyle(ast, filePath, config)

test/scss.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { shallow } from 'vue-test-utils'
22
import Sass from './resources/Sass.vue'
33
import SassModule from './resources/SassModule.vue'
44

5-
describe('processes .vue file with Stylus style', () => {
5+
describe('processes .vue file with scss style', () => {
66
it('does not error on scss or sass', () => {
77
const wrapper = shallow(Sass)
88
expect(wrapper.classes()).toContain('testA')

0 commit comments

Comments
 (0)