Skip to content

Commit b9bbbc1

Browse files
authored
Merge pull request #195 from mrvasia/postcss
added support postcss
2 parents 01e0710 + 6ad95c4 commit b9bbbc1

File tree

8 files changed

+127
-10
lines changed

8 files changed

+127
-10
lines changed

lib/compilers/postcss-compiler.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const postcss = require('postcss')
2+
const postcssrc = require('postcss-load-config')
3+
const ctx = { parser: true, map: 'inline' }
4+
const { plugins } = postcssrc.sync(ctx)
5+
const logger = require('../logger')
6+
const getVueJestConfig = require('../get-vue-jest-config')
7+
const ensureRequire = require('../ensure-require')
8+
9+
let prevCheckIsAsync = null
10+
function hasAsyncPlugin () {
11+
if (prevCheckIsAsync !== null) {
12+
return prevCheckIsAsync
13+
}
14+
const result = postcss(plugins)
15+
.process('', {
16+
from: undefined
17+
})
18+
19+
if (result.processing) {
20+
prevCheckIsAsync = true
21+
return prevCheckIsAsync
22+
}
23+
for (const plugin of result.processor.plugins) {
24+
const promise = result.run(plugin)
25+
if (typeof promise === 'object' && typeof promise.then === 'function') {
26+
prevCheckIsAsync = true
27+
break
28+
}
29+
}
30+
if (prevCheckIsAsync === null) {
31+
prevCheckIsAsync = false
32+
}
33+
34+
return prevCheckIsAsync
35+
}
36+
37+
function catchError (error, filePath, jestConfig) {
38+
if (!getVueJestConfig(jestConfig).hideStyleWarn) {
39+
logger.warn(`There was an error rendering the POSTCSS in ${filePath}. `)
40+
logger.warn(`Error while compiling styles: ${error}`)
41+
}
42+
}
43+
44+
module.exports = (content, filePath, jestConfig) => {
45+
ensureRequire('postcss', ['postcss'])
46+
47+
let css = null
48+
49+
const res = postcss(plugins)
50+
.process(content, {
51+
from: undefined
52+
})
53+
54+
if (hasAsyncPlugin()) {
55+
res
56+
.then(result => {
57+
css = result.css || ''
58+
})
59+
.catch((e) => {
60+
css = ''
61+
catchError(e, filePath, jestConfig)
62+
})
63+
64+
while (css === null) { //eslint-disable-line
65+
require('deasync').sleep(100)
66+
}
67+
68+
return css
69+
}
70+
71+
try {
72+
return res.css
73+
} catch (e) {
74+
catchError(e, filePath, jestConfig)
75+
return ''
76+
}
77+
}

lib/process-style.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ module.exports = function processStyle (stylePart, filePath, jestConfig = {}) {
2222
case 'sass':
2323
cssCode = processStyleByLang('sass')
2424
break
25+
case 'pcss':
26+
case 'postcss':
27+
cssCode = processStyleByLang('postcss')
28+
break
2529
}
2630

2731
const cssNames = cssExtract.extractClasses(cssCode)

lib/process.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ module.exports = function (src, filePath, jestConfig) {
8080
}
8181

8282
if (Array.isArray(parts.styles) && parts.styles.length > 0) {
83-
if ((parts.styles.some(ast => /^less|pcss|postcss/.test(ast.lang))) && logger.shouldLogStyleWarn) {
84-
!vueJestConfig.hideStyleWarn && logger.warn('Less and PostCSS are not currently compiled by vue-jest')
83+
if ((parts.styles.some(ast => /^less/.test(ast.lang))) && logger.shouldLogStyleWarn) {
84+
!vueJestConfig.hideStyleWarn && logger.warn('Less are not currently compiled by vue-jest')
8585
logger.shouldLogStyleWarn = false
8686
}
8787

8888
const styleStr = parts.styles
8989
.filter(ast => ast.module)
9090
.map(ast => {
91-
const styleObj = (/^less|pcss|postcss/.test(ast.lang))
91+
const styleObj = (/^less/.test(ast.lang))
9292
? {}
9393
: processStyle(ast, filePath, jestConfig)
9494

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
"jade": "^1.11.0",
4747
"jest": "^20.0.4",
4848
"node-sass": "^4.10.0",
49+
"postcss": "^7.0.18",
50+
"postcss-css-variables": "^0.13.0",
51+
"postcss-load-config": "^2.1.0",
52+
"postcss-nested": "^4.1.2",
4953
"pug": "^2.0.0-rc.3",
5054
"stylus": "^0.54.5",
5155
"typescript": "^2.5.2",
@@ -60,6 +64,7 @@
6064
"dependencies": {
6165
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
6266
"chalk": "^2.1.0",
67+
"deasync": "^0.1.15",
6368
"extract-from-css": "^0.4.4",
6469
"find-babel-config": "^1.1.0",
6570
"js-beautify": "^1.6.14",

postcss.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
plugins: {
3+
'postcss-css-variables': {},
4+
'postcss-nested': {}
5+
}
6+
}

test/postcss.spec.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@ import PostCss from './resources/PostCss.vue'
33
import PostCssModule from './resources/PostCssModule.vue'
44

55
describe('processes .vue file with PostCSS style', () => {
6-
it('does not error on pcss/postcss', () => {
7-
const wrapper = shallowMount(PostCss)
6+
const wrapper = shallowMount(PostCss)
7+
8+
it('does stick classes to component', () => {
89
expect(wrapper.classes()).toContain('testPcss')
910
})
1011

11-
it('does not error on pcss/postcss module', () => {
12-
expect(() => shallowMount(PostCssModule)).not.toThrow()
12+
it('does stick next classes to component', () => {
13+
expect(wrapper.find('span').classes()).toContain('nestedCom')
14+
})
15+
16+
const wrapperModules = shallowMount(PostCssModule)
17+
18+
const classListModules = Object.keys(wrapperModules.vm.$style)
19+
20+
it('does inject classes to $style', () => {
21+
expect(classListModules).toContain('testPcss')
22+
})
23+
24+
it('does inject nested classes to $style', () => {
25+
expect(classListModules).toContain('nestedClass')
1326
})
1427
})

test/resources/PostCss.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<template>
2-
<div class="testPcss"></div>
2+
<div class="testPcss">
3+
<span class="nestedCom"></span>
4+
</div>
35
</template>
46

57
<style lang="postcss">
68
.testPcss {
79
background-color: purple;
10+
11+
& .nestedCom {
12+
background-color: purple;
13+
}
814
}
915
</style>
1016

test/resources/PostCssModule.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@
55
<style module lang="postcss">
66
.testPcss {
77
background-color: purple;
8+
9+
& .nestedClass {
10+
background-color: purple;
11+
}
812
}
913
</style>
1014

1115
<style module lang="pcss">
1216
/* This syntax is for postcss-custom-properties */
13-
--primary-color: green;
17+
:root {
18+
primary-color: green;
19+
}
1420
1521
/* This syntax is for postcss-nesting, but invalid as Pure CSS */
1622
body {
1723
@media screen {
18-
background-color: grey;
24+
background-color: var(--primary-color);
1925
}
2026
}
2127
</style>

0 commit comments

Comments
 (0)