|
| 1 | +import type {FileInfo, API, Options} from 'jscodeshift'; |
| 2 | +import postcss, {Plugin} from 'postcss'; |
| 3 | +import valueParser from 'postcss-value-parser'; |
| 4 | +import {colors as tokenColors, createVar} from '@shopify/polaris-tokens'; |
| 5 | + |
| 6 | +import { |
| 7 | + NamespaceOptions, |
| 8 | + namespace, |
| 9 | + getFunctionArgs, |
| 10 | + stripQuotes, |
| 11 | + StopWalkingFunctionNodes, |
| 12 | +} from '../../utilities/sass'; |
| 13 | +import {isKeyOf} from '../../utilities/type-guards'; |
| 14 | + |
| 15 | +export default function replaceSassColors( |
| 16 | + file: FileInfo, |
| 17 | + _: API, |
| 18 | + options: Options, |
| 19 | +) { |
| 20 | + return postcss(plugin(options)).process(file.source, { |
| 21 | + syntax: require('postcss-scss'), |
| 22 | + }).css; |
| 23 | +} |
| 24 | + |
| 25 | +const processed = Symbol('processed'); |
| 26 | + |
| 27 | +interface PluginOptions extends Options, NamespaceOptions {} |
| 28 | + |
| 29 | +const plugin = (options: PluginOptions = {}): Plugin => { |
| 30 | + const namespacedColor = namespace('color', options); |
| 31 | + |
| 32 | + return { |
| 33 | + postcssPlugin: 'replace-sass-color', |
| 34 | + Declaration(decl) { |
| 35 | + // @ts-expect-error - Skip if processed so we don't process it again |
| 36 | + if (decl[processed]) return; |
| 37 | + |
| 38 | + if (!isKeyOf(propertyMaps, decl.prop)) return; |
| 39 | + const replacementMap = propertyMaps[decl.prop]; |
| 40 | + const parsed = valueParser(decl.value); |
| 41 | + |
| 42 | + parsed.walk((node) => { |
| 43 | + if (node.type !== 'function') return; |
| 44 | + |
| 45 | + if (node.value === 'rgba') { |
| 46 | + return StopWalkingFunctionNodes; |
| 47 | + } |
| 48 | + |
| 49 | + // 1. Remove color() fallbacks |
| 50 | + if (node.value === 'var') { |
| 51 | + const args = getFunctionArgs(node); |
| 52 | + const polarisCustomPropertyIndex = args.findIndex((arg) => |
| 53 | + polarisCustomPropertyRegEx.test(arg), |
| 54 | + ); |
| 55 | + const colorFnFallbackIndex = args.findIndex((arg) => |
| 56 | + arg.startsWith(namespacedColor), |
| 57 | + ); |
| 58 | + |
| 59 | + if (polarisCustomPropertyIndex < colorFnFallbackIndex) { |
| 60 | + node.nodes = [node.nodes[0]]; |
| 61 | + } |
| 62 | + |
| 63 | + return StopWalkingFunctionNodes; |
| 64 | + } |
| 65 | + |
| 66 | + // 2. Replace `color()` with variable |
| 67 | + if (node.value === namespacedColor) { |
| 68 | + const colorFnArgs = getFunctionArgs(node).map(stripQuotes); |
| 69 | + const hue = colorFnArgs[0] ?? ''; |
| 70 | + const value = colorFnArgs[1] ?? 'base'; |
| 71 | + const forBackground = colorFnArgs[2]; |
| 72 | + |
| 73 | + // Skip color() with for-background argument |
| 74 | + if (forBackground) return; |
| 75 | + |
| 76 | + // Skip if we don't have a color for the hue and value |
| 77 | + if ( |
| 78 | + !( |
| 79 | + isKeyOf(replacementMap, hue) && |
| 80 | + isKeyOf(replacementMap[hue], value) |
| 81 | + ) |
| 82 | + ) |
| 83 | + return; |
| 84 | + |
| 85 | + const colorCustomProperty: string = replacementMap[hue][value]; |
| 86 | + |
| 87 | + node.value = 'var'; |
| 88 | + node.nodes = [ |
| 89 | + { |
| 90 | + type: 'word', |
| 91 | + value: colorCustomProperty, |
| 92 | + sourceIndex: node.nodes[0]?.sourceIndex ?? 0, |
| 93 | + sourceEndIndex: colorCustomProperty.length, |
| 94 | + }, |
| 95 | + ]; |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + decl.value = parsed.toString(); |
| 100 | + |
| 101 | + // @ts-expect-error - Mark the declaration as processed |
| 102 | + decl[processed] = true; |
| 103 | + }, |
| 104 | + }; |
| 105 | +}; |
| 106 | + |
| 107 | +/* |
| 108 | + * See the legacy Sass API file for the original color palette |
| 109 | + * documentation/guides/legacy-polaris-v8-public-api.scss |
| 110 | + */ |
| 111 | + |
| 112 | +const colorMap = { |
| 113 | + blue: { |
| 114 | + dark: '--p-interactive-hovered', |
| 115 | + base: '--p-interactive', |
| 116 | + }, |
| 117 | + green: { |
| 118 | + dark: '--p-text-success', |
| 119 | + base: '--p-text-success', |
| 120 | + }, |
| 121 | + yellow: { |
| 122 | + dark: '--p-text-warning', |
| 123 | + base: '--p-text-warning', |
| 124 | + }, |
| 125 | + red: { |
| 126 | + dark: '--p-text-critical', |
| 127 | + base: '--p-text-critical', |
| 128 | + }, |
| 129 | + ink: { |
| 130 | + base: '--p-text', |
| 131 | + light: '--p-text-subdued', |
| 132 | + lighter: '--p-text-subdued', |
| 133 | + lightest: '--p-text-subdued', |
| 134 | + }, |
| 135 | + sky: { |
| 136 | + dark: '--p-text-subdued-on-dark', |
| 137 | + base: '--p-text-on-dark', |
| 138 | + light: '--p-text-on-dark', |
| 139 | + lighter: '--p-text-on-dark', |
| 140 | + }, |
| 141 | + black: { |
| 142 | + base: '--p-text', |
| 143 | + }, |
| 144 | + white: { |
| 145 | + base: '--p-text-on-dark', |
| 146 | + }, |
| 147 | +}; |
| 148 | + |
| 149 | +const backgroundColorMap = { |
| 150 | + green: { |
| 151 | + light: '--p-surface-success', |
| 152 | + lighter: '--p-surface-success-subdued', |
| 153 | + }, |
| 154 | + yellow: { |
| 155 | + light: '--p-surface-warning', |
| 156 | + lighter: '--p-surface-warning-subdued', |
| 157 | + }, |
| 158 | + red: { |
| 159 | + light: '--p-surface-critical', |
| 160 | + lighter: '--p-surface-critical-subdued', |
| 161 | + }, |
| 162 | + ink: { |
| 163 | + dark: '--p-surface-dark', |
| 164 | + base: '--p-surface-neutral-subdued-dark', |
| 165 | + }, |
| 166 | + sky: { |
| 167 | + base: '--p-surface-neutral', |
| 168 | + light: '--p-surface-neutral-subdued', |
| 169 | + lighter: '--p-surface-subdued', |
| 170 | + }, |
| 171 | + black: { |
| 172 | + base: '--p-surface-dark', |
| 173 | + }, |
| 174 | + white: { |
| 175 | + base: '--p-surface', |
| 176 | + }, |
| 177 | +}; |
| 178 | + |
| 179 | +const borderColorMap = { |
| 180 | + green: { |
| 181 | + dark: '--p-border-success', |
| 182 | + base: '--p-border-success', |
| 183 | + light: '--p-border-success-subdued', |
| 184 | + lighter: '--p-border-success-subdued', |
| 185 | + }, |
| 186 | + yellow: { |
| 187 | + dark: '--p-border-warning', |
| 188 | + base: '--p-border-warning', |
| 189 | + light: '--p-border-warning-disabled', |
| 190 | + lighter: '--p-border-warning-subdued', |
| 191 | + }, |
| 192 | + red: { |
| 193 | + dark: '--p-border-critical', |
| 194 | + base: '--p-border-critical', |
| 195 | + light: '--p-border-critical-subdued', |
| 196 | + lighter: '--p-border-critical-subdued', |
| 197 | + }, |
| 198 | + ink: { |
| 199 | + lightest: '--p-border', |
| 200 | + }, |
| 201 | + sky: { |
| 202 | + light: '--p-border-subdued', |
| 203 | + }, |
| 204 | +}; |
| 205 | + |
| 206 | +const fillColorMap = { |
| 207 | + green: { |
| 208 | + dark: '--p-icon-success', |
| 209 | + base: '--p-icon-success', |
| 210 | + }, |
| 211 | + yellow: { |
| 212 | + dark: '--p-icon-warning', |
| 213 | + base: '--p-icon-warning', |
| 214 | + }, |
| 215 | + red: { |
| 216 | + dark: '--p-icon-critical', |
| 217 | + base: '--p-icon-critical', |
| 218 | + }, |
| 219 | + ink: { |
| 220 | + base: '--p-icon', |
| 221 | + light: '--p-icon', |
| 222 | + lighter: '--p-icon-subdued', |
| 223 | + lightest: '--p-icon-disabled', |
| 224 | + }, |
| 225 | + black: { |
| 226 | + base: '--p-icon', |
| 227 | + }, |
| 228 | + white: { |
| 229 | + base: '--p-icon-on-dark', |
| 230 | + }, |
| 231 | +}; |
| 232 | + |
| 233 | +const propertyMaps = { |
| 234 | + color: colorMap, |
| 235 | + background: backgroundColorMap, |
| 236 | + 'background-color': backgroundColorMap, |
| 237 | + border: borderColorMap, |
| 238 | + 'border-color': borderColorMap, |
| 239 | + fill: fillColorMap, |
| 240 | +}; |
| 241 | + |
| 242 | +const polarisCustomPropertyRegEx = new RegExp( |
| 243 | + Object.keys(tokenColors).map(createVar).join('|'), |
| 244 | +); |
0 commit comments