Skip to content

added support postcss #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions lib/compilers/postcss-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const postcss = require('postcss')
const postcssrc = require('postcss-load-config')
const ctx = { parser: true, map: 'inline' }
const { plugins } = postcssrc.sync(ctx)
const logger = require('../logger')
const getVueJestConfig = require('../get-vue-jest-config')
const ensureRequire = require('../ensure-require')

let prevCheckIsAsync = null
function hasAsyncPlugin () {
if (prevCheckIsAsync !== null) {
return prevCheckIsAsync
}
const result = postcss(plugins)
.process('', {
from: undefined
})

if (result.processing) {
prevCheckIsAsync = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we could return early at this point

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return prevCheckIsAsync
}
for (const plugin of result.processor.plugins) {
const promise = result.run(plugin)
if (typeof promise === 'object' && typeof promise.then === 'function') {
prevCheckIsAsync = true
break
}
}
if (prevCheckIsAsync === null) {
prevCheckIsAsync = false
}

return prevCheckIsAsync
}

function catchError (error, filePath, jestConfig) {
if (!getVueJestConfig(jestConfig).hideStyleWarn) {
logger.warn(`There was an error rendering the POSTCSS in ${filePath}. `)
logger.warn(`Error while compiling styles: ${error}`)
}
}

module.exports = (content, filePath, jestConfig) => {
ensureRequire('postcss', ['postcss'])

let css = null

const res = postcss(plugins)
.process(content, {
from: undefined
})

if (hasAsyncPlugin()) {
res
.then(result => {
css = result.css || ''
})
.catch((e) => {
css = ''
catchError(e, filePath, jestConfig)
})

while (css === null) { //eslint-disable-line
require('deasync').sleep(100)
}

return css
}

try {
return res.css
} catch (e) {
catchError(e, filePath, jestConfig)
return ''
}
}
4 changes: 4 additions & 0 deletions lib/process-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ module.exports = function processStyle (stylePart, filePath, jestConfig = {}) {
case 'sass':
cssCode = processStyleByLang('sass')
break
case 'pcss':
case 'postcss':
cssCode = processStyleByLang('postcss')
break
}

const cssNames = cssExtract.extractClasses(cssCode)
Expand Down
6 changes: 3 additions & 3 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ module.exports = function (src, filePath, jestConfig) {
}

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

const styleStr = parts.styles
.filter(ast => ast.module)
.map(ast => {
const styleObj = (/^less|pcss|postcss/.test(ast.lang))
const styleObj = (/^less/.test(ast.lang))
? {}
: processStyle(ast, filePath, jestConfig)

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
"jade": "^1.11.0",
"jest": "^20.0.4",
"node-sass": "^4.10.0",
"postcss": "^7.0.18",
"postcss-css-variables": "^0.13.0",
"postcss-load-config": "^2.1.0",
"postcss-nested": "^4.1.2",
"pug": "^2.0.0-rc.3",
"stylus": "^0.54.5",
"typescript": "^2.5.2",
Expand All @@ -60,6 +64,7 @@
"dependencies": {
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"chalk": "^2.1.0",
"deasync": "^0.1.15",
"extract-from-css": "^0.4.4",
"find-babel-config": "^1.1.0",
"js-beautify": "^1.6.14",
Expand Down
6 changes: 6 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
'postcss-css-variables': {},
'postcss-nested': {}
}
}
21 changes: 17 additions & 4 deletions test/postcss.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@ import PostCss from './resources/PostCss.vue'
import PostCssModule from './resources/PostCssModule.vue'

describe('processes .vue file with PostCSS style', () => {
it('does not error on pcss/postcss', () => {
const wrapper = shallowMount(PostCss)
const wrapper = shallowMount(PostCss)

it('does stick classes to component', () => {
expect(wrapper.classes()).toContain('testPcss')
})

it('does not error on pcss/postcss module', () => {
expect(() => shallowMount(PostCssModule)).not.toThrow()
it('does stick next classes to component', () => {
expect(wrapper.find('span').classes()).toContain('nestedCom')
})

const wrapperModules = shallowMount(PostCssModule)

const classListModules = Object.keys(wrapperModules.vm.$style)

it('does inject classes to $style', () => {
expect(classListModules).toContain('testPcss')
})

it('does inject nested classes to $style', () => {
expect(classListModules).toContain('nestedClass')
})
})
8 changes: 7 additions & 1 deletion test/resources/PostCss.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<template>
<div class="testPcss"></div>
<div class="testPcss">
<span class="nestedCom"></span>
</div>
</template>

<style lang="postcss">
.testPcss {
background-color: purple;

& .nestedCom {
background-color: purple;
}
}
</style>

Expand Down
10 changes: 8 additions & 2 deletions test/resources/PostCssModule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
<style module lang="postcss">
.testPcss {
background-color: purple;

& .nestedClass {
background-color: purple;
}
}
</style>

<style module lang="pcss">
/* This syntax is for postcss-custom-properties */
--primary-color: green;
:root {
primary-color: green;
}

/* This syntax is for postcss-nesting, but invalid as Pure CSS */
body {
@media screen {
background-color: grey;
background-color: var(--primary-color);
}
}
</style>