forked from mui/pigment-css
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brijesh Bittu
committed
Dec 4, 2024
1 parent
56ace5a
commit 3cc6195
Showing
20 changed files
with
324 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true, | ||
}); | ||
|
||
exports.default = require('../build/processors/keyframes').KeyframesProcessor; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,161 @@ | ||
import type { Expression } from '@babel/types'; | ||
import type { | ||
CallParam, | ||
TemplateParam, | ||
Params, | ||
TailProcessorParams, | ||
ValueCache, | ||
} from '@wyw-in-js/processor-utils'; | ||
import { type Replacements, type Rules, ValueType } from '@wyw-in-js/shared'; | ||
import { BaseProcessor, validateParams } from '@wyw-in-js/processor-utils'; | ||
import { PigmentConfig, serializeStyles, processStyle } from '@pigment-css/shared'; | ||
|
||
export type Primitive = string | number | boolean | null | undefined; | ||
|
||
export type TemplateCallback = (params: Record<string, unknown> | undefined) => string | number; | ||
|
||
export class KeyframesProcessor extends BaseProcessor { | ||
callParam: CallParam | TemplateParam; | ||
|
||
constructor(params: Params, ...args: TailProcessorParams) { | ||
super([params[0]], ...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') { | ||
this.dependencies.push(callParams[1]); | ||
} 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 PigmentConfig; | ||
|
||
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(styleObjOrTagged: string[], ...args: Primitive[]) { | ||
const { styles } = serializeStyles( | ||
args.length > 0 ? [styleObjOrTagged, ...args] : [styleObjOrTagged], | ||
); | ||
const cssText = `@keyframes {${styles}}`; | ||
|
||
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([, callArg]: CallParam, values: ValueCache) { | ||
if (callArg.kind === ValueType.LAZY || callArg.kind === ValueType.FUNCTION) { | ||
const value = values.get(callArg.ex.name); | ||
const { themeArgs } = this.options as PigmentConfig; | ||
const styleObj = typeof value === 'function' ? value(themeArgs) : value; | ||
if (styleObj) { | ||
let count = 0; | ||
// Won't actually be used since keyframes call doesn't allow function values | ||
const getVariableName = () => { | ||
count += 1; | ||
return `${this.className}-${count}`; | ||
}; | ||
this.generateArtifacts(processStyle(styleObj, { getVariableName }).result); | ||
} | ||
} | ||
} | ||
|
||
doEvaltimeReplacement() { | ||
this.replacer(this.value, false); | ||
} | ||
|
||
doRuntimeReplacement() { | ||
this.doEvaltimeReplacement(); | ||
} | ||
|
||
get asSelector() { | ||
return this.className; | ||
} | ||
|
||
get value(): Expression { | ||
return this.astService.stringLiteral(this.className); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/pigment-css-core/tests/keyframes/fixtures/keyframes-theme.input.js
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,44 @@ | ||
import { keyframes } from '@pigment-css/core'; | ||
|
||
const green = 'green'; | ||
|
||
const gradientKeyframe = keyframes(({ theme }) => ({ | ||
'0%': { | ||
background: theme.palette.primary.main, | ||
}, | ||
'50%': { | ||
background: green, | ||
}, | ||
'100%': { | ||
background: theme.palette.secondary.main, | ||
}, | ||
})); | ||
|
||
const gradientKeyframe2 = keyframes` | ||
0% { | ||
background: ${({ theme }) => theme.palette.primary.main}; | ||
} | ||
50% { | ||
background: ${green}; | ||
} | ||
100% { | ||
background: ${({ theme }) => theme.palette.secondary.main}; | ||
} | ||
`; | ||
|
||
// simulate CssBaseline transpiled by Next.js | ||
export const styles = (theme) => ({ | ||
'0%': { | ||
background: theme.palette.primary.main, | ||
}, | ||
'50%': { | ||
background: green, | ||
}, | ||
'100%': { | ||
background: theme.palette.secondary.main, | ||
}, | ||
}); | ||
const gradientKeyframe3 = keyframes((_c = ({ theme }) => styles(theme))); | ||
var _c; |
4 changes: 4 additions & 0 deletions
4
packages/pigment-css-core/tests/keyframes/fixtures/keyframes-theme.output.css
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
packages/pigment-css-core/tests/keyframes/fixtures/keyframes-theme.output.js
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,18 @@ | ||
const green = 'green'; | ||
const gradientKeyframe = 'g5g95nd'; | ||
const gradientKeyframe2 = 'gt2fuab'; | ||
|
||
// simulate CssBaseline transpiled by Next.js | ||
export const styles = (theme) => ({ | ||
'0%': { | ||
background: theme.palette.primary.main, | ||
}, | ||
'50%': { | ||
background: green, | ||
}, | ||
'100%': { | ||
background: theme.palette.secondary.main, | ||
}, | ||
}); | ||
const gradientKeyframe3 = 'g1611ti3'; | ||
var _c; |
20 changes: 20 additions & 0 deletions
20
packages/pigment-css-core/tests/keyframes/fixtures/keyframes.input.js
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,20 @@ | ||
import { keyframes } from '@pigment-css/core'; | ||
|
||
const rotateKeyframe = keyframes({ | ||
from: { | ||
transform: 'rotate(360deg)', | ||
}, | ||
to: { | ||
transform: 'rotate(0deg)', | ||
}, | ||
}); | ||
|
||
const rotateKeyframe2 = keyframes` | ||
from { | ||
transform: rotate(360deg); | ||
} | ||
to { | ||
transform: rotate(0deg); | ||
} | ||
`; |
3 changes: 3 additions & 0 deletions
3
packages/pigment-css-core/tests/keyframes/fixtures/keyframes.output.css
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
packages/pigment-css-core/tests/keyframes/fixtures/keyframes.output.js
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,2 @@ | ||
const rotateKeyframe = 'rrtfw11'; | ||
const rotateKeyframe2 = 'rw2yxe7'; |
36 changes: 36 additions & 0 deletions
36
packages/pigment-css-core/tests/keyframes/keyframes.test.ts
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,36 @@ | ||
import path from 'node:path'; | ||
import { runTransformation, expect } from '../testUtils'; | ||
|
||
describe('Pigment CSS - keyframes', () => { | ||
it('basics', async () => { | ||
const { output, fixture } = await runTransformation( | ||
path.join(__dirname, 'fixtures/keyframes.input.js'), | ||
); | ||
|
||
expect(output.js).to.equal(fixture.js); | ||
expect(output.css).to.equal(fixture.css); | ||
}); | ||
|
||
it('should transform correctly with theme', async () => { | ||
const { output, fixture } = await runTransformation( | ||
path.join(__dirname, 'fixtures/keyframes-theme.input.js'), | ||
{ | ||
themeArgs: { | ||
theme: { | ||
palette: { | ||
primary: { | ||
main: 'red', | ||
}, | ||
secondary: { | ||
main: 'blue', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
expect(output.js).to.equal(fixture.js); | ||
expect(output.css).to.equal(fixture.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
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
Oops, something went wrong.