From c06f7ee0a40255214c477e13d9b956a7e07c2900 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 14 Oct 2023 15:05:55 +0800 Subject: postcss,(plugins): nanocss, postcss-preset-env --- .../css-declaration-sorter/src/bubble-sort.mjs | 20 + node_modules/css-declaration-sorter/src/main.d.ts | 27 ++ node_modules/css-declaration-sorter/src/main.mjs | 146 +++++++ .../css-declaration-sorter/src/shorthand-data.mjs | 433 +++++++++++++++++++++ 4 files changed, 626 insertions(+) create mode 100644 node_modules/css-declaration-sorter/src/bubble-sort.mjs create mode 100644 node_modules/css-declaration-sorter/src/main.d.ts create mode 100644 node_modules/css-declaration-sorter/src/main.mjs create mode 100644 node_modules/css-declaration-sorter/src/shorthand-data.mjs (limited to 'node_modules/css-declaration-sorter/src') diff --git a/node_modules/css-declaration-sorter/src/bubble-sort.mjs b/node_modules/css-declaration-sorter/src/bubble-sort.mjs new file mode 100644 index 00000000..87803b46 --- /dev/null +++ b/node_modules/css-declaration-sorter/src/bubble-sort.mjs @@ -0,0 +1,20 @@ +export function bubbleSort (list, comparator) { + let upperIndex = list.length - 1; + + while (upperIndex > 0) { + let swapIndex = 0; + + for (let i = 0; i < upperIndex; i += 1) { + if (comparator(list[i], list[i + 1]) > 0) { + const temp = list[i + 1]; + list[i + 1] = list[i]; + list[i] = temp; + swapIndex = i; + } + } + + upperIndex = swapIndex; + } + + return list; +} diff --git a/node_modules/css-declaration-sorter/src/main.d.ts b/node_modules/css-declaration-sorter/src/main.d.ts new file mode 100644 index 00000000..257704b6 --- /dev/null +++ b/node_modules/css-declaration-sorter/src/main.d.ts @@ -0,0 +1,27 @@ +import type { PluginCreator } from 'postcss'; + +declare const cssDeclarationSorter: PluginCreator<{ + /** + Provide the name of one of the built-in sort orders or a comparison function that is passed to `Array.sort`. + + @default 'alphabetical' + */ + order?: SortOrder | SortFunction | undefined; + + /** + To prevent breaking legacy CSS where shorthand declarations override longhand declarations. For example `animation-name: some; animation: greeting;` will be kept in this order. + + @default false + */ + keepOverrides?: boolean; +}>; + +export = cssDeclarationSorter; + +type SortOrder = 'alphabetical' | 'concentric-css' | 'smacss'; + +/** + * This function receives two declaration property names and is expected + * to return -1, 0 or 1 depending on the wanted order. + */ +type SortFunction = (propertyNameA: string, propertyNameB: string) => -1 | 0 | 1; diff --git a/node_modules/css-declaration-sorter/src/main.mjs b/node_modules/css-declaration-sorter/src/main.mjs new file mode 100644 index 00000000..b17cde16 --- /dev/null +++ b/node_modules/css-declaration-sorter/src/main.mjs @@ -0,0 +1,146 @@ +import { shorthandData } from './shorthand-data.mjs'; +import { bubbleSort } from './bubble-sort.mjs'; + +const builtInOrders = [ + 'alphabetical', + 'concentric-css', + 'smacss', +]; + +export const cssDeclarationSorter = ({ order = 'alphabetical', keepOverrides = false } = {}) => ({ + postcssPlugin: 'css-declaration-sorter', + OnceExit (css) { + let withKeepOverrides = comparator => comparator; + if (keepOverrides) { + withKeepOverrides = withOverridesComparator(shorthandData); + } + + if (typeof order === 'function') { + return processCss({ css, comparator: withKeepOverrides(order) }); + } + + if (!builtInOrders.includes(order)) + return Promise.reject( + Error([ + `Invalid built-in order '${order}' provided.`, + `Available built-in orders are: ${builtInOrders}`, + ].join('\n')) + ); + + return import(`../orders/${order}.mjs`) + .then(({ properties }) => processCss({ + css, + comparator: withKeepOverrides(orderComparator(properties)), + })); + }, +}); + +cssDeclarationSorter.postcss = true; + +// Kept for backward compatibility +export default cssDeclarationSorter; + +function processCss ({ css, comparator }) { + const comments = []; + const rulesCache = []; + + css.walk(node => { + const nodes = node.nodes; + const type = node.type; + + if (type === 'comment') { + // Don't do anything to root comments or the last newline comment + const isNewlineNode = node.raws.before && node.raws.before.includes('\n'); + const lastNewlineNode = isNewlineNode && !node.next(); + const onlyNode = !node.prev() && !node.next() || !node.parent; + + if (lastNewlineNode || onlyNode || node.parent.type === 'root') { + return; + } + + if (isNewlineNode) { + const pairedNode = node.next() || node.prev(); + if (pairedNode) { + comments.unshift({ + 'comment': node, + 'pairedNode': pairedNode, + 'insertPosition': node.next() ? 'Before' : 'After', + }); + node.remove(); + } + } else { + const pairedNode = node.prev() || node.next(); + if (pairedNode) { + comments.push({ + 'comment': node, + 'pairedNode': pairedNode, + 'insertPosition': 'After', + }); + node.remove(); + } + } + return; + } + + // Add rule-like nodes to a cache so that we can remove all + // comment nodes before we start sorting. + const isRule = type === 'rule' || type === 'atrule'; + if (isRule && nodes && nodes.length > 1) { + rulesCache.push(nodes); + } + }); + + // Perform a sort once all comment nodes are removed + rulesCache.forEach(nodes => { + sortCssDeclarations({ nodes, comparator }); + }); + + // Add comments back to the nodes they are paired with + comments.forEach(node => { + const pairedNode = node.pairedNode; + node.comment.remove(); + pairedNode.parent && pairedNode.parent['insert' + node.insertPosition](pairedNode, node.comment); + }); +} + +function sortCssDeclarations ({ nodes, comparator }) { + bubbleSort(nodes, (a, b) => { + if (a.type === 'decl' && b.type === 'decl') { + return comparator(a.prop, b.prop); + } else { + return compareDifferentType(a, b); + } + }); +} + +function withOverridesComparator (shorthandData) { + return function (comparator) { + return function (a, b) { + a = removeVendorPrefix(a); + b = removeVendorPrefix(b); + + if (shorthandData[a] && shorthandData[a].includes(b)) return 0; + if (shorthandData[b] && shorthandData[b].includes(a)) return 0; + + return comparator(a, b); + }; + }; +} + +function orderComparator (order) { + return function (a, b) { + return order.indexOf(a) - order.indexOf(b); + }; +} + +function compareDifferentType (a, b) { + if (b.type === 'atrule' || a.type === 'atrule') { + return 0; + } + + return a.type === 'decl' ? -1 : b.type === 'decl' ? 1 : 0; +} + +function removeVendorPrefix (property) { + return property.replace(/^-\w+-/, ''); +} diff --git a/node_modules/css-declaration-sorter/src/shorthand-data.mjs b/node_modules/css-declaration-sorter/src/shorthand-data.mjs new file mode 100644 index 00000000..d79b7376 --- /dev/null +++ b/node_modules/css-declaration-sorter/src/shorthand-data.mjs @@ -0,0 +1,433 @@ +export const shorthandData = { + 'animation': [ + 'animation-name', + 'animation-duration', + 'animation-timing-function', + 'animation-delay', + 'animation-iteration-count', + 'animation-direction', + 'animation-fill-mode', + 'animation-play-state', + ], + 'background': [ + 'background-image', + 'background-size', + 'background-position', + 'background-repeat', + 'background-origin', + 'background-clip', + 'background-attachment', + 'background-color', + ], + 'columns': [ + 'column-width', + 'column-count', + ], + 'column-rule': [ + 'column-rule-width', + 'column-rule-style', + 'column-rule-color', + ], + 'flex': [ + 'flex-grow', + 'flex-shrink', + 'flex-basis', + ], + 'flex-flow': [ + 'flex-direction', + 'flex-wrap', + ], + 'font': [ + 'font-style', + 'font-variant', + 'font-weight', + 'font-stretch', + 'font-size', + 'font-family', + 'line-height', + ], + 'grid': [ + 'grid-template-rows', + 'grid-template-columns', + 'grid-template-areas', + 'grid-auto-rows', + 'grid-auto-columns', + 'grid-auto-flow', + 'column-gap', + 'row-gap', + ], + 'grid-area': [ + 'grid-row-start', + 'grid-column-start', + 'grid-row-end', + 'grid-column-end', + ], + 'grid-column': [ + 'grid-column-start', + 'grid-column-end', + ], + 'grid-row': [ + 'grid-row-start', + 'grid-row-end', + ], + 'grid-template': [ + 'grid-template-columns', + 'grid-template-rows', + 'grid-template-areas', + ], + 'list-style': [ + 'list-style-type', + 'list-style-position', + 'list-style-image', + ], + 'padding': [ + 'padding-block', + 'padding-block-start', + 'padding-block-end', + 'padding-inline', + 'padding-inline-start', + 'padding-inline-end', + 'padding-top', + 'padding-right', + 'padding-bottom', + 'padding-left', + ], + 'padding-block': [ + 'padding-block-start', + 'padding-block-end', + 'padding-top', + 'padding-right', + 'padding-bottom', + 'padding-left', + ], + 'padding-block-start': [ + 'padding-top', + 'padding-right', + 'padding-left', + ], + 'padding-block-end': [ + 'padding-right', + 'padding-bottom', + 'padding-left', + ], + 'padding-inline': [ + 'padding-inline-start', + 'padding-inline-end', + 'padding-top', + 'padding-right', + 'padding-bottom', + 'padding-left', + ], + 'padding-inline-start': [ + 'padding-top', + 'padding-right', + 'padding-left', + ], + 'padding-inline-end': [ + 'padding-right', + 'padding-bottom', + 'padding-left', + ], + 'margin': [ + 'margin-block', + 'margin-block-start', + 'margin-block-end', + 'margin-inline', + 'margin-inline-start', + 'margin-inline-end', + 'margin-top', + 'margin-right', + 'margin-bottom', + 'margin-left', + ], + 'margin-block': [ + 'margin-block-start', + 'margin-block-end', + 'margin-top', + 'margin-right', + 'margin-bottom', + 'margin-left', + ], + 'margin-inline': [ + 'margin-inline-start', + 'margin-inline-end', + 'margin-top', + 'margin-right', + 'margin-bottom', + 'margin-left', + ], + 'margin-inline-start': [ + 'margin-top', + 'margin-right', + 'margin-bottom', + 'margin-left', + ], + 'margin-inline-end': [ + 'margin-top', + 'margin-right', + 'margin-bottom', + 'margin-left', + ], + 'border': [ + 'border-top', + 'border-right', + 'border-bottom', + 'border-left', + 'border-width', + 'border-style', + 'border-color', + 'border-top-width', + 'border-right-width', + 'border-bottom-width', + 'border-left-width', + 'border-inline-start-width', + 'border-inline-end-width', + 'border-block-start-width', + 'border-block-end-width', + 'border-top-style', + 'border-right-style', + 'border-bottom-style', + 'border-left-style', + 'border-inline-start-style', + 'border-inline-end-style', + 'border-block-start-style', + 'border-block-end-style', + 'border-top-color', + 'border-right-color', + 'border-bottom-color', + 'border-left-color', + 'border-inline-start-color', + 'border-inline-end-color', + 'border-block-start-color', + 'border-block-end-color', + 'border-block', + 'border-block-start', + 'border-block-end', + 'border-block-width', + 'border-block-style', + 'border-block-color', + 'border-inline', + 'border-inline-start', + 'border-inline-end', + 'border-inline-width', + 'border-inline-style', + 'border-inline-color', + ], + 'border-top': [ + 'border-width', + 'border-style', + 'border-color', + 'border-top-width', + 'border-top-style', + 'border-top-color', + ], + 'border-right': [ + 'border-width', + 'border-style', + 'border-color', + 'border-right-width', + 'border-right-style', + 'border-right-color', + ], + 'border-bottom': [ + 'border-width', + 'border-style', + 'border-color', + 'border-bottom-width', + 'border-bottom-style', + 'border-bottom-color', + ], + 'border-left': [ + 'border-width', + 'border-style', + 'border-color', + 'border-left-width', + 'border-left-style', + 'border-left-color', + ], + 'border-color': [ + 'border-top-color', + 'border-bottom-color', + 'border-left-color', + 'border-right-color', + 'border-inline-start-color', + 'border-inline-end-color', + 'border-block-start-color', + 'border-block-end-color', + ], + 'border-width': [ + 'border-top-width', + 'border-bottom-width', + 'border-left-width', + 'border-right-width', + 'border-inline-start-width', + 'border-inline-end-width', + 'border-block-start-width', + 'border-block-end-width', + ], + 'border-style': [ + 'border-top-style', + 'border-bottom-style', + 'border-left-style', + 'border-right-style', + 'border-inline-start-style', + 'border-inline-end-style', + 'border-block-start-style', + 'border-block-end-style', + ], + 'border-radius': [ + 'border-top-right-radius', + 'border-top-left-radius', + 'border-bottom-right-radius', + 'border-bottom-left-radius', + ], + 'border-block': [ + 'border-block-start', + 'border-block-end', + 'border-block-width', + 'border-width', + 'border-block-style', + 'border-style', + 'border-block-color', + 'border-color', + ], + 'border-block-start': [ + 'border-block-start-width', + 'border-width', + 'border-block-start-style', + 'border-style', + 'border-block-start-color', + 'border-color', + ], + 'border-block-end': [ + 'border-block-end-width', + 'border-width', + 'border-block-end-style', + 'border-style', + 'border-block-end-color', + 'border-color', + ], + 'border-inline': [ + 'border-inline-start', + 'border-inline-end', + 'border-inline-width', + 'border-width', + 'border-inline-style', + 'border-style', + 'border-inline-color', + 'border-color', + ], + 'border-inline-start': [ + 'border-inline-start-width', + 'border-width', + 'border-inline-start-style', + 'border-style', + 'border-inline-start-color', + 'border-color', + ], + 'border-inline-end': [ + 'border-inline-end-width', + 'border-width', + 'border-inline-end-style', + 'border-style', + 'border-inline-end-color', + 'border-color', + ], + 'border-image': [ + 'border-image-source', + 'border-image-slice', + 'border-image-width', + 'border-image-outset', + 'border-image-repeat', + ], + 'mask': [ + 'mask-image', + 'mask-mode', + 'mask-position', + 'mask-size', + 'mask-repeat', + 'mask-origin', + 'mask-clip', + 'mask-composite', + ], + 'inline-size': [ + 'width', + 'height', + ], + 'block-size': [ + 'width', + 'height', + ], + 'max-inline-size': [ + 'max-width', + 'max-height', + ], + 'max-block-size': [ + 'max-width', + 'max-height', + ], + 'inset': [ + 'inset-block', + 'inset-block-start', + 'inset-block-end', + 'inset-inline', + 'inset-inline-start', + 'inset-inline-end', + 'top', + 'right', + 'bottom', + 'left', + ], + 'inset-block': [ + 'inset-block-start', + 'inset-block-end', + 'top', + 'right', + 'bottom', + 'left', + ], + 'inset-inline': [ + 'inset-inline-start', + 'inset-inline-end', + 'top', + 'right', + 'bottom', + 'left', + ], + 'outline': [ + 'outline-color', + 'outline-style', + 'outline-width', + ], + 'overflow': [ + 'overflow-x', + 'overflow-y', + ], + 'place-content': [ + 'align-content', + 'justify-content', + ], + 'place-items': [ + 'align-items', + 'justify-items', + ], + 'place-self': [ + 'align-self', + 'justify-self', + ], + 'text-decoration': [ + 'text-decoration-color', + 'text-decoration-style', + 'text-decoration-line', + ], + 'transition': [ + 'transition-delay', + 'transition-duration', + 'transition-property', + 'transition-timing-function', + ], + 'text-emphasis': [ + 'text-emphasis-style', + 'text-emphasis-color', + ], +}; -- cgit v1.2.3