Skip to content

Commit f40cc81

Browse files
authored
Merge pull request #86 from hyperz111/reduce-size
chore: reduce package size
2 parents b2247fc + 5cdfdf7 commit f40cc81

5 files changed

Lines changed: 30 additions & 44 deletions

File tree

async.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
let postcss = require('postcss')
22

3-
let parse = require('./parser')
3+
let parser = require('./parser')
44
let processResult = require('./process-result')
55

66
module.exports = function async(plugins) {
77
let processor = postcss(plugins)
88
return async input => {
9-
let result = await processor.process(input, {
10-
parser: parse,
11-
from: undefined
12-
})
9+
let result = await processor.process(input, { parser, from: undefined })
1310
return processResult(result)
1411
}
1512
}

objectifier.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ let UNITLESS = {
2424
}
2525

2626
function atRule(node) {
27-
if (typeof node.nodes === 'undefined') {
28-
return true
29-
} else {
30-
return process(node)
31-
}
27+
return node.nodes === undefined ? true : process(node)
3228
}
3329

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

4440
// Microsoft vendor-prefixes are uniquely cased
4541
if (property.startsWith('-ms-')) {
46-
property = property.substring(1)
42+
property = property.slice(1)
4743
index = property.indexOf('-')
4844
}
4945

5046
let cursor = 0
5147
let result = ''
5248

5349
do {
54-
result += property.substring(cursor, index) + property[index + 1].toUpperCase()
50+
result += property.slice(cursor, index) + property[index + 1].toUpperCase()
5551
cursor = index + 2
5652
index = property.indexOf('-', cursor)
5753
} while (index !== -1)
5854

59-
return result + property.substring(cursor)
55+
return result + property.slice(cursor)
6056
}
6157

6258
function process(node, options = {}) {
6359
let name
6460
let result = {}
65-
let { stringifyImportant } = options
6661

6762
node.each(child => {
6863
if (child.type === 'atrule') {
6964
name = '@' + child.name
7065
if (child.params) name += ' ' + child.params
71-
if (typeof result[name] === 'undefined') {
66+
if (result[name] === undefined) {
7267
result[name] = atRule(child)
7368
} else if (Array.isArray(result[name])) {
7469
result[name].push(atRule(child))
@@ -81,7 +76,7 @@ function process(node, options = {}) {
8176
for (let i in body) {
8277
let object = result[child.selector]
8378
if (
84-
stringifyImportant &&
79+
options.stringifyImportant &&
8580
typeof object[i] === 'string' &&
8681
object[i].endsWith('!important')
8782
) {
@@ -96,19 +91,17 @@ function process(node, options = {}) {
9691
result[child.selector] = body
9792
}
9893
} else if (child.type === 'decl') {
99-
if (child.prop[0] === '-' && child.prop[1] === '-') {
94+
if (child.prop.startsWith('--')) {
10095
name = child.prop
10196
} else if (child.parent && child.parent.selector === ':export') {
10297
name = child.prop
10398
} else {
10499
name = camelcase(child.prop)
105100
}
106101
let value = child.value
107-
if (!isNaN(child.value) && UNITLESS[name]) {
108-
value = parseFloat(child.value)
109-
}
102+
if (!isNaN(child.value) && UNITLESS[name]) value = parseFloat(child.value)
110103
if (child.important) value += ' !important'
111-
if (typeof result[name] === 'undefined') {
104+
if (result[name] === undefined) {
112105
result[name] = value
113106
} else if (Array.isArray(result[name])) {
114107
result[name].push(value)

parser.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,30 @@ let UNITLESS = {
2727
'stroke-width': true
2828
}
2929

30-
let { fromCharCode } = String;
30+
let { fromCharCode } = String
3131

3232
function dashify(str) {
33-
let result = '';
34-
let i = 0;
35-
let len = str.length;
36-
let code;
33+
if (str === 'cssFloat') return 'float'
3734

38-
if (str[0] === 'm' && str[1] === 's') result += fromCharCode(45); // '-'
35+
let result = ''
36+
let i = 0
37+
let len = str.length
38+
let code
39+
40+
if (str.startsWith('ms')) result += fromCharCode(45) // '-'
3941

4042
for (; i < len; i++) {
41-
code = str[i].charCodeAt(0);
43+
code = str[i].charCodeAt(0)
4244

4345
if (code > 64 && code < 91) {
44-
result += fromCharCode(45) + fromCharCode(code + 32);
45-
continue;
46+
result += fromCharCode(45) + fromCharCode(code + 32)
47+
continue
4648
}
4749

48-
result += fromCharCode(code);
50+
result += fromCharCode(code)
4951
}
5052

51-
return result;
53+
return result
5254
}
5355

5456
function decl(parent, name, value) {
@@ -59,15 +61,10 @@ function decl(parent, name, value) {
5961
}
6062

6163
if (typeof value === 'number') {
62-
if (value === 0 || UNITLESS[name]) {
63-
value = value.toString()
64-
} else {
65-
value += 'px'
66-
}
64+
value = value.toString()
65+
if (value !== '0' && !UNITLESS[name]) value += 'px'
6766
}
6867

69-
if (name === 'css-float') name = 'float'
70-
7168
if (IMPORTANT.test(value)) {
7269
value = value.replace(IMPORTANT, '')
7370
parent.push(postcss.decl({ prop: name, value, important: true }))
@@ -89,7 +86,7 @@ function parse(obj, parent) {
8986
let name, node, value
9087
for (name in obj) {
9188
value = obj[name]
92-
if (value === null || typeof value === 'undefined') {
89+
if (value == null) {
9390
continue
9491
} else if (name[0] === '@') {
9592
let parts = name.match(/@(\S+)(\s+([\W\w]*)\s*)?/)

process-result.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ let objectify = require('./objectifier')
33
module.exports = function processResult(result) {
44
if (console && console.warn) {
55
result.warnings().forEach(warn => {
6-
let source = warn.plugin || 'PostCSS'
7-
console.warn(source + ': ' + warn.text)
6+
console.warn((warn.plugin || 'PostCSS') + ': ' + warn.text)
87
})
98
}
109
return objectify(result.root)

sync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
let postcss = require('postcss')
22

3-
let parse = require('./parser')
3+
let parser = require('./parser')
44
let processResult = require('./process-result')
55

66
module.exports = function (plugins) {
77
let processor = postcss(plugins)
88
return input => {
9-
let result = processor.process(input, { parser: parse, from: undefined })
9+
let result = processor.process(input, { parser, from: undefined })
1010
return processResult(result)
1111
}
1212
}

0 commit comments

Comments
 (0)