Skip to content

Commit 1ba6ea3

Browse files
authored
refactor: use component compiler utils to compile style (#150)
1 parent bc70876 commit 1ba6ea3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+924
-822
lines changed

e2e/__projects__/babel-config/babel-transformer.js

-5
This file was deleted.

e2e/__projects__/babel-in-package/package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"@babel/core": "^7.2.2",
1616
"@babel/preset-env": "^7.2.3",
1717
"@vue/test-utils": "^1.0.0-beta.28",
18-
"babel-core": "^7.0.0-bridge.0",
19-
"jest": "^23.6.0",
18+
"jest": "^24.0.0",
2019
"vue-jest": "file:../../../"
2120
},
2221
"jest": {
@@ -28,9 +27,6 @@
2827
"transform": {
2928
"^.+\\.js$": "babel-jest",
3029
"^.+\\.vue$": "vue-jest"
31-
},
32-
"moduleNameMapper": {
33-
"^~?__styles/(.*)$": "<rootDir>/components/styles/$1"
3430
}
3531
},
3632
"babel": {

e2e/__projects__/babel-config/__snapshots__/test.js.snap e2e/__projects__/basic/__snapshots__/test.js.snap

-6
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,3 @@ __options__.staticRenderFns = staticRenderFns
140140
141141
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIkJhc2ljU3JjLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUhBO0FBS0E7QUFQQTtBQVNBO0FBQ0E7QUFDQTtBQUNBO0FBRkE7QUFJQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBSEE7QUFqQkEiLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCB7XG4gIG5hbWU6ICdiYXNpYycsXG4gIGNvbXB1dGVkOiB7XG4gICAgaGVhZGluZ0NsYXNzZXM6IGZ1bmN0aW9uIGhlYWRpbmdDbGFzc2VzKCkge1xuICAgICAgcmV0dXJuIHtcbiAgICAgICAgcmVkOiB0aGlzLmlzQ3JhenksXG4gICAgICAgIGJsdWU6ICF0aGlzLmlzQ3JhenksXG4gICAgICAgIHNoYWRvdzogdGhpcy5pc0NyYXp5XG4gICAgICB9XG4gICAgfVxuICB9LFxuICBkYXRhOiBmdW5jdGlvbiBkYXRhKCkge1xuICAgIHJldHVybiB7XG4gICAgICBtc2c6ICdXZWxjb21lIHRvIFlvdXIgVnVlLmpzIEFwcCcsXG4gICAgICBpc0NyYXp5OiBmYWxzZVxuICAgIH1cbiAgfSxcbiAgbWV0aG9kczoge1xuICAgIHRvZ2dsZUNsYXNzOiBmdW5jdGlvbiB0b2dnbGVDbGFzcygpIHtcbiAgICAgIHRoaXMuaXNDcmF6eSA9ICF0aGlzLmlzQ3JhenlcbiAgICB9XG4gIH1cbn1cbiJdfQ=="
142142
`;
143-
144-
exports[`processes PostCSS 1`] = `"<section><div></div> <div class=\\"red\\"></div></section>"`;
145-
146-
exports[`processes SCSS using user specified post transforms 1`] = `"<div class=\\"testA\\"><span class=\\"g\\"></span> <span class=\\"f\\"></span></div>"`;
147-
148-
exports[`processes SCSS using user specified pre transforms 1`] = `"<div class=\\"testA\\"><span class=\\"g\\"></span> <span class=\\"f\\"></span></div>"`;

e2e/__projects__/babel-config/package.json e2e/__projects__/basic/package.json

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "babel-config",
2+
"name": "basic",
33
"version": "1.0.0",
44
"main": "index.js",
55
"license": "MIT",
@@ -15,14 +15,10 @@
1515
"@babel/core": "^7.2.2",
1616
"@babel/preset-env": "^7.2.3",
1717
"@vue/test-utils": "^1.0.0-beta.28",
18-
"babel-core": "^7.0.0-bridge.0",
1918
"babel-helper-vue-jsx-merge-props": "^2.0.3",
2019
"babel-plugin-syntax-jsx": "^6.18.0",
2120
"babel-plugin-transform-vue-jsx": "^3.7.0",
22-
"jest": "^23.6.0",
23-
"node-sass": "^4.11.0",
24-
"postcss": "^7.0.13",
25-
"postcss-color-function": "^4.0.1",
21+
"jest": "^24.0.0",
2622
"vue-jest": "file:../../../"
2723
},
2824
"jest": {
@@ -42,11 +38,6 @@
4238
"vue-jest": {
4339
"pug": {
4440
"basedir": "./"
45-
},
46-
"transform": {
47-
"^scss$": "./scssTransform.js",
48-
"^pcss|postcss$": "./pcssTransform.js",
49-
"^js$": "./babel-transformer.js"
5041
}
5142
}
5243
}

e2e/__projects__/babel-config/test.js e2e/__projects__/basic/test.js

-52
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { mount } from '@vue/test-utils'
22
import TypeScript from './components/TypeScript.vue'
3-
import Stylus from './components/Stylus.vue'
43
import { resolve } from 'path'
54
import { readFileSync } from 'fs'
65
import jestVue from 'vue-jest'
7-
import Scss from './components/Scss.vue'
8-
import Sass from './components/Sass.vue'
96
import RenderFunction from './components/RenderFunction.vue'
107
import Jade from './components/Jade.vue'
118
import FunctionalSFC from './components/FunctionalSFC.vue'
@@ -16,8 +13,6 @@ import Coffee from './components/Coffee.vue'
1613
import CoffeeScript from './components/CoffeeScript.vue'
1714
import FunctionalSFCParent from './components/FunctionalSFCParent.vue'
1815
import NoScript from './components/NoScript.vue'
19-
import Less from './components/Less.vue'
20-
import PostCss from './components/PostCss.vue'
2116
import Pug from './components/Pug.vue'
2217
import PugRelative from './components/PugRelativeExtends.vue'
2318
import Jsx from './components/Jsx.vue'
@@ -113,18 +108,6 @@ test('processes .vue file with jade template', () => {
113108
expect(wrapper.classes()).toContain('jade')
114109
})
115110

116-
it('processes Less', () => {
117-
const wrapper = mount(Less)
118-
expect(wrapper.is('div')).toBeTruthy()
119-
})
120-
121-
it('processes PostCSS', () => {
122-
const wrapper = mount(PostCss)
123-
expect(wrapper.is('section')).toBeTruthy()
124-
expect(wrapper.vm.$style.red).toEqual('red')
125-
expect(wrapper.html()).toMatchSnapshot()
126-
})
127-
128111
test('processes pug templates', () => {
129112
const wrapper = mount(Pug)
130113
expect(wrapper.is('div')).toBeTruthy()
@@ -142,38 +125,3 @@ test('processes SFC with no template', () => {
142125
const wrapper = mount(RenderFunction)
143126
expect(wrapper.is('section')).toBe(true)
144127
})
145-
146-
it('processes Sass', () => {
147-
const wrapper = mount(Sass)
148-
expect(wrapper.vm.$style.a).toEqual('a')
149-
expect(wrapper.vm.$style.b).toEqual('b')
150-
expect(wrapper.vm.$style.c).toEqual('c')
151-
expect(wrapper.vm.$style.light).toBeUndefined()
152-
})
153-
154-
it('processes SCSS', () => {
155-
const wrapper = mount(Scss)
156-
expect(wrapper.vm.$style.a).toEqual('a')
157-
expect(wrapper.vm.$style.b).toEqual('b')
158-
expect(wrapper.vm.$style.c).toEqual('c')
159-
})
160-
161-
test('processes SCSS using user specified post transforms', () => {
162-
const wrapper = mount(Scss)
163-
expect(wrapper.vm.$style.light.a).toBeUndefined()
164-
expect(wrapper.vm.$style.light.f).toEqual('f')
165-
expect(wrapper.vm.$style.dark.f).toEqual('f')
166-
expect(wrapper.vm.$style.dark.g).toEqual('g')
167-
expect(wrapper.html()).toMatchSnapshot()
168-
})
169-
170-
test('processes SCSS using user specified pre transforms', () => {
171-
const wrapper = mount(Scss)
172-
expect(wrapper.vm.$style.g).toEqual('g')
173-
expect(wrapper.html()).toMatchSnapshot()
174-
})
175-
176-
test('process Stylus', () => {
177-
const wrapper = mount(Stylus)
178-
expect(wrapper.vm).toBeTruthy()
179-
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env']
3+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
<template>
2-
<div class="testA">
2+
<div>
33
<span :class="this.$style.g"></span>
44
<span :class="this.$style.dark.f"></span>
55
</div>
66
</template>
77

8-
<style module lang="scss">
9-
@import '~__styles/scss-a';
10-
11-
.c {
8+
<style lang="scss" module themed>
9+
.f {
1210
background-color: red;
1311
}
1412
</style>
1513

16-
<style lang="scss">
14+
<style lang="scss" module>
1715
.d {
1816
background-color: red;
1917
}
2018
</style>
21-
22-
<style lang="scss" module themed>
23-
.f {
24-
background-color: red;
25-
}
26-
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "custom-transformers",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"private": true,
7+
"scripts": {
8+
"test": "jest --no-cache --coverage test.js"
9+
},
10+
"dependencies": {
11+
"vue": "^2.5.21",
12+
"vue-template-compiler": "^2.5.21"
13+
},
14+
"devDependencies": {
15+
"@babel/core": "^7.2.2",
16+
"@babel/preset-env": "^7.2.3",
17+
"@vue/test-utils": "^1.0.0-beta.28",
18+
"jest": "^24.0.0",
19+
"node-sass": "^4.11.0",
20+
"postcss": "^7.0.13",
21+
"postcss-color-function": "^4.0.1",
22+
"vue-jest": "file:../../../"
23+
},
24+
"jest": {
25+
"moduleFileExtensions": [
26+
"js",
27+
"json",
28+
"vue"
29+
],
30+
"transform": {
31+
"^.+\\.js$": "babel-jest",
32+
"^.+\\.vue$": "vue-jest"
33+
},
34+
"moduleNameMapper": {
35+
"^~?__styles/(.*)$": "<rootDir>/components/styles/$1"
36+
},
37+
"globals": {
38+
"vue-jest": {
39+
"transform": {
40+
"^scss$": "./scss-transformer.js",
41+
"^pcss|postcss$": "./pcss-transformer.js",
42+
"^js$": "./babel-transformer.js"
43+
}
44+
}
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
const cssExtract = require('extract-from-css')
22
module.exports = {
3-
postProcess: function postProcess(src, filepath, config, attrs) {
3+
preprocess: function preprocess(src, filepath, config, attrs) {
4+
return `${src}\n .g{width: 10px}`
5+
},
6+
postprocess: function postprocess(src, filepath, config, attrs) {
47
const cssNames = cssExtract.extractClasses(src)
58
const obj = {}
69
for (let i = 0, l = cssNames.length; i < l; i++) {
710
obj[cssNames[i]] = cssNames[i]
811
}
912

10-
if (attrs.themed) {
11-
return {
12-
light: obj,
13-
dark: obj
14-
}
13+
if (!attrs.themed) {
14+
return JSON.stringify(obj)
1515
}
16-
return obj
17-
},
18-
preProcess: function postProcess(src, filepath, config, attrs) {
19-
return `${src}\n .g{width: 10px}`
16+
17+
return JSON.stringify({
18+
light: obj,
19+
dark: obj
20+
})
2021
}
2122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { mount } from '@vue/test-utils'
2+
import Scss from './components/Scss.vue'
3+
4+
test('processes SCSS using user specified post transforms', () => {
5+
const wrapper = mount(Scss)
6+
expect(wrapper.vm.$style.light.a).toBeUndefined()
7+
expect(wrapper.vm.$style.light.f).toEqual('f')
8+
expect(wrapper.vm.$style.dark.f).toEqual('f')
9+
expect(wrapper.vm.$style.dark.g).toEqual('g')
10+
})
11+
12+
test('processes SCSS using user specified pre transforms', () => {
13+
const wrapper = mount(Scss)
14+
expect(wrapper.vm.$style.g).toEqual('g')
15+
})
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env']
3+
}

e2e/__projects__/babel-config/components/Less.vue e2e/__projects__/style/components/Less.vue

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
</template>
44

55
<style module lang="less">
6-
.testLess {
6+
.a {
77
background-color: @primary-color;
88
}
99
</style>
10-
11-
<style lang="less">
12-
@primary-color: #333;
13-
</style>
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<template>
22
<section>
3-
<div :class="$style.testPcss"></div>
4-
<div :class="$style.red"></div>
3+
<div :class="$style.a"></div>
4+
<div :class="$style.b"></div>
55
</section>
66
</template>
77

8-
<style lang="postcss">
9-
.testPcss {
8+
<style module lang="postcss">
9+
.a {
1010
background: color(purple a(90%));
1111
}
1212
</style>
1313

1414
<style module lang="pcss">
15-
.red {
15+
.b {
1616
background: color(red a(90%));
1717
}
1818
</style>
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<template>
2+
<div />
3+
</template>
4+
5+
<style lang="scss" module>
6+
@import '~__styles/scss-a';
7+
8+
.c {
9+
background-color: $primary-color;
10+
}
11+
</style>
12+
13+
<style lang="scss">
14+
.d {
15+
background-color: red;
16+
}
17+
</style>
18+
19+
<style lang="scss" module themed>
20+
.f {
21+
background-color: red;
22+
}
23+
</style>

e2e/__projects__/babel-config/components/Stylus.vue e2e/__projects__/style/components/Stylus.vue

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
<div />
33
</template>
44

5-
<script>
6-
export default {
7-
name: 'Button'
8-
}
9-
</script>
10-
115
<style lang="stylus" module="css">
126
@import './relative/resource';
13-
.testA {
7+
8+
.a {
149
background-color: red;
1510
}
1611
</style>
1712

1813
<style lang="styl" module>
19-
.testB {
14+
.b {
2015
background-color: blue;
2116
}
2217
</style>

0 commit comments

Comments
 (0)