This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(macro): faster Rem value transformation
No longer traverse the whole module just to transform Rem values.
- Loading branch information
Showing
5 changed files
with
65 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Packages | ||
import * as t from '@babel/types'; | ||
import traverse from '@babel/traverse'; | ||
import { NodePath } from '@babel/core'; | ||
|
||
// Ours | ||
import { importStyleSheet, importUtil } from './add-import'; | ||
|
||
export function injectStyles( | ||
program: NodePath<t.Program>, | ||
stylesVariableId: t.Identifier, | ||
styles: Record<string, Record<string, any>> | ||
) { | ||
// Use StyleSheet.create | ||
// => const styles = StyleSheet.create({...}) | ||
const stylesheet = t.variableDeclaration('const', [ | ||
t.variableDeclarator( | ||
stylesVariableId, | ||
t.callExpression( | ||
t.memberExpression( | ||
importStyleSheet(program), | ||
t.identifier('create') | ||
), | ||
[t.valueToNode(styles)] | ||
) | ||
), | ||
]); | ||
|
||
// Handle Rem values | ||
traverse(stylesheet, { | ||
noScope: true, | ||
StringLiteral: (path) => { | ||
// Replace Rem values with function calls | ||
if (path.node.value.match(/rem/)) { | ||
const remValue = parseFloat( | ||
path.node.value.replace(/rem|\(|\)/g, '') | ||
); | ||
|
||
path.replaceWith( | ||
t.callExpression(importUtil(program, 'rem'), [ | ||
t.numericLiteral(remValue), | ||
]) | ||
); | ||
} | ||
}, | ||
}); | ||
|
||
// Create stylesheet | ||
// => const styles = StyleSheet.create({...}) | ||
program.unshiftContainer('body' as any, stylesheet); | ||
} |
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