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
7 changes: 2 additions & 5 deletions async.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
let postcss = require('postcss')

let parse = require('./parser')
let parser = require('./parser')
let processResult = require('./process-result')

module.exports = function async(plugins) {
let processor = postcss(plugins)
return async input => {
let result = await processor.process(input, {
parser: parse,
from: undefined
})
let result = await processor.process(input, { parser, from: undefined })
return processResult(result)
}
}
25 changes: 9 additions & 16 deletions objectifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ let UNITLESS = {
}

function atRule(node) {
if (typeof node.nodes === 'undefined') {
return true
} else {
return process(node)
}
return node.nodes === undefined ? true : process(node)
}

// From https://github.com/hyperz111/fast-camelcase-css
Expand All @@ -43,32 +39,31 @@ function camelcase(property) {

// Microsoft vendor-prefixes are uniquely cased
if (property.startsWith('-ms-')) {
property = property.substring(1)
property = property.slice(1)
index = property.indexOf('-')
}

let cursor = 0
let result = ''

do {
result += property.substring(cursor, index) + property[index + 1].toUpperCase()
result += property.slice(cursor, index) + property[index + 1].toUpperCase()
cursor = index + 2
index = property.indexOf('-', cursor)
} while (index !== -1)

return result + property.substring(cursor)
return result + property.slice(cursor)
}

function process(node, options = {}) {
let name
let result = {}
let { stringifyImportant } = options

node.each(child => {
if (child.type === 'atrule') {
name = '@' + child.name
if (child.params) name += ' ' + child.params
if (typeof result[name] === 'undefined') {
if (result[name] === undefined) {
result[name] = atRule(child)
} else if (Array.isArray(result[name])) {
result[name].push(atRule(child))
Expand All @@ -81,7 +76,7 @@ function process(node, options = {}) {
for (let i in body) {
let object = result[child.selector]
if (
stringifyImportant &&
options.stringifyImportant &&
typeof object[i] === 'string' &&
object[i].endsWith('!important')
) {
Expand All @@ -96,19 +91,17 @@ function process(node, options = {}) {
result[child.selector] = body
}
} else if (child.type === 'decl') {
if (child.prop[0] === '-' && child.prop[1] === '-') {
if (child.prop.startsWith('--')) {
name = child.prop
} else if (child.parent && child.parent.selector === ':export') {
name = child.prop
} else {
name = camelcase(child.prop)
}
let value = child.value
if (!isNaN(child.value) && UNITLESS[name]) {
value = parseFloat(child.value)
}
if (!isNaN(child.value) && UNITLESS[name]) value = parseFloat(child.value)
if (child.important) value += ' !important'
if (typeof result[name] === 'undefined') {
if (result[name] === undefined) {
result[name] = value
} else if (Array.isArray(result[name])) {
result[name].push(value)
Expand Down
35 changes: 16 additions & 19 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,30 @@ let UNITLESS = {
'stroke-width': true
}

let { fromCharCode } = String;
let { fromCharCode } = String

function dashify(str) {
let result = '';
let i = 0;
let len = str.length;
let code;
if (str === 'cssFloat') return 'float'

if (str[0] === 'm' && str[1] === 's') result += fromCharCode(45); // '-'
let result = ''
let i = 0
let len = str.length
let code

if (str.startsWith('ms')) result += fromCharCode(45) // '-'

for (; i < len; i++) {
code = str[i].charCodeAt(0);
code = str[i].charCodeAt(0)

if (code > 64 && code < 91) {
result += fromCharCode(45) + fromCharCode(code + 32);
continue;
result += fromCharCode(45) + fromCharCode(code + 32)
continue
}

result += fromCharCode(code);
result += fromCharCode(code)
}

return result;
return result
}

function decl(parent, name, value) {
Expand All @@ -59,15 +61,10 @@ function decl(parent, name, value) {
}

if (typeof value === 'number') {
if (value === 0 || UNITLESS[name]) {
value = value.toString()
} else {
value += 'px'
}
value = value.toString()
if (value !== '0' && !UNITLESS[name]) value += 'px'
}

if (name === 'css-float') name = 'float'

if (IMPORTANT.test(value)) {
value = value.replace(IMPORTANT, '')
parent.push(postcss.decl({ prop: name, value, important: true }))
Expand All @@ -89,7 +86,7 @@ function parse(obj, parent) {
let name, node, value
for (name in obj) {
value = obj[name]
if (value === null || typeof value === 'undefined') {
if (value == null) {
continue
} else if (name[0] === '@') {
let parts = name.match(/@(\S+)(\s+([\W\w]*)\s*)?/)
Expand Down
3 changes: 1 addition & 2 deletions process-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ let objectify = require('./objectifier')
module.exports = function processResult(result) {
if (console && console.warn) {
result.warnings().forEach(warn => {
let source = warn.plugin || 'PostCSS'
console.warn(source + ': ' + warn.text)
console.warn((warn.plugin || 'PostCSS') + ': ' + warn.text)
})
}
return objectify(result.root)
Expand Down
4 changes: 2 additions & 2 deletions sync.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
let postcss = require('postcss')

let parse = require('./parser')
let parser = require('./parser')
let processResult = require('./process-result')

module.exports = function (plugins) {
let processor = postcss(plugins)
return input => {
let result = processor.process(input, { parser: parse, from: undefined })
let result = processor.process(input, { parser, from: undefined })
return processResult(result)
}
}