-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[zero] Add support for css import (#40541)
- Loading branch information
1 parent
bd2bd9a
commit 334f6c4
Showing
12 changed files
with
257 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true, | ||
}); | ||
|
||
exports.default = require('../processors/css').CssProcessor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { CSSObjectNoCallback } from './base'; | ||
import type { ThemeArgs } from './theme'; | ||
|
||
type Primitve = string | null | undefined | boolean | number; | ||
|
||
type CssArg = ((themeArgs: ThemeArgs) => CSSObjectNoCallback) | CSSObjectNoCallback; | ||
type CssFn = (themeArgs: ThemeArgs) => string | number; | ||
|
||
interface Css { | ||
/** | ||
* @returns {string} The generated css class name to be referenced. | ||
*/ | ||
(...arg: CssArg[]): string; | ||
/** | ||
* @returns {string} The generated css class name to be referenced. | ||
*/ | ||
(arg: TemplateStringsArray, ...templateArgs: (Primitve | CssFn)[]): string; | ||
} | ||
|
||
declare const css: Css; | ||
|
||
export default css; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default function css() { | ||
throw new Error( | ||
'MUI: You were trying to call "css" function without configuring your bundler. Make sure to install the bundler specific plugin and use it. @mui/zero-vite-plugin for Vite integration or @mui/zero-next-plugin for Next.js integration.', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import type { Expression } from '@babel/types'; | ||
import { validateParams } from '@linaria/tags'; | ||
import type { | ||
CallParam, | ||
TemplateParam, | ||
Params, | ||
TailProcessorParams, | ||
ValueCache, | ||
} from '@linaria/tags'; | ||
import type { Replacements, Rules } from '@linaria/utils'; | ||
import { ValueType } from '@linaria/utils'; | ||
import type { CSSInterpolation } from '@emotion/css'; | ||
import deepMerge from 'lodash.merge'; | ||
import BaseProcessor from './base-processor'; | ||
import type { IOptions } from './styled'; | ||
import { cache, css } from '../utils/emotion'; | ||
import type { Primitive, TemplateCallback } from './keyframes'; | ||
|
||
/** | ||
* @description Scope css class generation similar to css from emotion. | ||
* | ||
* @example | ||
* ```ts | ||
* import { css } from '@mui/zero-runtime'; | ||
* | ||
* const class1 = css(({theme}) => ({ | ||
* color: (theme.vars || theme).palette.primary.main, | ||
* })) | ||
* ``` | ||
* | ||
* <html className={class1} /> | ||
*/ | ||
export class CssProcessor extends BaseProcessor { | ||
callParam: CallParam | TemplateParam; | ||
|
||
constructor(params: Params, ...args: TailProcessorParams) { | ||
super(params, ...args); | ||
if (params.length < 2) { | ||
throw BaseProcessor.SKIP; | ||
} | ||
validateParams( | ||
params, | ||
['callee', ['call', 'template']], | ||
`Invalid use of ${this.tagSource.imported} tag.`, | ||
); | ||
|
||
const [, callParams] = params; | ||
if (callParams[0] === 'call') { | ||
const [, ...callArgs] = callParams; | ||
this.dependencies.push(...callArgs); | ||
} else if (callParams[0] === 'template') { | ||
callParams[1].forEach((element) => { | ||
if ('kind' in element && element.kind !== ValueType.CONST) { | ||
this.dependencies.push(element); | ||
} | ||
}); | ||
} | ||
this.callParam = callParams; | ||
} | ||
|
||
build(values: ValueCache) { | ||
if (this.artifacts.length > 0) { | ||
throw new Error(`MUI: "${this.tagSource.imported}" is already built`); | ||
} | ||
|
||
const [callType] = this.callParam; | ||
|
||
if (callType === 'template') { | ||
this.handleTemplate(this.callParam, values); | ||
} else { | ||
this.handleCall(this.callParam, values); | ||
} | ||
} | ||
|
||
private handleTemplate([, callArgs]: TemplateParam, values: ValueCache) { | ||
const templateStrs: string[] = []; | ||
// @ts-ignore @TODO - Fix this. No idea how to initialize a Tagged String array. | ||
templateStrs.raw = []; | ||
const templateExpressions: Primitive[] = []; | ||
const { themeArgs } = this.options as IOptions; | ||
|
||
callArgs.forEach((item) => { | ||
if ('kind' in item) { | ||
switch (item.kind) { | ||
case ValueType.FUNCTION: { | ||
const value = values.get(item.ex.name) as TemplateCallback; | ||
templateExpressions.push(value(themeArgs)); | ||
break; | ||
} | ||
case ValueType.CONST: | ||
templateExpressions.push(item.value); | ||
break; | ||
case ValueType.LAZY: { | ||
const evaluatedValue = values.get(item.ex.name); | ||
if (typeof evaluatedValue === 'function') { | ||
templateExpressions.push(evaluatedValue(themeArgs)); | ||
} else { | ||
templateExpressions.push(evaluatedValue as Primitive); | ||
} | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
} else if (item.type === 'TemplateElement') { | ||
templateStrs.push(item.value.cooked as string); | ||
// @ts-ignore | ||
templateStrs.raw.push(item.value.raw); | ||
} | ||
}); | ||
this.generateArtifacts(templateStrs, ...templateExpressions); | ||
} | ||
|
||
generateArtifacts(styleObjOrTaggged: CSSInterpolation | string[], ...args: Primitive[]) { | ||
const cssClassName = css(styleObjOrTaggged, ...args); | ||
const cssText = cache.registered[cssClassName] as string; | ||
|
||
const rules: Rules = { | ||
[this.asSelector]: { | ||
className: this.className, | ||
cssText, | ||
displayName: this.displayName, | ||
start: this.location?.start ?? null, | ||
}, | ||
}; | ||
const sourceMapReplacements: Replacements = [ | ||
{ | ||
length: cssText.length, | ||
original: { | ||
start: { | ||
column: this.location?.start.column ?? 0, | ||
line: this.location?.start.line ?? 0, | ||
}, | ||
end: { | ||
column: this.location?.end.column ?? 0, | ||
line: this.location?.end.line ?? 0, | ||
}, | ||
}, | ||
}, | ||
]; | ||
this.artifacts.push(['css', [rules, sourceMapReplacements]]); | ||
} | ||
|
||
private handleCall([, ...callArgs]: CallParam, values: ValueCache) { | ||
const mergedStyleObj: CSSInterpolation = {}; | ||
|
||
callArgs.forEach((callArg) => { | ||
let styleObj: CSSInterpolation; | ||
if (callArg.kind === ValueType.LAZY) { | ||
styleObj = values.get(callArg.ex.name) as CSSInterpolation; | ||
} else if (callArg.kind === ValueType.FUNCTION) { | ||
const { themeArgs } = this.options as IOptions; | ||
const value = values.get(callArg.ex.name) as ( | ||
args: Record<string, unknown> | undefined, | ||
) => CSSInterpolation; | ||
styleObj = value(themeArgs); | ||
} | ||
|
||
if (styleObj) { | ||
deepMerge(mergedStyleObj, styleObj); | ||
} | ||
}); | ||
if (Object.keys(mergedStyleObj).length > 0) { | ||
this.generateArtifacts(mergedStyleObj); | ||
} | ||
} | ||
|
||
doEvaltimeReplacement() { | ||
this.replacer(this.value, false); | ||
} | ||
|
||
doRuntimeReplacement() { | ||
this.doEvaltimeReplacement(); | ||
} | ||
|
||
get asSelector() { | ||
return `.${this.className}`; | ||
} | ||
|
||
get value(): Expression { | ||
return this.astService.stringLiteral(this.className); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.