{"version":3,"sources":["../../src/processors/css.ts","../../src/processors/helpers/atomize.ts","../../src/processors/helpers/propertyPriority.ts"],"sourcesContent":["import type { SourceLocation } from '@babel/types';\nimport type { Rules, ValueCache } from '@wyw-in-js/processor-utils';\nimport { logger } from '@wyw-in-js/shared';\n\nimport CssProcessor from '@linaria/core/processors/css';\n\nimport atomize from './helpers/atomize';\n\nconst debug = logger.extend('AtomicCssProcessor');\n\nexport default class AtomicCssProcessor extends CssProcessor {\n  #classes: string | undefined;\n\n  private get classes(): string {\n    if (typeof this.#classes !== 'undefined') {\n      return this.#classes;\n    }\n\n    throw new Error('Styles are not extracted yet. Please call `build` first.');\n  }\n\n  public override doRuntimeReplacement(): void {\n    this.replacer(this.astService.stringLiteral(this.classes), false);\n  }\n\n  public override extractRules(\n    valueCache: ValueCache,\n    cssText: string,\n    loc?: SourceLocation | null\n  ): Rules {\n    const rules: Rules = {};\n\n    const atomicRules = atomize(cssText, false);\n    atomicRules.forEach((rule) => {\n      // eslint-disable-next-line no-param-reassign\n      rules[rule.cssText] = {\n        cssText: rule.cssText,\n        start: loc?.start ?? null,\n        className: this.className!,\n        displayName: this.displayName!,\n        atom: true,\n      };\n\n      debug('extracted-atomic-rule:\\n%s', rule.cssText);\n    });\n\n    this.#classes = atomicRules\n      // Some atomic rules produced (eg. keyframes) don't have class names, and they also don't need to appear in the object\n      .filter((rule) => !!rule.className)\n      .map((rule) => rule.className!)\n      .join(' ');\n\n    return rules;\n  }\n}\n","import { slugify } from '@wyw-in-js/shared';\nimport { all as knownProperties } from 'known-css-properties';\nimport type { Document, AtRule, Container, Rule } from 'postcss';\nimport postcss from 'postcss';\nimport { compile, serialize, stringify } from 'stylis';\n\nimport { getPropertyPriority } from './propertyPriority';\n\nconst knownPropertiesMap = knownProperties.reduce(\n  (acc: { [property: string]: number }, property, i) => {\n    acc[property] = i;\n    return acc;\n  },\n  {}\n);\n\nfunction hashProperty(property: string) {\n  const index = knownPropertiesMap[property];\n  // If it's a known property, let's use the index to cut down the length of the hash.\n  // otherwise, slugify\n  if (index !== undefined) {\n    return index.toString(36); // base 36 so that we get a-z,0-9\n  }\n  return slugify(property);\n}\n\nconst parseCss = (cssText: string) => {\n  try {\n    return postcss.parse(cssText);\n  } catch (e) {\n    if (e instanceof Error) {\n      throw new Error(`Error parsing CSS: ${e.message}\\nCSS:\\n${cssText}`);\n    }\n\n    throw new Error(`Unknown error parsing CSS.\\nCSS:\\n${cssText}`);\n  }\n};\n\nexport default function atomize(cssText: string, hasPriority = false) {\n  const atomicRules: {\n    className?: string;\n    cssText: string;\n    property: string;\n  }[] = [];\n\n  const stylesheet = parseCss(cssText);\n\n  // We want to extract all keyframes and leave them as-is.\n  // This isn't scoped locally yet\n  stylesheet.walkAtRules('keyframes', (atRule) => {\n    atRule.remove();\n    atomicRules.push({\n      property: atRule.name,\n      cssText: atRule.toString(),\n    });\n  });\n\n  stylesheet.walkDecls((decl) => {\n    let thisParent: Document | Container | undefined = decl.parent;\n    const parents: (Document | Container)[] = [];\n    const atomicProperty = [decl.prop];\n    let hasAtRule = false;\n\n    // Traverse the declarations parents, and collect them all.\n    while (thisParent && thisParent !== stylesheet) {\n      parents.unshift(thisParent);\n      if (thisParent.type === 'atrule') {\n        hasAtRule = true;\n        // @media queries, @supports etc.\n        atomicProperty.push(\n          (thisParent as AtRule).name,\n          (thisParent as AtRule).params\n        );\n      } else if (thisParent.type === 'rule') {\n        // pseudo classes etc.\n        atomicProperty.push((thisParent as Rule).selector);\n      }\n\n      thisParent = thisParent.parent;\n    }\n\n    // Create a new stylesheet that contains *just* the extracted atomic rule and wrapping selectors, eg.\n    // `@media (max-width: 400px) { background: red; }`, or\n    // `&:hover { background: red; }`, or\n    // `background: red;`\n    // We do this so we can run it through stylis, to produce a full atom, eg.\n    // `@media (max-width: 400px) { .atm_foo { background: red; } }`\n    const root = postcss.root();\n    let container: Document | Container = root;\n    parents.forEach((parent) => {\n      const newNode = parent.clone();\n      newNode.removeAll();\n      container.append(newNode);\n      container = newNode;\n    });\n    container.append(decl.clone());\n\n    const css = root.toString();\n    const propertySlug = hashProperty([...atomicProperty].join(';'));\n    const valueSlug = slugify(decl.value);\n    const className = `atm_${propertySlug}_${valueSlug}`;\n\n    const propertyPriority =\n      getPropertyPriority(decl.prop) +\n      (hasAtRule ? 1 : 0) +\n      (hasPriority ? 1 : 0);\n    const selector = `.${className}`.repeat(propertyPriority);\n    const processedCss = serialize(compile(`${selector} {${css}}`), stringify);\n\n    atomicRules.push({\n      property: atomicProperty.join(' '),\n      className,\n      cssText: processedCss,\n    });\n  });\n\n  return atomicRules;\n}\n","const shorthandProperties = {\n  // The `all` property resets everything, and should effectively have priority zero.\n  // In practice, this can be achieved by using: div { all: ... }  to have even less specificity, but to avoid duplicating all selectors, we just let it be\n  // 'all': []\n  animation: [\n    'animation-name',\n    'animation-duration',\n    'animation-timing-function',\n    'animation-delay',\n    'animation-iteration-count',\n    'animation-direction',\n    'animation-fill-mode',\n    'animation-play-state',\n  ],\n  background: [\n    'background-attachment',\n    'background-clip',\n    'background-color',\n    'background-image',\n    'background-origin',\n    'background-position',\n    'background-repeat',\n    'background-size',\n  ],\n  border: ['border-color', 'border-style', 'border-width'],\n  'border-block-end': [\n    'border-block-end-color',\n    'border-block-end-style',\n    'border-block-end-width',\n  ],\n  'border-block-start': [\n    'border-block-start-color',\n    'border-block-start-style',\n    'border-block-start-width',\n  ],\n  'border-bottom': [\n    'border-bottom-color',\n    'border-bottom-style',\n    'border-bottom-width',\n  ],\n  'border-color': [\n    'border-bottom-color',\n    'border-left-color',\n    'border-right-color',\n    'border-top-color',\n  ],\n  'border-image': [\n    'border-image-outset',\n    'border-image-repeat',\n    'border-image-slice',\n    'border-image-source',\n    'border-image-width',\n  ],\n  'border-inline-end': [\n    'border-inline-end-color',\n    'border-inline-end-style',\n    'border-inline-end-width',\n  ],\n  'border-inline-start': [\n    'border-inline-start-color',\n    'border-inline-start-style',\n    'border-inline-start-width',\n  ],\n  'border-left': [\n    'border-left-color',\n    'border-left-style',\n    'border-left-width',\n  ],\n  'border-radius': [\n    'border-top-left-radius',\n    'border-top-right-radius',\n    'border-bottom-right-radius',\n    'border-bottom-left-radius',\n  ],\n  'border-right': [\n    'border-right-color',\n    'border-right-style',\n    'border-right-width',\n  ],\n  'border-style': [\n    'border-bottom-style',\n    'border-left-style',\n    'border-right-style',\n    'border-top-style',\n  ],\n  'border-top': ['border-top-color', 'border-top-style', 'border-top-width'],\n  'border-width': [\n    'border-bottom-width',\n    'border-left-width',\n    'border-right-width',\n    'border-top-width',\n  ],\n  'column-rule': [\n    'column-rule-width',\n    'column-rule-style',\n    'column-rule-color',\n  ],\n  columns: ['column-count', 'column-width'],\n  flex: ['flex-grow', 'flex-shrink', 'flex-basis'],\n  'flex-flow': ['flex-direction', 'flex-wrap'],\n  font: [\n    'font-family',\n    'font-size',\n    'font-stretch',\n    'font-style',\n    'font-variant',\n    'font-weight',\n    'line-height',\n  ],\n  gap: ['row-gap', 'column-gap'],\n  grid: [\n    'grid-auto-columns',\n    'grid-auto-flow',\n    'grid-auto-rows',\n    'grid-template-areas',\n    'grid-template-columns',\n    'grid-template-rows',\n  ],\n  'grid-area': [\n    'grid-row-start',\n    'grid-column-start',\n    'grid-row-end',\n    'grid-column-end',\n  ],\n  'grid-column': ['grid-column-end', 'grid-column-start'],\n  'grid-row': ['grid-row-end', 'grid-row-start'],\n  'grid-template': [\n    'grid-template-areas',\n    'grid-template-columns',\n    'grid-template-rows',\n  ],\n  'list-style': ['list-style-image', 'list-style-position', 'list-style-type'],\n  margin: ['margin-bottom', 'margin-left', 'margin-right', 'margin-top'],\n  mask: [\n    'mask-clip',\n    'mask-composite',\n    'mask-image',\n    'mask-mode',\n    'mask-origin',\n    'mask-position',\n    'mask-repeat',\n    'mask-size',\n  ],\n  offset: [\n    'offset-anchor',\n    'offset-distance',\n    'offset-path',\n    'offset-position',\n    'offset-rotate',\n  ],\n  outline: ['outline-color', 'outline-style', 'outline-width'],\n  overflow: ['overflow-x', 'overflow-y'],\n  padding: ['padding-bottom', 'padding-left', 'padding-right', 'padding-top'],\n  'place-content': ['align-content', 'justify-content'],\n  'place-items': ['align-items', 'justify-items'],\n  'place-self': ['align-self', 'justify-self'],\n  'scroll-margin': [\n    'scroll-margin-bottom',\n    'scroll-margin-left',\n    'scroll-margin-right',\n    'scroll-margin-top',\n  ],\n  'scroll-padding': [\n    'scroll-padding-bottom',\n    'scroll-padding-left',\n    'scroll-padding-right',\n    'scroll-padding-top',\n  ],\n  'text-decoration': [\n    'text-decoration-color',\n    'text-decoration-line',\n    'text-decoration-style',\n    'text-decoration-thickness',\n  ],\n  'text-emphasis': ['text-emphasis-color', 'text-emphasis-style'],\n  transition: [\n    'transition-delay',\n    'transition-duration',\n    'transition-property',\n    'transition-timing-function',\n  ],\n};\n\n// Get the property priority: the higher the priority, the higher the resulting\n// specificity of the atom. For example, if we had:\n//\n// import { css } from '@linaria/atomic';\n// css`\n//   background-color: blue;\n//   background: red;\n// `;\n//\n// we would produce:\n//\n// .atm_a.atm_a { background-color: blue }\n// .atm_b { background: red }\n//\n// and so the more specific selector (.atm_a.atm_a) would win\nexport function getPropertyPriority(property: string) {\n  const longhands = Object.values(shorthandProperties).reduce(\n    (a, b) => [...a, ...b],\n    []\n  );\n\n  return longhands.includes(property) ? 2 : 1;\n}\n"],"mappings":";AAEA,SAAS,cAAc;AAEvB,OAAO,kBAAkB;;;ACJzB,SAAS,eAAe;AACxB,SAAS,OAAO,uBAAuB;AAEvC,OAAO,aAAa;AACpB,SAAS,SAAS,WAAW,iBAAiB;;;ACJ9C,IAAM,sBAAsB;AAAA;AAAA;AAAA;AAAA,EAI1B,WAAW;AAAA,IACT;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAAA,EACA,YAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;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