Skip to content
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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"env": {
"browser": false,
"node": true,
"mocha": true
"jest": true
},
"ecmaFeatures": {
"jsx": false,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
"postcss-js": "^0.3.0",
"postcss-load-config": "^1.2.0",
"postcss-safe-parser": "^2.0.0",
"postcss-scss": "^0.4.1",
"postcss-simple-vars": "^3.0.0"
},
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-eslint": "^7.2.3",
"babel-jest": "^19.0.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-runtime": "^6.23.0",
Expand Down
37 changes: 1 addition & 36 deletions src/async/parser.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
/**
* @see https://gist.github.com/muratgozel/e3ca2c08f74c9cb6eb7314e3088edb77#gistcomment-1802108
*/
import postcss from 'postcss'
import safeParse from 'postcss-safe-parser'
import postcssJs from 'postcss-js'
import postcssrc from 'postcss-load-config'

let config
let processor

/**
* 1. Initiate config, options and processor variables in module scope,
* if they are not initiated yet
*
* 2. Process parsing with initiated options
*
* @param {String} rawStyles
* @param {Object} processOptions
* @returns {Object} JSS Object
*/
const processParsing = async (rawStyles, processOptions = {}) => {
const { config: customConfig } = processOptions
if (!config && customConfig) {
config = customConfig
} else if (!config) {
config = await postcssrc()
}

const { plugins = [], options = {} } = config
const finalOptions = { parser: safeParse, ...options }

if (!processor) {
processor = postcss(plugins)
}

return processor.process(rawStyles, finalOptions)
}
import processParsing from '../common/process-parsing'

/**
* Parse specified Tagged Template Strings with CSS and expressions
Expand All @@ -48,6 +16,3 @@ export default async (rawStyles, processOptions) => {
const processed = await processParsing(rawStyles, processOptions)
return postcssJs.objectify(processed.root)
}



3 changes: 3 additions & 0 deletions src/common/default-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const syntax = require('postcss-scss')

export default { options: { syntax } }
42 changes: 42 additions & 0 deletions src/common/process-parsing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import postcss from 'postcss'
import postcssrc from 'postcss-load-config'
import safeParse from 'postcss-safe-parser'

import defaultConfig from './default-config'

let config
let processor

/**
* 1. Initiate config, options and processor variables in module scope,
* if they are not initiated yet
*
* 2. Process parsing with initiated options
*
* @param {String} rawStyles
* @param {Object} processOptions
* @returns {Object} JSS Object
*/
const processParsing = async (rawStyles, processOptions = {}) => {
const { config: customConfig } = processOptions
if (!config && customConfig) {
config = customConfig
} else if (!config) {
try {
config = await postcssrc()
} catch (e) {
config = defaultConfig
}
}

const { plugins = [], options = {} } = config
const finalOptions = { parser: safeParse, ...options }

if (!processor) {
processor = postcss(plugins)
}

return processor.process(rawStyles, finalOptions)
}

export default processParsing
37 changes: 1 addition & 36 deletions src/sync/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,10 @@
*
* @todo Find a way to do not use deasync
*/
import postcss from 'postcss'
import safeParse from 'postcss-safe-parser'
import postcssJs from 'postcss-js'
import postcssrc from 'postcss-load-config'
import deasync from 'deasync'

let config
let processor

/**
* 1. Initiate config, options and processor variables in module scope,
* if they are not initiated yet
*
* 2. Process parsing with initiated options
*
* @param {String} rawStyles
* @param {Object} processOptions
* @returns {Object} JSS Object
*/
const processParsing = async (rawStyles, processOptions = {}) => {
const { config: customConfig } = processOptions
if (!config && customConfig) {
config = customConfig
} else if (!config) {
config = await postcssrc()
}

const { plugins = [], options = {} } = config
const finalOptions = { parser: safeParse, ...options }

if (!processor) {
processor = postcss(plugins)
}

return processor.process(rawStyles, finalOptions)
}
import processParsing from '../common/process-parsing'

/**
* Parse specified Tagged Template Strings with CSS and expressions
Expand All @@ -58,6 +26,3 @@ export default deasync((rawStyles, processOptions = {}, cb) => {
.then(result => cb(null, result))
.catch(error => cb(error))
})



34 changes: 34 additions & 0 deletions tests/default-config/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { asyncParser } from '../../src'

const rawCSS = `
button {
color: #ffffff;
width: 100px;
height: 70px;

&.primary {
color: red;
}
}
`

const expectedJSS = {
button: {
color: '#ffffff',
width: '100px',
height: '70px',
'&.primary': {
color: 'red',
},
},
}

beforeAll(() => {
process.chdir(__dirname)
})

it('should handle default config', async () => {
const result = await asyncParser(rawCSS)

expect(result).toEqual(expectedJSS)
})
14 changes: 7 additions & 7 deletions tests/parser.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { syncParser, asyncParser } from '../src'

const css = `
const rawCSS = `
button {
color: #ffffff;
width: 100px;
height: 70px;
}
`

const objectCss = {
const expectedJSS = {
button: {
color: '#ffffff',
width: '100px',
Expand All @@ -23,13 +23,13 @@ const options = {
}

it('sync parse from plain css to object', () => {
const result = syncParser(css, options)
const result = syncParser(rawCSS, options)

expect(result).toEqual(objectCss)
expect(result).toEqual(expectedJSS)
})

it('async parse from plain css to object', async () => {
const result = await asyncParser(css, options)
const result = await asyncParser(rawCSS, options)

expect(result).toEqual(objectCss)
})
expect(result).toEqual(expectedJSS)
})
File renamed without changes.
34 changes: 34 additions & 0 deletions tests/postcssrc/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { asyncParser } from '../../src'

const rawCSS = `
button {
color: #ffffff;
width: 100px;
height: 70px;

&.primary {
color: red;
}
}
`

const expectedJSS = {
button: {
color: '#ffffff',
width: '100px',
height: '70px',
},
'button.primary': {
color: 'red',
},
}

beforeAll(() => {
process.chdir(__dirname)
})

it('should handle postcssrc config', async () => {
const result = await asyncParser(rawCSS)

expect(result).toEqual(expectedJSS)
})