@linaria/react
Advanced tools
+41
-48
| import BaseProcessor from '@linaria/core/processors/BaseProcessor'; | ||
| export function hasMeta(value) { | ||
| return typeof value === 'object' && value !== null && '__linaria' in value; | ||
| } | ||
| import { ValueType } from '@linaria/core/processors/types'; | ||
| import hasMeta from '@linaria/core/processors/utils/hasMeta'; | ||
@@ -18,2 +17,5 @@ const isNotNull = x => x !== null; | ||
| export default class StyledProcessor extends BaseProcessor { | ||
| #variableIdx = 0; | ||
| #variablesCache = new Map(); | ||
| constructor(...args) { | ||
@@ -25,19 +27,10 @@ super(...args); | ||
| if (type === 'call' && rest.length === 0) { | ||
| const [source, path] = value; | ||
| if (path.node.type === 'StringLiteral') { | ||
| component = path.node.value; | ||
| } else if (path.node.type === 'ArrowFunctionExpression' || path.node.type === 'FunctionExpression') { | ||
| // Special case when styled wraps a function | ||
| // It's actually the same as wrapping a built-in tag | ||
| if (value.kind === ValueType.FUNCTION) { | ||
| component = 'FunctionalComponent'; | ||
| } else { | ||
| component = { | ||
| node: path.node, | ||
| source | ||
| node: value.ex, | ||
| source: value.source | ||
| }; | ||
| this.dependencies.push({ | ||
| ex: path, | ||
| source | ||
| }); | ||
| this.dependencies.push(value); | ||
| } | ||
@@ -47,7 +40,3 @@ } | ||
| if (type === 'member') { | ||
| if (value.node.type === 'Identifier') { | ||
| component = value.node.name; | ||
| } else if (value.node.type === 'StringLiteral') { | ||
| component = value.node.value; | ||
| } | ||
| component = value; | ||
| } | ||
@@ -62,4 +51,4 @@ | ||
| addInterpolation(node, source) { | ||
| const id = this.getVariableId(source); | ||
| addInterpolation(node, source, unit = '') { | ||
| const id = this.getVariableId(source + unit); | ||
| this.interpolations.push({ | ||
@@ -69,7 +58,17 @@ id, | ||
| source, | ||
| unit: '' | ||
| unit | ||
| }); | ||
| return `var(--${id})`; | ||
| return id; | ||
| } | ||
| doEvaltimeReplacement() { | ||
| this.replacer(this.value, false); | ||
| } | ||
| doRuntimeReplacement() { | ||
| const t = this.astService; | ||
| const props = this.getProps(); | ||
| this.replacer(t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]), true); | ||
| } | ||
| extractRules(valueCache, cssText, loc) { | ||
@@ -81,3 +80,3 @@ const rules = {}; | ||
| let value = typeof this.component === 'string' ? null : valueCache.get(this.component.node); | ||
| let value = typeof this.component === 'string' ? null : valueCache.get(this.component.node.name); | ||
@@ -95,11 +94,5 @@ while (hasMeta(value)) { | ||
| }; | ||
| return [rules, this.className]; | ||
| return rules; | ||
| } | ||
| getRuntimeReplacement(classes, uniqInterpolations) { | ||
| const t = this.astService; | ||
| const props = this.getProps(classes, uniqInterpolations); | ||
| return [t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]), true]; | ||
| } | ||
| get asSelector() { | ||
@@ -120,3 +113,3 @@ return `.${this.className}`; | ||
| return t.identifier(this.component.source); | ||
| return t.callExpression(t.identifier(this.component.node.name), []); | ||
| } | ||
@@ -129,14 +122,9 @@ | ||
| get valueSource() { | ||
| const extendsNode = typeof this.component === 'string' ? null : this.component.source; | ||
| return `{ | ||
| displayName: "${this.displayName}", | ||
| __linaria: { | ||
| className: "${this.className}", | ||
| extends: ${extendsNode} | ||
| } | ||
| }`; | ||
| get value() { | ||
| const t = this.astService; | ||
| const extendsNode = typeof this.component === 'string' ? null : this.component.node.name; | ||
| return t.objectExpression([t.objectProperty(t.stringLiteral('displayName'), t.stringLiteral(this.displayName)), t.objectProperty(t.stringLiteral('__linaria'), t.objectExpression([t.objectProperty(t.stringLiteral('className'), t.stringLiteral(this.className)), t.objectProperty(t.stringLiteral('extends'), extendsNode ? t.callExpression(t.identifier(extendsNode), []) : t.nullLiteral())]))]); | ||
| } | ||
| getProps(classes, uniqInterpolations) { | ||
| getProps() { | ||
| const propsObj = { | ||
@@ -149,3 +137,3 @@ name: this.displayName, | ||
| propsObj.vars = {}; | ||
| uniqInterpolations.forEach(({ | ||
| this.interpolations.forEach(({ | ||
| id, | ||
@@ -155,3 +143,3 @@ unit, | ||
| }) => { | ||
| const items = [node]; | ||
| const items = [this.astService.callExpression(node, [])]; | ||
@@ -196,4 +184,9 @@ if (unit) { | ||
| getVariableId(value) { | ||
| // make the variable unique to this styled component | ||
| return `${this.slug}-${this.interpolations.length}`; | ||
| if (!this.#variablesCache.has(value)) { | ||
| // make the variable unique to this styled component | ||
| // eslint-disable-next-line no-plusplus | ||
| this.#variablesCache.set(value, `${this.slug}-${this.#variableIdx++}`); | ||
| } | ||
| return this.#variablesCache.get(value); | ||
| } | ||
@@ -200,0 +193,0 @@ |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"styled.js","names":["BaseProcessor","hasMeta","value","isNotNull","x","singleQuotedStringLiteral","type","extra","rawValue","raw","StyledProcessor","constructor","args","component","rest","params","length","source","path","node","dependencies","push","ex","name","Error","addInterpolation","id","getVariableId","interpolations","unit","extractRules","valueCache","cssText","loc","rules","selector","className","get","__linaria","extends","displayName","start","getRuntimeReplacement","classes","uniqInterpolations","t","astService","props","getProps","callExpression","tagExpression","getTagComponentProps","asSelector","tagExpressionArgument","arrowFunctionExpression","blockStatement","identifier","tagExp","valueSource","extendsNode","propsObj","class","vars","forEach","items","stringLiteral","propExpressions","Object","entries","map","key","keyNode","objectProperty","booleanLiteral","propName","propValue","arrayExpression","objectExpression","filter","slug"],"sources":["../../src/processors/styled.ts"],"sourcesContent":["import type {\n CallExpression,\n Expression,\n ObjectExpression,\n SourceLocation,\n StringLiteral,\n} from '@babel/types';\n\nimport type { StyledMeta } from '@linaria/core';\nimport type { ProcessorParams } from '@linaria/core/processors/BaseProcessor';\nimport BaseProcessor from '@linaria/core/processors/BaseProcessor';\nimport type {\n Rules,\n WrappedNode,\n IInterpolation,\n ValueCache,\n} from '@linaria/core/processors/types';\n\nexport function hasMeta(value: unknown): value is StyledMeta {\n return typeof value === 'object' && value !== null && '__linaria' in value;\n}\n\nconst isNotNull = <T>(x: T | null): x is T => x !== null;\n\nexport interface IProps {\n atomic?: boolean;\n class?: string;\n name: string;\n vars?: Record<string, Expression[]>;\n}\n\nconst singleQuotedStringLiteral = (value: string): StringLiteral => ({\n type: 'StringLiteral',\n value,\n extra: {\n rawValue: value,\n raw: `'${value}'`,\n },\n});\n\nexport default class StyledProcessor extends BaseProcessor {\n public component: WrappedNode;\n\n constructor(...args: ProcessorParams) {\n super(...args);\n\n let component: WrappedNode | undefined;\n const [type, value, ...rest] = this.params[0] ?? [];\n if (type === 'call' && rest.length === 0) {\n const [source, path] = value;\n if (path.node.type === 'StringLiteral') {\n component = path.node.value;\n } else if (\n path.node.type === 'ArrowFunctionExpression' ||\n path.node.type === 'FunctionExpression'\n ) {\n // Special case when styled wraps a function\n // It's actually the same as wrapping a built-in tag\n component = 'FunctionalComponent';\n } else {\n component = {\n node: path.node,\n source,\n };\n this.dependencies.push({\n ex: path,\n source,\n });\n }\n }\n\n if (type === 'member') {\n if (value.node.type === 'Identifier') {\n component = value.node.name;\n } else if (value.node.type === 'StringLiteral') {\n component = value.node.value;\n }\n }\n\n if (!component || this.params.length > 1) {\n throw new Error('Invalid usage of `styled` tag');\n }\n\n this.component = component;\n }\n\n public override addInterpolation(node: Expression, source: string) {\n const id = this.getVariableId(source);\n\n this.interpolations.push({\n id,\n node,\n source,\n unit: '',\n });\n\n return `var(--${id})`;\n }\n\n public override extractRules(\n valueCache: ValueCache,\n cssText: string,\n loc?: SourceLocation | null\n ): [Rules, string] {\n const rules: Rules = {};\n\n let selector = `.${this.className}`;\n\n // If `styled` wraps another component and not a primitive,\n // get its class name to create a more specific selector\n // it'll ensure that styles are overridden properly\n let value =\n typeof this.component === 'string'\n ? null\n : valueCache.get(this.component.node);\n while (hasMeta(value)) {\n selector += `.${value.__linaria.className}`;\n value = value.__linaria.extends;\n }\n\n rules[selector] = {\n cssText,\n className: this.className,\n displayName: this.displayName,\n start: loc?.start ?? null,\n };\n\n return [rules, this.className];\n }\n\n public override getRuntimeReplacement(\n classes: string,\n uniqInterpolations: IInterpolation[]\n ): [Expression, boolean] {\n const t = this.astService;\n\n const props = this.getProps(classes, uniqInterpolations);\n\n return [\n t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]),\n true,\n ];\n }\n\n public override get asSelector(): string {\n return `.${this.className}`;\n }\n\n protected get tagExpressionArgument(): Expression {\n const t = this.astService;\n if (typeof this.component === 'string') {\n if (this.component === 'FunctionalComponent') {\n return t.arrowFunctionExpression([], t.blockStatement([]));\n }\n\n return singleQuotedStringLiteral(this.component);\n }\n\n return t.identifier(this.component.source);\n }\n\n protected get tagExpression(): CallExpression {\n const t = this.astService;\n return t.callExpression(this.tagExp, [this.tagExpressionArgument]);\n }\n\n public override get valueSource(): string {\n const extendsNode =\n typeof this.component === 'string' ? null : this.component.source;\n return `{\n displayName: \"${this.displayName}\",\n __linaria: {\n className: \"${this.className}\",\n extends: ${extendsNode}\n }\n }`;\n }\n\n protected getProps(\n classes: string,\n uniqInterpolations: IInterpolation[]\n ): IProps {\n const propsObj: IProps = {\n name: this.displayName,\n class: this.className,\n };\n\n // If we found any interpolations, also pass them, so they can be applied\n if (this.interpolations.length) {\n propsObj.vars = {};\n uniqInterpolations.forEach(({ id, unit, node }) => {\n const items: Expression[] = [node];\n\n if (unit) {\n items.push(this.astService.stringLiteral(unit));\n }\n\n propsObj.vars![id] = items;\n });\n }\n\n return propsObj;\n }\n\n protected getTagComponentProps(props: IProps): ObjectExpression {\n const t = this.astService;\n\n const propExpressions = Object.entries(props)\n .map(([key, value]: [key: string, value: IProps[keyof IProps]]) => {\n if (!value) {\n return null;\n }\n\n const keyNode = t.identifier(key);\n\n if (typeof value === 'string') {\n return t.objectProperty(keyNode, t.stringLiteral(value));\n }\n\n if (typeof value === 'boolean') {\n return t.objectProperty(keyNode, t.booleanLiteral(value));\n }\n\n const vars = Object.entries(value).map(([propName, propValue]) => {\n return t.objectProperty(\n t.stringLiteral(propName),\n t.arrayExpression(propValue)\n );\n });\n\n return t.objectProperty(keyNode, t.objectExpression(vars));\n })\n .filter(isNotNull);\n\n return t.objectExpression(propExpressions);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected getVariableId(value: string): string {\n // make the variable unique to this styled component\n return `${this.slug}-${this.interpolations.length}`;\n }\n}\n"],"mappings":"AAUA,OAAOA,aAAP,MAA0B,wCAA1B;AAQA,OAAO,SAASC,OAAT,CAAiBC,KAAjB,EAAsD;EAC3D,OAAO,OAAOA,KAAP,KAAiB,QAAjB,IAA6BA,KAAK,KAAK,IAAvC,IAA+C,eAAeA,KAArE;AACD;;AAED,MAAMC,SAAS,GAAOC,CAAJ,IAA4BA,CAAC,KAAK,IAApD;;AASA,MAAMC,yBAAyB,GAAIH,KAAD,KAAmC;EACnEI,IAAI,EAAE,eAD6D;EAEnEJ,KAFmE;EAGnEK,KAAK,EAAE;IACLC,QAAQ,EAAEN,KADL;IAELO,GAAG,EAAG,IAAGP,KAAM;EAFV;AAH4D,CAAnC,CAAlC;;AASA,eAAe,MAAMQ,eAAN,SAA8BV,aAA9B,CAA4C;EAGzDW,WAAW,CAAC,GAAGC,IAAJ,EAA2B;IACpC,MAAM,GAAGA,IAAT;IAEA,IAAIC,SAAJ;IACA,MAAM,CAACP,IAAD,EAAOJ,KAAP,EAAc,GAAGY,IAAjB,IAAyB,KAAKC,MAAL,CAAY,CAAZ,KAAkB,EAAjD;;IACA,IAAIT,IAAI,KAAK,MAAT,IAAmBQ,IAAI,CAACE,MAAL,KAAgB,CAAvC,EAA0C;MACxC,MAAM,CAACC,MAAD,EAASC,IAAT,IAAiBhB,KAAvB;;MACA,IAAIgB,IAAI,CAACC,IAAL,CAAUb,IAAV,KAAmB,eAAvB,EAAwC;QACtCO,SAAS,GAAGK,IAAI,CAACC,IAAL,CAAUjB,KAAtB;MACD,CAFD,MAEO,IACLgB,IAAI,CAACC,IAAL,CAAUb,IAAV,KAAmB,yBAAnB,IACAY,IAAI,CAACC,IAAL,CAAUb,IAAV,KAAmB,oBAFd,EAGL;QACA;QACA;QACAO,SAAS,GAAG,qBAAZ;MACD,CAPM,MAOA;QACLA,SAAS,GAAG;UACVM,IA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AAY;QACV,OAAO,IAAP;MACD;;MAED,MAAMqE,OAAO,GAAG1B,CAAC,CAACW,UAAF,CAAac,GAAb,CAAhB;;MAEA,IAAI,OAAOpE,KAAP,KAAiB,QAArB,EAA+B;QAC7B,OAAO2C,CAAC,CAAC2B,cAAF,CAAiBD,OAAjB,EAA0B1B,CAAC,CAACoB,aAAF,CAAgB/D,KAAhB,CAA1B,CAAP;MACD;;MAED,IAAI,OAAOA,KAAP,KAAiB,SAArB,EAAgC;QAC9B,OAAO2C,CAAC,CAAC2B,cAAF,CAAiBD,OAAjB,EAA0B1B,CAAC,CAAC4B,cAAF,CAAiBvE,KAAjB,CAA1B,CAAP;MACD;;MAED,MAAM4D,IAAI,GAAGK,MAAM,CAACC,OAAP,CAAelE,KAAf,EAAsBmE,GAAtB,CAA0B,CAAC,CAACK,QAAD,EAAWC,SAAX,CAAD,KAA2B;QAChE,OAAO9B,CAAC,CAAC2B,cAAF,CACL3B,CAAC,CAACoB,aAAF,CAAgBS,QAAhB,CADK,EAEL7B,CAAC,CAAC+B,eAAF,CAAkBD,SAAlB,CAFK,CAAP;MAID,CALY,CAAb;MAOA,OAAO9B,CAAC,CAAC2B,cAAF,CAAiBD,OAAjB,EAA0B1B,CAAC,CAACgC,gBAAF,CAAmBf,IAAnB,CAA1B,CAAP;IACD,CAxBqB,EAyBrBgB,MAzBqB,CAyBd3E,SAzBc,CAAxB;IA2BA,OAAO0C,CAAC,CAACgC,gBAAF,CAAmBX,eAAnB,CAAP;EACD,CAnMwD,CAqMzD;;;EACUvC,aAAa,CAACzB,KAAD,EAAwB;IAC7C;IACA,OAAQ,GAAE,KAAK6E,IAAK,IAAG,KAAKnD,cAAL,CAAoBZ,MAAO,EAAlD;EACD;;AAzMwD"} | ||
| {"version":3,"file":"styled.js","names":["BaseProcessor","ValueType","hasMeta","isNotNull","x","singleQuotedStringLiteral","value","type","extra","rawValue","raw","StyledProcessor","variableIdx","variablesCache","Map","constructor","args","component","rest","params","length","kind","FUNCTION","node","ex","source","dependencies","push","Error","addInterpolation","unit","id","getVariableId","interpolations","doEvaltimeReplacement","replacer","doRuntimeReplacement","t","astService","props","getProps","callExpression","tagExpression","getTagComponentProps","extractRules","valueCache","cssText","loc","rules","selector","className","get","name","__linaria","extends","displayName","start","asSelector","tagExpressionArgument","arrowFunctionExpression","blockStatement","identifier","tagExp","extendsNode","objectExpression","objectProperty","stringLiteral","nullLiteral","propsObj","class","vars","forEach","items","propExpressions","Object","entries","map","key","keyNode","booleanLiteral","propName","propValue","arrayExpression","filter","has","set","slug"],"sources":["../../src/processors/styled.ts"],"sourcesContent":["import type {\n CallExpression,\n Expression,\n ObjectExpression,\n SourceLocation,\n StringLiteral,\n} from '@babel/types';\n\nimport type { ProcessorParams } from '@linaria/core/processors/BaseProcessor';\nimport BaseProcessor from '@linaria/core/processors/BaseProcessor';\nimport type {\n Rules,\n WrappedNode,\n ValueCache,\n} from '@linaria/core/processors/types';\nimport { ValueType } from '@linaria/core/processors/types';\nimport hasMeta from '@linaria/core/processors/utils/hasMeta';\n\nconst isNotNull = <T>(x: T | null): x is T => x !== null;\n\nexport interface IProps {\n atomic?: boolean;\n class?: string;\n name: string;\n vars?: Record<string, Expression[]>;\n}\n\nconst singleQuotedStringLiteral = (value: string): StringLiteral => ({\n type: 'StringLiteral',\n value,\n extra: {\n rawValue: value,\n raw: `'${value}'`,\n },\n});\n\nexport default class StyledProcessor extends BaseProcessor {\n public component: WrappedNode;\n\n #variableIdx = 0;\n\n #variablesCache = new Map<string, string>();\n\n constructor(...args: ProcessorParams) {\n super(...args);\n\n let component: WrappedNode | undefined;\n const [type, value, ...rest] = this.params[0] ?? [];\n if (type === 'call' && rest.length === 0) {\n if (value.kind === ValueType.FUNCTION) {\n component = 'FunctionalComponent';\n } else {\n component = {\n node: value.ex,\n source: value.source,\n };\n\n this.dependencies.push(value);\n }\n }\n\n if (type === 'member') {\n component = value;\n }\n\n if (!component || this.params.length > 1) {\n throw new Error('Invalid usage of `styled` tag');\n }\n\n this.component = component;\n }\n\n public override addInterpolation(\n node: Expression,\n source: string,\n unit = ''\n ): string {\n const id = this.getVariableId(source + unit);\n\n this.interpolations.push({\n id,\n node,\n source,\n unit,\n });\n\n return id;\n }\n\n public override doEvaltimeReplacement(): void {\n this.replacer(this.value, false);\n }\n\n public override doRuntimeReplacement(): void {\n const t = this.astService;\n\n const props = this.getProps();\n\n this.replacer(\n t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]),\n true\n );\n }\n\n public override extractRules(\n valueCache: ValueCache,\n cssText: string,\n loc?: SourceLocation | null\n ): Rules {\n const rules: Rules = {};\n\n let selector = `.${this.className}`;\n\n // If `styled` wraps another component and not a primitive,\n // get its class name to create a more specific selector\n // it'll ensure that styles are overridden properly\n let value =\n typeof this.component === 'string'\n ? null\n : valueCache.get(this.component.node.name);\n while (hasMeta(value)) {\n selector += `.${value.__linaria.className}`;\n value = value.__linaria.extends;\n }\n\n rules[selector] = {\n cssText,\n className: this.className,\n displayName: this.displayName,\n start: loc?.start ?? null,\n };\n\n return rules;\n }\n\n public override get asSelector(): string {\n return `.${this.className}`;\n }\n\n protected get tagExpressionArgument(): Expression {\n const t = this.astService;\n if (typeof this.component === 'string') {\n if (this.component === 'FunctionalComponent') {\n return t.arrowFunctionExpression([], t.blockStatement([]));\n }\n\n return singleQuotedStringLiteral(this.component);\n }\n\n return t.callExpression(t.identifier(this.component.node.name), []);\n }\n\n protected get tagExpression(): CallExpression {\n const t = this.astService;\n return t.callExpression(this.tagExp, [this.tagExpressionArgument]);\n }\n\n public override get value(): ObjectExpression {\n const t = this.astService;\n const extendsNode =\n typeof this.component === 'string' ? null : this.component.node.name;\n\n return t.objectExpression([\n t.objectProperty(\n t.stringLiteral('displayName'),\n t.stringLiteral(this.displayName)\n ),\n t.objectProperty(\n t.stringLiteral('__linaria'),\n t.objectExpression([\n t.objectProperty(\n t.stringLiteral('className'),\n t.stringLiteral(this.className)\n ),\n t.objectProperty(\n t.stringLiteral('extends'),\n extendsNode\n ? t.callExpression(t.identifier(extendsNode), [])\n : t.nullLiteral()\n ),\n ])\n ),\n ]);\n }\n\n protected getProps(): IProps {\n const propsObj: IProps = {\n name: this.displayName,\n class: this.className,\n };\n\n // If we found any interpolations, also pass them, so they can be applied\n if (this.interpolations.length) {\n propsObj.vars = {};\n this.interpolations.forEach(({ id, unit, node }) => {\n const items: Expression[] = [this.astService.callExpression(node, [])];\n\n if (unit) {\n items.push(this.astService.stringLiteral(unit));\n }\n\n propsObj.vars![id] = items;\n });\n }\n\n return propsObj;\n }\n\n protected getTagComponentProps(props: IProps): ObjectExpression {\n const t = this.astService;\n\n const propExpressions = Object.entries(props)\n .map(([key, value]: [key: string, value: IProps[keyof IProps]]) => {\n if (!value) {\n return null;\n }\n\n const keyNode = t.identifier(key);\n\n if (typeof value === 'string') {\n return t.objectProperty(keyNode, t.stringLiteral(value));\n }\n\n if (typeof value === 'boolean') {\n return t.objectProperty(keyNode, t.booleanLiteral(value));\n }\n\n const vars = Object.entries(value).map(([propName, propValue]) => {\n return t.objectProperty(\n t.stringLiteral(propName),\n t.arrayExpression(propValue)\n );\n });\n\n return t.objectProperty(keyNode, t.objectExpression(vars));\n })\n .filter(isNotNull);\n\n return t.objectExpression(propExpressions);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected getVariableId(value: string): string {\n if (!this.#variablesCache.has(value)) {\n // make the variable unique to this styled component\n // eslint-disable-next-line no-plusplus\n this.#variablesCache.set(value, `${this.slug}-${this.#variableIdx++}`);\n }\n\n return this.#variablesCache.get(value)!;\n }\n}\n"],"mappings":"AASA,OAAOA,aAAP,MAA0B,wCAA1B;AAMA,SAASC,SAAT,QAA0B,gCAA1B;AACA,OAAOC,OAAP,MAAoB,wCAApB;;AAEA,MAAMC,SAAS,GAAOC,CAAJ,IAA4BA,CAAC,KAAK,IAApD;;AASA,MAAMC,yBAAyB,GAAIC,KAAD,KAAmC;EACnEC,IAAI,EAAE,eAD6D;EAEnED,KAFmE;EAGnEE,K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 @@ "use strict"; | ||
| exports.default = void 0; | ||
| exports.hasMeta = hasMeta; | ||
| var _BaseProcessor = _interopRequireDefault(require("@linaria/core/processors/BaseProcessor")); | ||
| var _types = require("@linaria/core/processors/types"); | ||
| var _hasMeta = _interopRequireDefault(require("@linaria/core/processors/utils/hasMeta")); | ||
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
| function hasMeta(value) { | ||
| return typeof value === 'object' && value !== null && '__linaria' in value; | ||
| } | ||
| const isNotNull = x => x !== null; | ||
@@ -30,2 +29,5 @@ | ||
| class StyledProcessor extends _BaseProcessor.default { | ||
| #variableIdx = 0; | ||
| #variablesCache = new Map(); | ||
| constructor(...args) { | ||
@@ -39,19 +41,10 @@ var _this$params$; | ||
| if (type === 'call' && rest.length === 0) { | ||
| const [source, path] = value; | ||
| if (path.node.type === 'StringLiteral') { | ||
| component = path.node.value; | ||
| } else if (path.node.type === 'ArrowFunctionExpression' || path.node.type === 'FunctionExpression') { | ||
| // Special case when styled wraps a function | ||
| // It's actually the same as wrapping a built-in tag | ||
| if (value.kind === _types.ValueType.FUNCTION) { | ||
| component = 'FunctionalComponent'; | ||
| } else { | ||
| component = { | ||
| node: path.node, | ||
| source | ||
| node: value.ex, | ||
| source: value.source | ||
| }; | ||
| this.dependencies.push({ | ||
| ex: path, | ||
| source | ||
| }); | ||
| this.dependencies.push(value); | ||
| } | ||
@@ -61,7 +54,3 @@ } | ||
| if (type === 'member') { | ||
| if (value.node.type === 'Identifier') { | ||
| component = value.node.name; | ||
| } else if (value.node.type === 'StringLiteral') { | ||
| component = value.node.value; | ||
| } | ||
| component = value; | ||
| } | ||
@@ -76,4 +65,4 @@ | ||
| addInterpolation(node, source) { | ||
| const id = this.getVariableId(source); | ||
| addInterpolation(node, source, unit = '') { | ||
| const id = this.getVariableId(source + unit); | ||
| this.interpolations.push({ | ||
@@ -83,7 +72,17 @@ id, | ||
| source, | ||
| unit: '' | ||
| unit | ||
| }); | ||
| return `var(--${id})`; | ||
| return id; | ||
| } | ||
| doEvaltimeReplacement() { | ||
| this.replacer(this.value, false); | ||
| } | ||
| doRuntimeReplacement() { | ||
| const t = this.astService; | ||
| const props = this.getProps(); | ||
| this.replacer(t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]), true); | ||
| } | ||
| extractRules(valueCache, cssText, loc) { | ||
@@ -97,5 +96,5 @@ var _loc$start; | ||
| let value = typeof this.component === 'string' ? null : valueCache.get(this.component.node); | ||
| let value = typeof this.component === 'string' ? null : valueCache.get(this.component.node.name); | ||
| while (hasMeta(value)) { | ||
| while ((0, _hasMeta.default)(value)) { | ||
| selector += `.${value.__linaria.className}`; | ||
@@ -111,11 +110,5 @@ value = value.__linaria.extends; | ||
| }; | ||
| return [rules, this.className]; | ||
| return rules; | ||
| } | ||
| getRuntimeReplacement(classes, uniqInterpolations) { | ||
| const t = this.astService; | ||
| const props = this.getProps(classes, uniqInterpolations); | ||
| return [t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]), true]; | ||
| } | ||
| get asSelector() { | ||
@@ -136,3 +129,3 @@ return `.${this.className}`; | ||
| return t.identifier(this.component.source); | ||
| return t.callExpression(t.identifier(this.component.node.name), []); | ||
| } | ||
@@ -145,14 +138,9 @@ | ||
| get valueSource() { | ||
| const extendsNode = typeof this.component === 'string' ? null : this.component.source; | ||
| return `{ | ||
| displayName: "${this.displayName}", | ||
| __linaria: { | ||
| className: "${this.className}", | ||
| extends: ${extendsNode} | ||
| } | ||
| }`; | ||
| get value() { | ||
| const t = this.astService; | ||
| const extendsNode = typeof this.component === 'string' ? null : this.component.node.name; | ||
| return t.objectExpression([t.objectProperty(t.stringLiteral('displayName'), t.stringLiteral(this.displayName)), t.objectProperty(t.stringLiteral('__linaria'), t.objectExpression([t.objectProperty(t.stringLiteral('className'), t.stringLiteral(this.className)), t.objectProperty(t.stringLiteral('extends'), extendsNode ? t.callExpression(t.identifier(extendsNode), []) : t.nullLiteral())]))]); | ||
| } | ||
| getProps(classes, uniqInterpolations) { | ||
| getProps() { | ||
| const propsObj = { | ||
@@ -165,3 +153,3 @@ name: this.displayName, | ||
| propsObj.vars = {}; | ||
| uniqInterpolations.forEach(({ | ||
| this.interpolations.forEach(({ | ||
| id, | ||
@@ -171,3 +159,3 @@ unit, | ||
| }) => { | ||
| const items = [node]; | ||
| const items = [this.astService.callExpression(node, [])]; | ||
@@ -212,4 +200,9 @@ if (unit) { | ||
| getVariableId(value) { | ||
| // make the variable unique to this styled component | ||
| return `${this.slug}-${this.interpolations.length}`; | ||
| if (!this.#variablesCache.has(value)) { | ||
| // make the variable unique to this styled component | ||
| // eslint-disable-next-line no-plusplus | ||
| this.#variablesCache.set(value, `${this.slug}-${this.#variableIdx++}`); | ||
| } | ||
| return this.#variablesCache.get(value); | ||
| } | ||
@@ -216,0 +209,0 @@ |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"styled.js","names":["hasMeta","value","isNotNull","x","singleQuotedStringLiteral","type","extra","rawValue","raw","StyledProcessor","BaseProcessor","constructor","args","component","rest","params","length","source","path","node","dependencies","push","ex","name","Error","addInterpolation","id","getVariableId","interpolations","unit","extractRules","valueCache","cssText","loc","rules","selector","className","get","__linaria","extends","displayName","start","getRuntimeReplacement","classes","uniqInterpolations","t","astService","props","getProps","callExpression","tagExpression","getTagComponentProps","asSelector","tagExpressionArgument","arrowFunctionExpression","blockStatement","identifier","tagExp","valueSource","extendsNode","propsObj","class","vars","forEach","items","stringLiteral","propExpressions","Object","entries","map","key","keyNode","objectProperty","booleanLiteral","propName","propValue","arrayExpression","objectExpression","filter","slug"],"sources":["../../src/processors/styled.ts"],"sourcesContent":["import type {\n CallExpression,\n Expression,\n ObjectExpression,\n SourceLocation,\n StringLiteral,\n} from '@babel/types';\n\nimport type { StyledMeta } from '@linaria/core';\nimport type { ProcessorParams } from '@linaria/core/processors/BaseProcessor';\nimport BaseProcessor from '@linaria/core/processors/BaseProcessor';\nimport type {\n Rules,\n WrappedNode,\n IInterpolation,\n ValueCache,\n} from '@linaria/core/processors/types';\n\nexport function hasMeta(value: unknown): value is StyledMeta {\n return typeof value === 'object' && value !== null && '__linaria' in value;\n}\n\nconst isNotNull = <T>(x: T | null): x is T => x !== null;\n\nexport interface IProps {\n atomic?: boolean;\n class?: string;\n name: string;\n vars?: Record<string, Expression[]>;\n}\n\nconst singleQuotedStringLiteral = (value: string): StringLiteral => ({\n type: 'StringLiteral',\n value,\n extra: {\n rawValue: value,\n raw: `'${value}'`,\n },\n});\n\nexport default class StyledProcessor extends BaseProcessor {\n public component: WrappedNode;\n\n constructor(...args: ProcessorParams) {\n super(...args);\n\n let component: WrappedNode | undefined;\n const [type, value, ...rest] = this.params[0] ?? [];\n if (type === 'call' && rest.length === 0) {\n const [source, path] = value;\n if (path.node.type === 'StringLiteral') {\n component = path.node.value;\n } else if (\n path.node.type === 'ArrowFunctionExpression' ||\n path.node.type === 'FunctionExpression'\n ) {\n // Special case when styled wraps a function\n // It's actually the same as wrapping a built-in tag\n component = 'FunctionalComponent';\n } else {\n component = {\n node: path.node,\n source,\n };\n this.dependencies.push({\n ex: path,\n source,\n });\n }\n }\n\n if (type === 'member') {\n if (value.node.type === 'Identifier') {\n component = value.node.name;\n } else if (value.node.type === 'StringLiteral') {\n component = value.node.value;\n }\n }\n\n if (!component || this.params.length > 1) {\n throw new Error('Invalid usage of `styled` tag');\n }\n\n this.component = component;\n }\n\n public override addInterpolation(node: Expression, source: string) {\n const id = this.getVariableId(source);\n\n this.interpolations.push({\n id,\n node,\n source,\n unit: '',\n });\n\n return `var(--${id})`;\n }\n\n public override extractRules(\n valueCache: ValueCache,\n cssText: string,\n loc?: SourceLocation | null\n ): [Rules, string] {\n const rules: Rules = {};\n\n let selector = `.${this.className}`;\n\n // If `styled` wraps another component and not a primitive,\n // get its class name to create a more specific selector\n // it'll ensure that styles are overridden properly\n let value =\n typeof this.component === 'string'\n ? null\n : valueCache.get(this.component.node);\n while (hasMeta(value)) {\n selector += `.${value.__linaria.className}`;\n value = value.__linaria.extends;\n }\n\n rules[selector] = {\n cssText,\n className: this.className,\n displayName: this.displayName,\n start: loc?.start ?? null,\n };\n\n return [rules, this.className];\n }\n\n public override getRuntimeReplacement(\n classes: string,\n uniqInterpolations: IInterpolation[]\n ): [Expression, boolean] {\n const t = this.astService;\n\n const props = this.getProps(classes, uniqInterpolations);\n\n return [\n t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]),\n true,\n ];\n }\n\n public override get asSelector(): string {\n return `.${this.className}`;\n }\n\n protected get tagExpressionArgument(): Expression {\n const t = this.astService;\n if (typeof this.component === 'string') {\n if (this.component === 'FunctionalComponent') {\n return t.arrowFunctionExpression([], t.blockStatement([]));\n }\n\n return singleQuotedStringLiteral(this.component);\n }\n\n return t.identifier(this.component.source);\n }\n\n protected get tagExpression(): CallExpression {\n const t = this.astService;\n return t.callExpression(this.tagExp, [this.tagExpressionArgument]);\n }\n\n public override get valueSource(): string {\n const extendsNode =\n typeof this.component === 'string' ? null : this.component.source;\n return `{\n displayName: \"${this.displayName}\",\n __linaria: {\n className: \"${this.className}\",\n extends: ${extendsNode}\n }\n }`;\n }\n\n protected getProps(\n classes: string,\n uniqInterpolations: IInterpolation[]\n ): IProps {\n const propsObj: IProps = {\n name: this.displayName,\n class: this.className,\n };\n\n // If we found any interpolations, also pass them, so they can be applied\n if (this.interpolations.length) {\n propsObj.vars = {};\n uniqInterpolations.forEach(({ id, unit, node }) => {\n const items: Expression[] = [node];\n\n if (unit) {\n items.push(this.astService.stringLiteral(unit));\n }\n\n propsObj.vars![id] = items;\n });\n }\n\n return propsObj;\n }\n\n protected getTagComponentProps(props: IProps): ObjectExpression {\n const t = this.astService;\n\n const propExpressions = Object.entries(props)\n .map(([key, value]: [key: string, value: IProps[keyof IProps]]) => {\n if (!value) {\n return null;\n }\n\n const keyNode = t.identifier(key);\n\n if (typeof value === 'string') {\n return t.objectProperty(keyNode, t.stringLiteral(value));\n }\n\n if (typeof value === 'boolean') {\n return t.objectProperty(keyNode, t.booleanLiteral(value));\n }\n\n const vars = Object.entries(value).map(([propName, propValue]) => {\n return t.objectProperty(\n t.stringLiteral(propName),\n t.arrayExpression(propValue)\n );\n });\n\n return t.objectProperty(keyNode, t.objectExpression(vars));\n })\n .filter(isNotNull);\n\n return t.objectExpression(propExpressions);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected getVariableId(value: string): string {\n // make the variable unique to this styled component\n return `${this.slug}-${this.interpolations.length}`;\n }\n}\n"],"mappings":";;;;;;;;AAUA;;;;AAQO,SAASA,OAAT,CAAiBC,KAAjB,EAAsD;EAC3D,OAAO,OAAOA,KAAP,KAAiB,QAAjB,IAA6BA,KAAK,KAAK,IAAvC,IAA+C,eAAeA,KAArE;AACD;;AAED,MAAMC,SAAS,GAAOC,CAAJ,IAA4BA,CAAC,KAAK,IAApD;;AASA,MAAMC,yBAAyB,GAAIH,KAAD,KAAmC;EACnEI,IAAI,EAAE,eAD6D;EAEnEJ,KAFmE;EAGnEK,KAAK,EAAE;IACLC,QAAQ,EAAEN,KADL;IAELO,GAAG,EAAG,IAAGP,KAAM;EAFV;AAH4D,CAAnC,CAAlC;;AASe,MAAMQ,eAAN,SAA8BC,sBAA9B,CAA4C;EAGzDC,WAAW,CAAC,GAAGC,IAAJ,EAA2B;IAAA;;IACpC,MAAM,GAAGA,IAAT;IAEA,IAAIC,SAAJ;IACA,MAAM,CAACR,IAAD,EAAOJ,KAAP,EAAc,GAAGa,IAAjB,qBAAyB,KAAKC,MAAL,CAAY,CAAZ,CAAzB,yDAA2C,EAAjD;;IACA,IAAIV,IAAI,KAAK,MAAT,IAAmBS,IAAI,CAACE,MAAL,KAAgB,CAAvC,EAA0C;MACxC,MAAM,CAACC,MAAD,EAASC,IAAT,IAAiBjB,KAAvB;;MACA,IAAIiB,IAAI,CAACC,IAAL,CAAUd,IAAV,KAAmB,eAAvB,EAAwC;QACtCQ,SAAS,GAAGK,IAAI,CAACC,IAAL,CAAUlB,KAAtB;MACD,CAFD,MAEO,IAC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,GAAE,KAAK8E,IAAK,IAAG,KAAKnD,cAAL,CAAoBZ,MAAO,EAAlD;EACD;;AAzMwD"} | ||
| {"version":3,"file":"styled.js","names":["isNotNull","x","singleQuotedStringLiteral","value","type","extra","rawValue","raw","StyledProcessor","BaseProcessor","variableIdx","variablesCache","Map","constructor","args","component","rest","params","length","kind","ValueType","FUNCTION","node","ex","source","dependencies","push","Error","addInterpolation","unit","id","getVariableId","interpolations","doEvaltimeReplacement","replacer","doRuntimeReplacement","t","astService","props","getProps","callExpression","tagExpression","getTagComponentProps","extractRules","valueCache","cssText","loc","rules","selector","className","get","name","hasMeta","__linaria","extends","displayName","start","asSelector","tagExpressionArgument","arrowFunctionExpression","blockStatement","identifier","tagExp","extendsNode","objectExpression","objectProperty","stringLiteral","nullLiteral","propsObj","class","vars","forEach","items","propExpressions","Object","entries","map","key","keyNode","booleanLiteral","propName","propValue","arrayExpression","filter","has","set","slug"],"sources":["../../src/processors/styled.ts"],"sourcesContent":["import type {\n CallExpression,\n Expression,\n ObjectExpression,\n SourceLocation,\n StringLiteral,\n} from '@babel/types';\n\nimport type { ProcessorParams } from '@linaria/core/processors/BaseProcessor';\nimport BaseProcessor from '@linaria/core/processors/BaseProcessor';\nimport type {\n Rules,\n WrappedNode,\n ValueCache,\n} from '@linaria/core/processors/types';\nimport { ValueType } from '@linaria/core/processors/types';\nimport hasMeta from '@linaria/core/processors/utils/hasMeta';\n\nconst isNotNull = <T>(x: T | null): x is T => x !== null;\n\nexport interface IProps {\n atomic?: boolean;\n class?: string;\n name: string;\n vars?: Record<string, Expression[]>;\n}\n\nconst singleQuotedStringLiteral = (value: string): StringLiteral => ({\n type: 'StringLiteral',\n value,\n extra: {\n rawValue: value,\n raw: `'${value}'`,\n },\n});\n\nexport default class StyledProcessor extends BaseProcessor {\n public component: WrappedNode;\n\n #variableIdx = 0;\n\n #variablesCache = new Map<string, string>();\n\n constructor(...args: ProcessorParams) {\n super(...args);\n\n let component: WrappedNode | undefined;\n const [type, value, ...rest] = this.params[0] ?? [];\n if (type === 'call' && rest.length === 0) {\n if (value.kind === ValueType.FUNCTION) {\n component = 'FunctionalComponent';\n } else {\n component = {\n node: value.ex,\n source: value.source,\n };\n\n this.dependencies.push(value);\n }\n }\n\n if (type === 'member') {\n component = value;\n }\n\n if (!component || this.params.length > 1) {\n throw new Error('Invalid usage of `styled` tag');\n }\n\n this.component = component;\n }\n\n public override addInterpolation(\n node: Expression,\n source: string,\n unit = ''\n ): string {\n const id = this.getVariableId(source + unit);\n\n this.interpolations.push({\n id,\n node,\n source,\n unit,\n });\n\n return id;\n }\n\n public override doEvaltimeReplacement(): void {\n this.replacer(this.value, false);\n }\n\n public override doRuntimeReplacement(): void {\n const t = this.astService;\n\n const props = this.getProps();\n\n this.replacer(\n t.callExpression(this.tagExpression, [this.getTagComponentProps(props)]),\n true\n );\n }\n\n public override extractRules(\n valueCache: ValueCache,\n cssText: string,\n loc?: SourceLocation | null\n ): Rules {\n const rules: Rules = {};\n\n let selector = `.${this.className}`;\n\n // If `styled` wraps another component and not a primitive,\n // get its class name to create a more specific selector\n // it'll ensure that styles are overridden properly\n let value =\n typeof this.component === 'string'\n ? null\n : valueCache.get(this.component.node.name);\n while (hasMeta(value)) {\n selector += `.${value.__linaria.className}`;\n value = value.__linaria.extends;\n }\n\n rules[selector] = {\n cssText,\n className: this.className,\n displayName: this.displayName,\n start: loc?.start ?? null,\n };\n\n return rules;\n }\n\n public override get asSelector(): string {\n return `.${this.className}`;\n }\n\n protected get tagExpressionArgument(): Expression {\n const t = this.astService;\n if (typeof this.component === 'string') {\n if (this.component === 'FunctionalComponent') {\n return t.arrowFunctionExpression([], t.blockStatement([]));\n }\n\n return singleQuotedStringLiteral(this.component);\n }\n\n return t.callExpression(t.identifier(this.component.node.name), []);\n }\n\n protected get tagExpression(): CallExpression {\n const t = this.astService;\n return t.callExpression(this.tagExp, [this.tagExpressionArgument]);\n }\n\n public override get value(): ObjectExpression {\n const t = this.astService;\n const extendsNode =\n typeof this.component === 'string' ? null : this.component.node.name;\n\n return t.objectExpression([\n t.objectProperty(\n t.stringLiteral('displayName'),\n t.stringLiteral(this.displayName)\n ),\n t.objectProperty(\n t.stringLiteral('__linaria'),\n t.objectExpression([\n t.objectProperty(\n t.stringLiteral('className'),\n t.stringLiteral(this.className)\n ),\n t.objectProperty(\n t.stringLiteral('extends'),\n extendsNode\n ? t.callExpression(t.identifier(extendsNode), [])\n : t.nullLiteral()\n ),\n ])\n ),\n ]);\n }\n\n protected getProps(): IProps {\n const propsObj: IProps = {\n name: this.displayName,\n class: this.className,\n };\n\n // If we found any interpolations, also pass them, so they can be applied\n if (this.interpolations.length) {\n propsObj.vars = {};\n this.interpolations.forEach(({ id, unit, node }) => {\n const items: Expression[] = [this.astService.callExpression(node, [])];\n\n if (unit) {\n items.push(this.astService.stringLiteral(unit));\n }\n\n propsObj.vars![id] = items;\n });\n }\n\n return propsObj;\n }\n\n protected getTagComponentProps(props: IProps): ObjectExpression {\n const t = this.astService;\n\n const propExpressions = Object.entries(props)\n .map(([key, value]: [key: string, value: IProps[keyof IProps]]) => {\n if (!value) {\n return null;\n }\n\n const keyNode = t.identifier(key);\n\n if (typeof value === 'string') {\n return t.objectProperty(keyNode, t.stringLiteral(value));\n }\n\n if (typeof value === 'boolean') {\n return t.objectProperty(keyNode, t.booleanLiteral(value));\n }\n\n const vars = Object.entries(value).map(([propName, propValue]) => {\n return t.objectProperty(\n t.stringLiteral(propName),\n t.arrayExpression(propValue)\n );\n });\n\n return t.objectProperty(keyNode, t.objectExpression(vars));\n })\n .filter(isNotNull);\n\n return t.objectExpression(propExpressions);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected getVariableId(value: string): string {\n if (!this.#variablesCache.has(value)) {\n // make the variable unique to this styled component\n // eslint-disable-next-line no-plusplus\n this.#variablesCache.set(value, `${this.slug}-${this.#variableIdx++}`);\n }\n\n return this.#variablesCache.get(value)!;\n }\n}\n"],"mappings":";;;;;;;AASA;;AAMA;;A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,cAAN,CAAqB0E,GAArB,CAAyBlF,KAAzB,EAAiC,GAAE,KAAKmF,IAAK,IAAG,KAAK,CAAC5E,WAAN,EAAoB,EAApE;IACD;;IAED,OAAO,KAAK,CAACC,cAAN,CAAqBuC,GAArB,CAAyB/C,KAAzB,CAAP;EACD;;AAtNwD"} |
+2
-2
| { | ||
| "name": "@linaria/react", | ||
| "description": "Blazing fast zero-runtime CSS in JS library", | ||
| "version": "3.0.0-beta.22", | ||
| "version": "4.0.0-beta.0", | ||
| "bugs": "https://github.com/callstack/linaria/issues", | ||
| "dependencies": { | ||
| "@emotion/is-prop-valid": "^0.8.8", | ||
| "@linaria/core": "^3.0.0-beta.22", | ||
| "@linaria/core": "^4.0.0-beta.0", | ||
| "ts-invariant": "^0.10.3" | ||
@@ -10,0 +10,0 @@ }, |
| import type { CallExpression, Expression, ObjectExpression, SourceLocation } from '@babel/types'; | ||
| import type { StyledMeta } from '@linaria/core'; | ||
| import type { ProcessorParams } from '@linaria/core/processors/BaseProcessor'; | ||
| import BaseProcessor from '@linaria/core/processors/BaseProcessor'; | ||
| import type { Rules, WrappedNode, IInterpolation, ValueCache } from '@linaria/core/processors/types'; | ||
| export declare function hasMeta(value: unknown): value is StyledMeta; | ||
| import type { Rules, WrappedNode, ValueCache } from '@linaria/core/processors/types'; | ||
| export interface IProps { | ||
@@ -14,14 +12,16 @@ atomic?: boolean; | ||
| export default class StyledProcessor extends BaseProcessor { | ||
| #private; | ||
| component: WrappedNode; | ||
| constructor(...args: ProcessorParams); | ||
| addInterpolation(node: Expression, source: string): string; | ||
| extractRules(valueCache: ValueCache, cssText: string, loc?: SourceLocation | null): [Rules, string]; | ||
| getRuntimeReplacement(classes: string, uniqInterpolations: IInterpolation[]): [Expression, boolean]; | ||
| addInterpolation(node: Expression, source: string, unit?: string): string; | ||
| doEvaltimeReplacement(): void; | ||
| doRuntimeReplacement(): void; | ||
| extractRules(valueCache: ValueCache, cssText: string, loc?: SourceLocation | null): Rules; | ||
| get asSelector(): string; | ||
| protected get tagExpressionArgument(): Expression; | ||
| protected get tagExpression(): CallExpression; | ||
| get valueSource(): string; | ||
| protected getProps(classes: string, uniqInterpolations: IInterpolation[]): IProps; | ||
| get value(): ObjectExpression; | ||
| protected getProps(): IProps; | ||
| protected getTagComponentProps(props: IProps): ObjectExpression; | ||
| protected getVariableId(value: string): string; | ||
| } |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
81885
1.35%593
-2.79%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
Updated