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
Showing
17 changed files
with
320 additions
and
14 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,11 @@ | ||
declare module '@babel/helper-module-imports' { | ||
import type { NodePath } from '@babel/traverse'; | ||
import type { Identifier } from '@babel/types'; | ||
|
||
function addNamed( | ||
path: NodePath, | ||
name: string, | ||
importedSource: string, | ||
opts?: { nameHint: string } | ||
): Identifier; | ||
} |
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,3 @@ | ||
const config = require('../../babel.config'); | ||
|
||
module.exports = config; |
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,55 @@ | ||
{ | ||
"name": "@linaria/griffel", | ||
"description": "Blazing fast zero-runtime CSS in JS library", | ||
"version": "3.0.0-beta.21", | ||
"bugs": "https://github.com/callstack/linaria/issues", | ||
"dependencies": { | ||
"@griffel/core": "^1.5.0", | ||
"@linaria/logger": "workspace:^", | ||
"@linaria/tags": "workspace:^", | ||
"@linaria/utils": "workspace:^", | ||
"ts-invariant": "^0.10.3" | ||
}, | ||
"devDependencies": { | ||
"@babel/types": "^7.18.9" | ||
}, | ||
"engines": { | ||
"node": "^12.16.0 || >=13.7.0" | ||
}, | ||
"files": [ | ||
"esm/", | ||
"lib/", | ||
"processors/", | ||
"types/" | ||
], | ||
"homepage": "https://github.com/callstack/linaria#readme", | ||
"keywords": [ | ||
"css", | ||
"css-in-js", | ||
"linaria", | ||
"react", | ||
"styled-components" | ||
], | ||
"license": "MIT", | ||
"linaria": { | ||
"tags": { | ||
"makeStyles": "./lib/processors/makeStyles.js" | ||
} | ||
}, | ||
"main": "lib/index.js", | ||
"module": "esm/index.js", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": "git@github.com:callstack/linaria.git", | ||
"scripts": { | ||
"build": "npm run build:lib && npm run build:esm && npm run build:declarations", | ||
"build:declarations": "tsc --emitDeclarationOnly --outDir types", | ||
"build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start", | ||
"build:lib": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start", | ||
"typecheck": "tsc --noEmit --composite false", | ||
"watch": "npm run build --watch" | ||
}, | ||
"sideEffects": false, | ||
"types": "types/index.d.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,5 @@ | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true, | ||
}); | ||
|
||
exports.default = require('../lib/processors/makeStyles').default; |
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 @@ | ||
export { default as makeStyles } from './makeStyles'; |
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 makeStyles<Slots extends string | number>( | ||
stylesBySlots: Record<Slots, unknown> | ||
): () => Record<Slots, string> { | ||
throw new Error('Cannot be called in runtime'); | ||
} |
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,107 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
import type { Expression } from '@babel/types'; | ||
import { resolveStyleRulesForSlots } from '@griffel/core'; | ||
import type { | ||
StylesBySlots, | ||
CSSClassesMapBySlot, | ||
CSSRulesByBucket, | ||
} from '@griffel/core/types'; | ||
|
||
import type { Rules, ValueCache, ProcessorParams } from '@linaria/tags'; | ||
import { BaseProcessor, isCallParam } from '@linaria/tags'; | ||
|
||
export default class MakeStylesProcessor extends BaseProcessor { | ||
#cssClassMap: CSSClassesMapBySlot<string> | undefined; | ||
|
||
#cssRulesByBucket: CSSRulesByBucket | undefined; | ||
|
||
readonly #slotsExpName: string; | ||
|
||
constructor(...args: ProcessorParams) { | ||
super(...args); | ||
|
||
const callParam = this.params.find(isCallParam); | ||
if (!callParam || this.params.length !== 1) { | ||
throw new Error('Invalid usage of `makeStyles` tag'); | ||
} | ||
|
||
this.#slotsExpName = callParam[1].ex.name; | ||
} | ||
|
||
public override addInterpolation(): string { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
public override get asSelector(): string { | ||
throw new Error('The result of makeStyles cannot be used as a selector.'); | ||
} | ||
|
||
public override build(valueCache: ValueCache) { | ||
const slots = valueCache.get(this.#slotsExpName) as StylesBySlots<string>; | ||
[this.#cssClassMap, this.#cssRulesByBucket] = | ||
resolveStyleRulesForSlots(slots); | ||
} | ||
|
||
public override doEvaltimeReplacement(): void { | ||
this.replacer(this.value, false); | ||
} | ||
|
||
public override doRuntimeReplacement(): void { | ||
if (!this.#cssClassMap || !this.#cssRulesByBucket) { | ||
throw new Error( | ||
'Styles are not extracted yet. Please call `build` first.' | ||
); | ||
} | ||
|
||
const t = this.astService; | ||
|
||
const importedStyles = t.addNamedImport('__styles', '@griffel/react'); | ||
|
||
const cssClassMap = t.objectExpression( | ||
Object.entries(this.#cssClassMap).map(([slot, classesMap]) => { | ||
return t.objectProperty( | ||
t.identifier(slot), | ||
t.objectExpression( | ||
Object.entries(classesMap).map(([className, classValue]) => | ||
t.objectProperty( | ||
t.identifier(className), | ||
Array.isArray(classValue) | ||
? t.arrayExpression(classValue.map((i) => t.stringLiteral(i))) | ||
: t.stringLiteral(classValue) | ||
) | ||
) | ||
) | ||
); | ||
}) | ||
); | ||
|
||
const cssRulesByBucket = t.objectExpression( | ||
Object.entries(this.#cssRulesByBucket).map(([bucket, rules]) => { | ||
return t.objectProperty( | ||
t.identifier(bucket), | ||
t.arrayExpression( | ||
rules.map((rule) => t.stringLiteral(rule as string)) | ||
) | ||
); | ||
}) | ||
); | ||
|
||
const stylesCall = t.callExpression(importedStyles, [ | ||
cssClassMap, | ||
cssRulesByBucket, | ||
]); | ||
this.replacer(stylesCall, true); | ||
} | ||
|
||
protected get tagExpression(): Expression { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
public override get value(): Expression { | ||
return this.astService.nullLiteral(); | ||
} | ||
|
||
extractRules(): Rules { | ||
throw new Error('Not implemented'); | ||
} | ||
} |
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 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { "paths": {}, "rootDir": "src/" }, | ||
"references": [{ "path": "../core" }, { "path": "../logger" }, { "path": "../react" }, { "path": "../utils" }] | ||
} |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './BaseProcessor'; | ||
export * from './types'; | ||
export { default as isSerializable } from './utils/isSerializable'; | ||
export * from './utils/params'; | ||
export * from './utils/types'; | ||
export { default as BaseProcessor } from './BaseProcessor'; | ||
export { default as hasMeta } from './utils/hasMeta'; |
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,10 @@ | ||
import type { Param, TemplateParam, CallParam, MemberParam } from '../types'; | ||
|
||
export const isCallParam = (param: Param): param is CallParam => | ||
param[0] === 'call'; | ||
|
||
export const isMemberParam = (param: Param): param is MemberParam => | ||
param[0] === 'member'; | ||
|
||
export const isTemplateParam = (param: Param): param is TemplateParam => | ||
param[0] === 'template'; |
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.