From 02abb619397e20a7feca921314c9bec42959a201 Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Wed, 31 Dec 2025 21:12:52 +0700 Subject: [PATCH 1/6] reduce parser.js --- parser.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/parser.js b/parser.js index 3cac15d..2a794f2 100644 --- a/parser.js +++ b/parser.js @@ -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) { @@ -66,8 +68,6 @@ function decl(parent, name, value) { } } - if (name === 'css-float') name = 'float' - if (IMPORTANT.test(value)) { value = value.replace(IMPORTANT, '') parent.push(postcss.decl({ prop: name, value, important: true })) @@ -89,7 +89,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*)?/) From 7a650cde74db2c796a5b10f94e90b3cd91a333e4 Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Wed, 31 Dec 2025 21:20:48 +0700 Subject: [PATCH 2/6] reduce objectifier.js --- objectifier.js | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/objectifier.js b/objectifier.js index bd47fd2..07e16fd 100644 --- a/objectifier.js +++ b/objectifier.js @@ -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 @@ -62,7 +58,6 @@ function camelcase(property) { function process(node, options = {}) { let name let result = {} - let { stringifyImportant } = options node.each(child => { if (child.type === 'atrule') { @@ -81,13 +76,11 @@ 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') ) { - if (typeof body[i] === 'string' && body[i].endsWith('!important')) { - object[i] = body[i] - } + if (typeof body[i] === 'string' && body[i].endsWith('!important')) object[i] = body[i] } else { object[i] = body[i] } @@ -96,7 +89,7 @@ function process(node, options = {}) { result[child.selector] = body } } else if (child.type === 'decl') { - if (child.prop[0] === '-' && child.prop[1] === '-') { + if (child.startsWith('--')) { name = child.prop } else if (child.parent && child.parent.selector === ':export') { name = child.prop @@ -104,9 +97,7 @@ function process(node, options = {}) { 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') { result[name] = value From 77b8134366c7526be8402ef364b47e02e36f349c Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Wed, 31 Dec 2025 21:31:43 +0700 Subject: [PATCH 3/6] reduce process-result.js & objectifier.js --- objectifier.js | 10 ++++++---- process-result.js | 3 +-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/objectifier.js b/objectifier.js index 07e16fd..8f0ff0a 100644 --- a/objectifier.js +++ b/objectifier.js @@ -39,7 +39,7 @@ function camelcase(property) { // Microsoft vendor-prefixes are uniquely cased if (property.startsWith('-ms-')) { - property = property.substring(1) + property = property.slice(1) index = property.indexOf('-') } @@ -47,12 +47,12 @@ function camelcase(property) { 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 = {}) { @@ -80,7 +80,9 @@ function process(node, options = {}) { typeof object[i] === 'string' && object[i].endsWith('!important') ) { - if (typeof body[i] === 'string' && body[i].endsWith('!important')) object[i] = body[i] + if (typeof body[i] === 'string' && body[i].endsWith('!important')) { + object[i] = body[i] + } } else { object[i] = body[i] } diff --git a/process-result.js b/process-result.js index 215a95c..4128876 100644 --- a/process-result.js +++ b/process-result.js @@ -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) From 8c89d4ae22b570009ababddedd921b2ba66fa93d Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Wed, 31 Dec 2025 21:58:48 +0700 Subject: [PATCH 4/6] reduce parser.js again & fix objectifier.js --- objectifier.js | 2 +- parser.js | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/objectifier.js b/objectifier.js index 8f0ff0a..b77e19f 100644 --- a/objectifier.js +++ b/objectifier.js @@ -91,7 +91,7 @@ function process(node, options = {}) { result[child.selector] = body } } else if (child.type === 'decl') { - if (child.startsWith('--')) { + if (child.prop.startsWith('--')) { name = child.prop } else if (child.parent && child.parent.selector === ':export') { name = child.prop diff --git a/parser.js b/parser.js index 2a794f2..144387b 100644 --- a/parser.js +++ b/parser.js @@ -61,11 +61,8 @@ 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 (IMPORTANT.test(value)) { From aa6183130609f1d5fe496068c988bef5a8ce47e7 Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Wed, 31 Dec 2025 22:33:35 +0700 Subject: [PATCH 5/6] reduce objectifier.js again --- objectifier.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/objectifier.js b/objectifier.js index b77e19f..6cb9d88 100644 --- a/objectifier.js +++ b/objectifier.js @@ -63,7 +63,7 @@ function process(node, options = {}) { 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)) @@ -101,7 +101,7 @@ function process(node, options = {}) { let value = 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) From 5cdfdf7c8b40ce73c852460817cec2051cd177cf Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Thu, 1 Jan 2026 14:08:04 +0700 Subject: [PATCH 6/6] use shorthand --- async.js | 7 ++----- parser.js | 2 +- sync.js | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/async.js b/async.js index 1ba6b3b..99dbae1 100644 --- a/async.js +++ b/async.js @@ -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) } } diff --git a/parser.js b/parser.js index 144387b..e6df6e8 100644 --- a/parser.js +++ b/parser.js @@ -62,7 +62,7 @@ function decl(parent, name, value) { if (typeof value === 'number') { value = value.toString() - if (value !== "0" && !UNITLESS[name]) value += 'px' + if (value !== '0' && !UNITLESS[name]) value += 'px' } if (IMPORTANT.test(value)) { diff --git a/sync.js b/sync.js index 668a9ef..90c6337 100644 --- a/sync.js +++ b/sync.js @@ -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) } }