Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 084c874

Browse files
committed
Add scss support
1 parent 4d5d80f commit 084c874

File tree

12 files changed

+91
-25
lines changed

12 files changed

+91
-25
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@
4141
"debug": "^2.6.0",
4242
"html-minifier": "^3.2.3",
4343
"magic-string": "^0.19.0",
44+
"node-sass": "^4.5.0",
4445
"parse5": "^3.0.1",
4546
"postcss": "^5.2.11",
4647
"postcss-modules": "^0.6.4",
4748
"posthtml": "^0.9.2",
49+
"posthtml-attrs-parser": "^0.1.1",
4850
"rollup-pluginutils": "^2.0.1",
4951
"vue-template-compiler": "^2.1.10",
5052
"vue-template-es2015-compiler": "^1.5.0",
51-
"vue-template-validator": "^1.1.5",
52-
"posthtml-attrs-parser": "^0.1.1"
53+
"vue-template-validator": "^1.1.5"
5354
},
5455
"devDependencies": {
5556
"babel-eslint": "^7.1.1",

src/options.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ export default {
3939
disableCssModuleStaticReplacement: false,
4040
modules: {
4141
generateScopedName: '[name]__[local]___[hash:base64:5]'
42-
}
42+
},
43+
scss: {}
4344
}

src/style/css.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import postcss from 'postcss'
22
import modules from 'postcss-modules'
33

