postcss-js
Advanced tools
| import { async as _async } from './index.js' | ||
| declare const async: typeof _async | ||
| export = async |
| export { | ||
| CssInJs, | ||
| ObjectifyOptions, | ||
| sync, | ||
| async, | ||
| objectify, | ||
| parse, | ||
| default | ||
| } from './index.js' |
+59
| // From DefinitelyTyped by Adam Thompson-Sharpe (https://github.com/MysteryBlokHed) with some modifications. | ||
| import { AcceptedPlugin, Root } from 'postcss' | ||
| declare namespace postcssJs { | ||
| /** CSS-in-JS object */ | ||
| type CssInJs = Record<string, any> | ||
| /** | ||
| * Create a PostCSS processor with a simple API | ||
| * @param plugins Synchronous plugins to use with PostCSS | ||
| * @returns A processor function that accepts (idk) and returns a CSS-in-JS object | ||
| */ | ||
| function sync( | ||
| plugins?: readonly AcceptedPlugin[] | ||
| ): (input: CssInJs) => CssInJs | ||
| function sync(...plugins: AcceptedPlugin[]): (input: CssInJs) => CssInJs | ||
| /** | ||
| * Create a PostCSS processor with a simple API, allowing asynchronous plugins | ||
| * @param plugins Plugins to use with PostCSS | ||
| * @returns A processor function that accepts (idk) and returns a CSS-in-JS object | ||
| */ | ||
| function async( | ||
| plugins?: readonly AcceptedPlugin[] | ||
| ): (input: CssInJs) => Promise<CssInJs> | ||
| function async( | ||
| ...plugins: AcceptedPlugin[] | ||
| ): (input: CssInJs) => Promise<CssInJs> | ||
| /** | ||
| * Parse a CSS-in-JS object into a PostCSS `Root` | ||
| * @param obj The CSS-in-JS to parse | ||
| * @returns A PostCSS `Root` | ||
| */ | ||
| function parse(obj: CssInJs): Root | ||
| interface ObjectifyOptions { | ||
| stringifyImportant?: boolean | undefined | ||
| } | ||
| /** | ||
| * Convert a PostCSS `Root` into a CSS-in-JS object | ||
| * @param root The root to convert | ||
| * @returns CSS-in-JS object | ||
| */ | ||
| function objectify(root: Root, options?: ObjectifyOptions): CssInJs | ||
| export { | ||
| CssInJs, | ||
| ObjectifyOptions, | ||
| sync, | ||
| async, | ||
| objectify, | ||
| parse, | ||
| postcssJs as default | ||
| } | ||
| } | ||
| export = postcssJs |
| import { ObjectifyOptions, objectifiy } from './index.js' | ||
| declare namespace objectifier { | ||
| export { ObjectifyOptions, objectifiy as default } | ||
| } | ||
| export = objectifier |
| import { parse } from './index.js' | ||
| declare const parser: typeof parse | ||
| export = parser |
| import { CssInJs } from './index.js' | ||
| import { LazyResult } from 'postcss' | ||
| import NoWorkResult from 'postcss/lib/no-work-result' | ||
| declare function processResult(result: LazyResult | NoWorkResult): CssInJs | ||
| export = processResult |
| import { sync as _sync } from './index.js' | ||
| declare const sync: typeof _sync | ||
| export = sync |
+2
-5
| let postcss = require('postcss') | ||
| let parse = require('./parser') | ||
| let parser = require('./parser') | ||
| let processResult = require('./process-result') | ||
@@ -9,8 +9,5 @@ | ||
| 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) | ||
| } | ||
| } |
+9
-16
@@ -27,7 +27,3 @@ let UNITLESS = { | ||
| function atRule(node) { | ||
| if (typeof node.nodes === 'undefined') { | ||
| return true | ||
| } else { | ||
| return process(node) | ||
| } | ||
| return node.nodes === undefined ? true : process(node) | ||
| } | ||
@@ -47,3 +43,3 @@ | ||
| if (property.startsWith('-ms-')) { | ||
| property = property.substring(1) | ||
| property = property.slice(1) | ||
| index = property.indexOf('-') | ||
@@ -56,3 +52,3 @@ } | ||
| do { | ||
| result += property.substring(cursor, index) + property[index + 1].toUpperCase() | ||
| result += property.slice(cursor, index) + property[index + 1].toUpperCase() | ||
| cursor = index + 2 | ||
@@ -62,3 +58,3 @@ index = property.indexOf('-', cursor) | ||
| return result + property.substring(cursor) | ||
| return result + property.slice(cursor) | ||
| } | ||
@@ -69,3 +65,2 @@ | ||
| let result = {} | ||
| let { stringifyImportant } = options | ||
@@ -76,3 +71,3 @@ node.each(child => { | ||
| if (child.params) name += ' ' + child.params | ||
| if (typeof result[name] === 'undefined') { | ||
| if (result[name] === undefined) { | ||
| result[name] = atRule(child) | ||
@@ -90,3 +85,3 @@ } else if (Array.isArray(result[name])) { | ||
| if ( | ||
| stringifyImportant && | ||
| options.stringifyImportant && | ||
| typeof object[i] === 'string' && | ||
@@ -106,3 +101,3 @@ object[i].endsWith('!important') | ||
| } else if (child.type === 'decl') { | ||
| if (child.prop[0] === '-' && child.prop[1] === '-') { | ||
| if (child.prop.startsWith('--')) { | ||
| name = child.prop | ||
@@ -115,7 +110,5 @@ } else if (child.parent && child.parent.selector === ':export') { | ||
| 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 | ||
@@ -122,0 +115,0 @@ } else if (Array.isArray(result[name])) { |
+1
-1
| { | ||
| "name": "postcss-js", | ||
| "version": "5.0.3", | ||
| "version": "5.1.0", | ||
| "description": "PostCSS for CSS-in-JS and styles in JS objects", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
+16
-19
@@ -30,24 +30,26 @@ let postcss = require('postcss') | ||
| 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 | ||
| } | ||
@@ -63,11 +65,6 @@ | ||
| 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)) { | ||
@@ -94,3 +91,3 @@ value = value.replace(IMPORTANT, '') | ||
| value = obj[name] | ||
| if (value === null || typeof value === 'undefined') { | ||
| if (value == null) { | ||
| continue | ||
@@ -97,0 +94,0 @@ } else if (name[0] === '@') { |
@@ -6,4 +6,3 @@ let objectify = require('./objectifier') | ||
| result.warnings().forEach(warn => { | ||
| let source = warn.plugin || 'PostCSS' | ||
| console.warn(source + ': ' + warn.text) | ||
| console.warn((warn.plugin || 'PostCSS') + ': ' + warn.text) | ||
| }) | ||
@@ -10,0 +9,0 @@ } |
+2
-2
| let postcss = require('postcss') | ||
| let parse = require('./parser') | ||
| let parser = require('./parser') | ||
| let processResult = require('./process-result') | ||
@@ -9,5 +9,5 @@ | ||
| return input => { | ||
| let result = processor.process(input, { parser: parse, from: undefined }) | ||
| let result = processor.process(input, { parser, from: undefined }) | ||
| return processResult(result) | ||
| } | ||
| } |
12029
21.79%17
70%321
21.13%