Skip to content

Commit 4f3b52a

Browse files
committed
Support default config fallback
1 parent a79be0f commit 4f3b52a

10 files changed

Lines changed: 119 additions & 74 deletions

File tree

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"env": {
55
"browser": false,
66
"node": true,
7-
"mocha": true
7+
"jest": true
88
},
99
"ecmaFeatures": {
1010
"jsx": false,

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@
4949
"postcss-js": "^0.3.0",
5050
"postcss-load-config": "^1.2.0",
5151
"postcss-safe-parser": "^2.0.0",
52+
"postcss-scss": "^0.4.1",
5253
"postcss-simple-vars": "^3.0.0"
5354
},
5455
"devDependencies": {
5556
"babel": "^6.23.0",
5657
"babel-cli": "^6.23.0",
5758
"babel-core": "^6.23.1",
59+
"babel-eslint": "^7.2.3",
5860
"babel-jest": "^19.0.0",
5961
"babel-plugin-transform-flow-strip-types": "^6.22.0",
6062
"babel-plugin-transform-runtime": "^6.23.0",

src/async/parser.js

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,9 @@
11
/**
22
* @see https://gist.github.com/muratgozel/e3ca2c08f74c9cb6eb7314e3088edb77#gistcomment-1802108
33
*/
4-
import postcss from 'postcss'
5-
import safeParse from 'postcss-safe-parser'
64
import postcssJs from 'postcss-js'
7-
import postcssrc from 'postcss-load-config'
85

9-
let config
10-
let processor
11-
12-
/**
13-
* 1. Initiate config, options and processor variables in module scope,
14-
* if they are not initiated yet
15-
*
16-
* 2. Process parsing with initiated options
17-
*
18-
* @param {String} rawStyles
19-
* @param {Object} processOptions
20-
* @returns {Object} JSS Object
21-
*/
22-
const processParsing = async (rawStyles, processOptions = {}) => {
23-
const { config: customConfig } = processOptions
24-
if (!config && customConfig) {
25-
config = customConfig
26-
} else if (!config) {
27-
config = await postcssrc()
28-
}
29-
30-
const { plugins = [], options = {} } = config
31-
const finalOptions = { parser: safeParse, ...options }
32-
33-
if (!processor) {
34-
processor = postcss(plugins)
35-
}
36-
37-
return processor.process(rawStyles, finalOptions)
38-
}
6+
import processParsing from '../common/process-parsing'
397

408
/**
419
* Parse specified Tagged Template Strings with CSS and expressions
@@ -48,6 +16,3 @@ export default async (rawStyles, processOptions) => {
4816
const processed = await processParsing(rawStyles, processOptions)
4917
return postcssJs.objectify(processed.root)
5018
}
51-
52-
53-

src/common/default-config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const syntax = require('postcss-scss')
2+
3+
export default { options: { syntax } }

src/common/process-parsing.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import postcss from 'postcss'
2+
import postcssrc from 'postcss-load-config'
3+
import safeParse from 'postcss-safe-parser'
4+
5+
import defaultConfig from './default-config'
6+
7+
let config
8+
let processor
9+
10+
/**
11+
* 1. Initiate config, options and processor variables in module scope,
12+
* if they are not initiated yet
13+
*
14+
* 2. Process parsing with initiated options
15+
*
16+
* @param {String} rawStyles
17+
* @param {Object} processOptions
18+
* @returns {Object} JSS Object
19+
*/
20+
const processParsing = async (rawStyles, processOptions = {}) => {
21+
const { config: customConfig } = processOptions
22+
if (!config && customConfig) {
23+
config = customConfig
24+
} else if (!config) {
25+
try {
26+
config = await postcssrc()
27+
} catch (e) {
28+
config = defaultConfig
29+
}
30+
}
31+
32+
const { plugins = [], options = {} } = config
33+
const finalOptions = { parser: safeParse, ...options }
34+
35+
if (!processor) {
36+
processor = postcss(plugins)
37+
}
38+
39+
return processor.process(rawStyles, finalOptions)
40+
}
41+
42+
export default processParsing

src/sync/parser.js

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,10 @@
88
*
99
* @todo Find a way to do not use deasync
1010
*/
11-
import postcss from 'postcss'
12-
import safeParse from 'postcss-safe-parser'
1311
import postcssJs from 'postcss-js'
14-
import postcssrc from 'postcss-load-config'
1512
import deasync from 'deasync'
1613

17-
let config
18-
let processor
19-
20-
/**
21-
* 1. Initiate config, options and processor variables in module scope,
22-
* if they are not initiated yet
23-
*
24-
* 2. Process parsing with initiated options
25-
*
26-
* @param {String} rawStyles
27-
* @param {Object} processOptions
28-
* @returns {Object} JSS Object
29-
*/
30-
const processParsing = async (rawStyles, processOptions = {}) => {
31-
const { config: customConfig } = processOptions
32-
if (!config && customConfig) {
33-
config = customConfig
34-
} else if (!config) {
35-
config = await postcssrc()
36-
}
37-
38-
const { plugins = [], options = {} } = config
39-
const finalOptions = { parser: safeParse, ...options }
40-
41-
if (!processor) {
42-
processor = postcss(plugins)
43-
}
44-
45-
return processor.process(rawStyles, finalOptions)
46-
}
14+
import processParsing from '../common/process-parsing'
4715

4816
/**
4917
* Parse specified Tagged Template Strings with CSS and expressions
@@ -58,6 +26,3 @@ export default deasync((rawStyles, processOptions = {}, cb) => {
5826
.then(result => cb(null, result))
5927
.catch(error => cb(error))
6028
})
61-
62-
63-

tests/default-config/index.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { asyncParser } from '../../src'
2+
3+
const css = `
4+
button {
5+
color: #ffffff;
6+
width: 100px;
7+
height: 70px;
8+
9+
&.primary {
10+
color: red;
11+
}
12+
}
13+
`
14+
15+
const objectCss = {
16+
button: {
17+
color: '#ffffff',
18+
width: '100px',
19+
height: '70px',
20+
'&.primary': {
21+
color: 'red',
22+
},
23+
},
24+
}
25+
26+
beforeAll(() => {
27+
process.chdir(__dirname)
28+
})
29+
30+
it('should handle default config', async () => {
31+
const result = await asyncParser(css)
32+
33+
expect(result).toEqual(objectCss)
34+
})

tests/parser.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ it('async parse from plain css to object', async () => {
3232
const result = await asyncParser(css, options)
3333

3434
expect(result).toEqual(objectCss)
35-
})
35+
})
File renamed without changes.

tests/postcssrc/index.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { asyncParser } from '../../src'
2+
3+
const css = `
4+
button {
5+
color: #ffffff;
6+
width: 100px;
7+
height: 70px;
8+
9+
&.primary {
10+
color: red;
11+
}
12+
}
13+
`
14+
15+
const objectCss = {
16+
button: {
17+
color: '#ffffff',
18+
width: '100px',
19+
height: '70px',
20+
},
21+
'button.primary': {
22+
color: 'red',
23+
},
24+
}
25+
26+
beforeAll(() => {
27+
process.chdir(__dirname)
28+
})
29+
30+
it('should handle postcssrc config', async () => {
31+
const result = await asyncParser(css)
32+
33+
expect(result).toEqual(objectCss)
34+
})

0 commit comments

Comments
 (0)