Skip to content

Commit 2309253

Browse files
yohodopoeddyerburgh
authored andcommitted
feat: add pre and post transform system (#146)
1 parent 34dd1e6 commit 2309253

18 files changed

+402
-135
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,9 @@ __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>"`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { createTransformer } = require('babel-jest')
2+
module.exports = createTransformer({
3+
presets: ['@babel/preset-env'],
4+
plugins: ['transform-vue-jsx']
5+
})
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<template>
2-
<div :class="$style.testPcss"></div>
2+
<section>
3+
<div :class="$style.testPcss"></div>
4+
<div :class="$style.red"></div>
5+
</section>
36
</template>
47

58
<style lang="postcss">
69
.testPcss {
7-
background-color: purple;
10+
background: color(purple a(90%));
811
}
912
</style>
1013

1114
<style module lang="pcss">
12-
/* This syntax is for postcss-custom-properties */
13-
--primary-color: green;
14-
15-
/* This syntax is for postcss-nesting, but invalid as Pure CSS */
16-
body {
17-
@media screen {
18-
background-color: grey;
19-
}
15+
.red {
16+
background: color(red a(90%));
2017
}
2118
</style>

e2e/__projects__/babel-config/components/Sass.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@
1313
.d
1414
background-color: blue
1515
</style>
16+
17+
<style lang="sass" module themed>
18+
.e
19+
background-color: blue
20+
</style>

e2e/__projects__/babel-config/components/Scss.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<template>
2-
<div class="testA"></div>
2+
<div class="testA">
3+
<span :class="this.$style.g"></span>
4+
<span :class="this.$style.dark.f"></span>
5+
</div>
36
</template>
47

58
<style module lang="scss">
@@ -15,3 +18,9 @@
1518
background-color: red;
1619
}
1720
</style>
21+
22+
<style lang="scss" module themed>
23+
.f {
24+
background-color: red;
25+
}
26+
</style>

e2e/__projects__/babel-config/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"babel-plugin-transform-vue-jsx": "^3.7.0",
2222
"jest": "^23.6.0",
2323
"node-sass": "^4.11.0",
24+
"postcss": "^7.0.13",
25+
"postcss-color-function": "^4.0.1",
2426
"vue-jest": "file:../../../"
2527
},
2628
"jest": {
@@ -40,6 +42,11 @@
4042
"vue-jest": {
4143
"pug": {
4244
"basedir": "./"
45+
},
46+
"transform": {
47+
"^scss$": "./scssTransform.js",
48+
"^pcss|postcss$": "./pcssTransform.js",
49+
"^js$": "./babel-transformer.js"
4350
}
4451
}
4552
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const postcss = require('postcss')
2+
var colorFunction = require('postcss-color-function')
3+
module.exports = {
4+
process: function(content, filepath, config, attrs) {
5+
return postcss([colorFunction()]).process(content).css
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const cssExtract = require('extract-from-css')
2+
module.exports = {
3+
postProcess: function postProcess(src, filepath, config, attrs) {
4+
const cssNames = cssExtract.extractClasses(src)
5+
const obj = {}
6+
for (let i = 0, l = cssNames.length; i < l; i++) {
7+
obj[cssNames[i]] = cssNames[i]
8+
}
9+
10+
if (attrs.themed) {
11+
return {
12+
light: obj,
13+
dark: obj
14+
}
15+
}
16+
return obj
17+
},
18+
preProcess: function postProcess(src, filepath, config, attrs) {
19+
return `${src}\n .g{width: 10px}`
20+
}
21+
}

e2e/__projects__/babel-config/test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Constructor from './components/Constructor.vue'
2525

2626
test('processes .vue files', () => {
2727
const wrapper = mount(Basic)
28+
expect(wrapper.vm.msg).toEqual('Welcome to Your Vue.js App')
2829
wrapper.vm.toggleClass()
2930
})
3031

@@ -119,7 +120,9 @@ it('processes Less', () => {
119120

120121
it('processes PostCSS', () => {
121122
const wrapper = mount(PostCss)
122-
expect(wrapper.is('div')).toBeTruthy()
123+
expect(wrapper.is('section')).toBeTruthy()
124+
expect(wrapper.vm.$style.red).toEqual('red')
125+
expect(wrapper.html()).toMatchSnapshot()
123126
})
124127

125128
test('processes pug templates', () => {
@@ -145,6 +148,7 @@ it('processes Sass', () => {
145148
expect(wrapper.vm.$style.a).toEqual('a')
146149
expect(wrapper.vm.$style.b).toEqual('b')
147150
expect(wrapper.vm.$style.c).toEqual('c')
151+
expect(wrapper.vm.$style.light).toBeUndefined()
148152
})
149153

150154
it('processes SCSS', () => {
@@ -154,6 +158,21 @@ it('processes SCSS', () => {
154158
expect(wrapper.vm.$style.c).toEqual('c')
155159
})
156160

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+
157176
test('process Stylus', () => {
158177
const wrapper = mount(Stylus)
159178
expect(wrapper.vm).toBeTruthy()

e2e/test-runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function runTest(dir) {
4343
run('npm install --silent')
4444

4545
log('Running tests')
46-
run('npm run test')
46+
run('npm run test')
4747

4848
success(`(${dir}) Complete`)
4949
}

0 commit comments

Comments
 (0)