Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybarbet committed Feb 7, 2025
1 parent c2e2151 commit 99ceb62
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 52 deletions.
2 changes: 1 addition & 1 deletion plugin/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions plugin/index.js.map

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions plugin/src/exotic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,25 @@ export function handleExoticImport(path: NodePath<ImportDeclaration>, state: Uni
}

if (isImportDefaultSpecifier(specifier)) {
// TODO
const newImport = importDeclaration(
[importDefaultSpecifier(identifier(specifier.local.name))],
stringLiteral(state.opts.isLocal
? state.file.opts.filename?.split('react-native-unistyles').at(0)?.concat(`react-native-unistyles/components/native/${rule.mapTo}`) ?? ''
stringLiteral(state.opts.isLocal && state.file.opts.filename
? state.file.opts.filename.split('react-native-unistyles').at(0)?.concat(`react-native-unistyles/components/native/${rule.mapTo}`) ?? ''
: `react-native-unistyles/components/native/${rule.mapTo}`
)
) as any // TODO: Remove this cast
) as any

path.replaceWith(newImport)
} else {
// TODO
const newImport = importDeclaration(
[importSpecifier(identifier(rule.mapTo), identifier(rule.mapTo))],
stringLiteral(state.opts.isLocal
? state.file.opts.filename?.split('react-native-unistyles').at(0)?.concat(`react-native-unistyles/components/native/${rule.mapTo}`) ?? ''
stringLiteral(state.opts.isLocal && state.file.opts.filename
? state.file.opts.filename.split('react-native-unistyles').at(0)?.concat(`react-native-unistyles/components/native/${rule.mapTo}`) ?? ''
: `react-native-unistyles/components/native/${rule.mapTo}`
)
) as any // TODO: Remove this cast
) as any

path.node.specifiers = specifiers.filter(s => s !== specifier)

Expand Down
86 changes: 43 additions & 43 deletions plugin/src/stylesheet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UnistylesPluginPass } from './types'
import type { NodePath } from '@babel/core'
import { arrayExpression, identifier, isArrowFunctionExpression, isBlockStatement, isFunctionExpression, isIdentifier, isIfStatement, isMemberExpression, isObjectExpression, isObjectPattern, isObjectProperty, isReturnStatement, numericLiteral, objectExpression, type ObjectProperty, objectProperty, spreadElement, type CallExpression } from '@babel/types'
import { arrayExpression, identifier, isArrowFunctionExpression, isBlockStatement, isFunctionExpression, isIdentifier, isIfStatement, isMemberExpression, isObjectExpression, isObjectPattern, isObjectProperty, isReturnStatement, numericLiteral, objectExpression, type ObjectProperty, objectProperty, spreadElement, type CallExpression, type ArrowFunctionExpression, type FunctionExpression, type RestElement, MemberExpression } from '@babel/types'

const UnistyleDependency = {
Theme: 0,
Expand Down Expand Up @@ -34,38 +34,42 @@ export function stringToUniqueId(str: string) {
}

export function isUnistylesStyleSheet(path: NodePath<CallExpression>, state: UnistylesPluginPass) {
const callee = path.get('callee')
const { callee } = path.node

if (isMemberExpression(callee) && isIdentifier(callee.property)) {
return (
callee.property.name === 'create' &&
isIdentifier(callee.object) &&
callee.object.name === state.file.styleSheetLocalName
)
}

return (
isMemberExpression(callee.node) &&
callee.node.property.name === 'create' &&
isIdentifier(callee.node.object) &&
callee.node.object.name === state.file.styleSheetLocalName
)
return false
}

export function isKindOfStyleSheet(path: NodePath<CallExpression>, state: UnistylesPluginPass) {
if (!state.file.forceProcessing && !state.file.hasUnistylesImport) {
return false
}

const callee = path.get('callee')
const { callee } = path.node // Directly access callee

return (
isMemberExpression(callee.node) &&
callee.node.property.name === 'create' &&
isIdentifier(callee.node.object)
isMemberExpression(callee) &&
isIdentifier(callee.property) &&
callee.property.name === 'create' &&
isIdentifier(callee.object)
)
}

export function addStyleSheetTag(path, state: UnistylesPluginPass) {
const callee = path.get('callee')
const uniqueId = stringToUniqueId(state.filename.replace(state.cwd, '')) + ++state.file.tagNumber
export function addStyleSheetTag(path: NodePath<CallExpression>, state: UnistylesPluginPass) {
const str = state.filename?.replace(state.cwd, '') ?? ''
const uniqueId = stringToUniqueId(str) + ++state.file.tagNumber

callee.container.arguments.push(numericLiteral(uniqueId))
path.node.arguments.push(numericLiteral(uniqueId))
}

const getProperty = (property) => {
function getProperty(property: any) {
if (!property) {
return undefined
}
Expand Down Expand Up @@ -145,25 +149,22 @@ export function getStylesDependenciesFromObject(path: NodePath<CallExpression>)

return variants.reduce<Record<string, string[]>>((acc, { key, label }) => {
if (acc[key]) {
return {
...acc,
[key]: [
...acc[key],
label
]
}
}
acc[key] = [
...acc[key],
label
]

return {
...acc,
[key]: [label]
return acc;
}

acc[key] = [label]

return acc;
}, {})
}

export function getStylesDependenciesFromFunction(path: NodePath<CallExpression>) {
// TODO
const funcPath = path.get('arguments.0') as unknown as NodePath<Node>
const funcPath = path.get('arguments.0') as unknown as NodePath<ArrowFunctionExpression | FunctionExpression>

if (!funcPath) {
return
Expand Down Expand Up @@ -311,7 +312,7 @@ export function getStylesDependenciesFromFunction(path: NodePath<CallExpression>
})
})

const detectedStylesWithRt = new Set()
const detectedStylesWithRt = new Set<{ label: 'rt'; key: string }>()
const localRtName = isIdentifier(rtParam)
? rtParam.name
: undefined
Expand Down Expand Up @@ -407,23 +408,22 @@ export function getStylesDependenciesFromFunction(path: NodePath<CallExpression>
.concat(variants)
.reduce((acc, { key, label }) => {
if (acc[key]) {
return {
...acc,
[key]: [
...acc[key],
label
]
}
}

return {
...acc,
[key]: [label]
acc[key] = [
...acc[key],
label
]

return acc
}

acc[key] = [label]

return acc
}, [])
}

export function toUnistylesDependency(dependency) {
export function toUnistylesDependency(dependency: string) {
switch (dependency) {
case 'theme': {
return UnistyleDependency.Theme
Expand Down
1 change: 1 addition & 0 deletions plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ interface UnistylesState {
export interface UnistylesPluginPass {
file: BabelFile & UnistylesState,
opts: UnistylesPluginOptions,
cwd: string,
filename: string | undefined,
reactNativeImports: Record<string, string>
}

0 comments on commit 99ceb62

Please sign in to comment.