4-
function compileModule (code, map, options) {
4+
function compileModule (code, map, source, options) {
55
let style
66

77
return postcss([
88
modules({
99
getJSON (filename, json) {
1010
style = json
1111
},
12-
...(options.modules || {})
12+
...options.modules
1313
})
14-
]).process(code, { map: { inline: false, prev: map } })
14+
]).process(code, { map: { inline: false, prev: map }, from: source.id, to: source.id })
1515
.then(
1616
result => ({ code: result.css, map: result.map, module: style }),
1717
error => {
@@ -24,7 +24,7 @@ export default async function (promise, options) {
2424
const { code, map } = ('$compiled' in style) ? style.$compiled : style
2525

2626
if (style.module === true) {
27-
return compileModule(code, map, options).then(compiled => {
27+
return compileModule(code, map, style, options).then(compiled => {
2828
if (style.$compiled) {
2929
compiled.$prev = style.$compiled
3030
}

src/style/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function compile (style, options) {
1212
if (style.lang === 'css') {
1313
output = await compileCSS(style, options)
1414
} else {
15-
output = await compileCSS(compilers[style.lang].call(null, style, options), options)
15+
output = await compileCSS(await compilers[style.lang].call(null, style, options), options)
1616
}
1717

1818
return output

src/style/scss.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1-
export default function () {
1+
import sass from 'node-sass'
2+
3+
export default function (style, options) {
4+
const { css, map } = sass.renderSync({
5+
file: style.id,
6+
data: style.code,
7+
omitSourceMapUrl: true,
8+
sourceMap: true,
9+
outFile: style.id,
10+
...options.scss
11+
})
12+
13+
style.$compiled = {
14+
code: css.toString(),
15+
map: map.toString()
16+
}
17+
18+
return style
219
}

test/expects/css-modules-static.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
var cssModulesStatic = { template: "<div class=\"file__test keep-me\">Foo</div>",cssModules: {"test":"file__test"},};
1+
var cssModulesStatic = { template: "<div class=\"css-modules-static__test keep-me\">Foo</div>",cssModules: {"test":"css-modules-static__test"},};
22

33
export default cssModulesStatic;

test/expects/css-modules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
var cssModules = { template: "<div :class=\"$style.test\">Foo</div>",cssModules: {"test":"file__test"},};
1+
var cssModules = { template: "<div :class=\"$style.test\">Foo</div>",cssModules: {"test":"css-modules__test"},};
22

33
export default cssModules;

test/expects/scss.css

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.scss__test {
2+
color: red; }

test/expects/scss.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var scss = { template: "<div class=\"scss__test\"></div>",cssModules: {"test":"scss__test"},};
2+
3+
export default scss;

test/fixtures/scss.vue

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template>
2+
<div class="test"></div>
3+
</template>
4+
5+
<script lang="babel">
6+
export default {}
7+
</script>
8+
9+
10+
<style lang="scss" module>
11+
$var: red;
12+
13+
.test {
14+
color: red;
15+
}
16+
</style>

test/test.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ function test(name) {
1818
var entry = './fixtures/' + name + '.vue'
1919
var expected = read('expects/' + name + '.js').replace(/\r/g, '')
2020
var actualCss
21-
var cssHandler = function (css) {
22-
actualCss = css
21+
var cssHandler = function (css, styles) {
22+
if (['scss'].indexOf(name) > -1) {
23+
actualCss = styles[0].$compiled.code
24+
} else {
25+
actualCss = css
26+
}
2327
}
2428

2529
return rollup.rollup({
@@ -37,7 +41,7 @@ function test(name) {
3741
assert.equal(code.trim(), expected.trim(), 'should compile code correctly')
3842

3943
// Check css output
40-
if (['style', 'css-modules', 'css-modules-static'].indexOf(name) > -1) {
44+
if (['style', 'css-modules', 'css-modules-static', 'scss'].indexOf(name) > -1) {
4145
var css = read('expects/' + name + '.css')
4246
assert.equal(css.trim(), actualCss.trim(), 'should output style tag content')
4347
} else {

yarn.lock

+33-11
Original file line numberDiff line numberDiff line change
@@ -2809,10 +2809,6 @@ lodash.flatten@^4.2.0:
28092809
version "4.4.0"
28102810
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
28112811

2812-
lodash.get@^4.0.2:
2813-
version "4.4.2"
2814-
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
2815-
28162812
lodash.isarguments@^3.0.0:
28172813
version "3.1.0"
28182814
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"
@@ -2849,6 +2845,10 @@ lodash.merge@^4.4.0:
28492845
version "4.6.0"
28502846
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.0.tgz#69884ba144ac33fe699737a6086deffadd0f89c5"
28512847

2848+
lodash.mergewith@^4.6.0:
2849+
version "4.6.0"
2850+
resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.0.tgz#150cf0a16791f5903b8891eab154609274bdea55"
2851+
28522852
lodash.partialright@^4.1.4:
28532853
version "4.2.1"
28542854
resolved "https://registry.yarnpkg.com/lodash.partialright/-/lodash.partialright-4.2.1.tgz#0130d80e83363264d40074f329b8a3e7a8a1cc4b"
@@ -3197,6 +3197,29 @@ node-sass@^3.4.2:
31973197
request "^2.61.0"
31983198
sass-graph "^2.1.1"
31993199

3200+
node-sass@^4.5.0:
3201+
version "4.5.0"
3202+
resolved "https://registry.yarnpkg.com/node-sass/-/node-sass-4.5.0.tgz#532e37bad0ce587348c831535dbc98ea4289508b"
3203+
dependencies:
3204+
async-foreach "^0.1.3"
3205+
chalk "^1.1.1"
3206+
cross-spawn "^3.0.0"
3207+
gaze "^1.0.0"
3208+
get-stdin "^4.0.1"
3209+
glob "^7.0.3"
3210+
in-publish "^2.0.0"
3211+
lodash.assign "^4.2.0"
3212+
lodash.clonedeep "^4.3.2"
3213+
lodash.mergewith "^4.6.0"
3214+
meow "^3.7.0"
3215+
mkdirp "^0.5.1"
3216+
nan "^2.3.2"
3217+
node-gyp "^3.3.1"
3218+
npmlog "^4.0.0"
3219+
request "^2.61.0"
3220+
sass-graph "^2.1.1"
3221+
stdout-stream "^1.4.0"
3222+
32003223
node-uuid@~1.4.7:
32013224
version "1.4.7"
32023225
resolved "https://registry.yarnpkg.com/node-uuid/-/node-uuid-1.4.7.tgz#6da5a17668c4b3dd59623bda11cf7fa4c1f60a6f"
@@ -3508,13 +3531,6 @@ posthtml-attrs-parser@^0.1.1:
35083531
dependencies:
35093532
object-assign "^4.0.1"
35103533

3511-
posthtml-css-modules@^0.1.1:
3512-
version "0.1.1"
3513-
resolved "https://registry.yarnpkg.com/posthtml-css-modules/-/posthtml-css-modules-0.1.1.tgz#0d5f38e1a0b8a06ca4462ec3425d649aa49c8676"
3514-
dependencies:
3515-
lodash.get "^4.0.2"
3516-
posthtml-attrs-parser "^0.1.1"
3517-
35183534
posthtml-parser@^0.2.0:
35193535
version "0.2.0"
35203536
resolved "https://registry.yarnpkg.com/posthtml-parser/-/posthtml-parser-0.2.0.tgz#8e4a742e3c10865a718b157d68b879896fa26b1b"
@@ -4070,6 +4086,12 @@ sshpk@^1.7.0:
40704086
jsbn "~0.1.0"
40714087
tweetnacl "~0.14.0"
40724088

4089+
stdout-stream@^1.4.0:
4090+
version "1.4.0"
4091+
resolved "https://registry.yarnpkg.com/stdout-stream/-/stdout-stream-1.4.0.tgz#a2c7c8587e54d9427ea9edb3ac3f2cd522df378b"
4092+
dependencies:
4093+
readable-stream "^2.0.1"
4094+
40734095
stream-array@^1.0.1:
40744096
version "1.1.2"
40754097
resolved "https://registry.yarnpkg.com/stream-array/-/stream-array-1.1.2.tgz#9e5f7345f2137c30ee3b498b9114e80b52bb7eb5"

0 commit comments

Comments
 (0)