-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: remove unused theme-proxy functionality in makeStyles() #21274
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "refactor: remove unused theme-proxy functionality", | ||
"packageName": "@fluentui/babel-make-styles", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "refactor: remove unused theme-proxy functionality", | ||
"packageName": "@fluentui/make-styles", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ import { NodePath, types as t } from '@babel/core'; | |
import { Scope } from '@babel/traverse'; | ||
import * as template from '@babel/template'; | ||
import generator from '@babel/generator'; | ||
import { resolveProxyValues } from '@fluentui/make-styles'; | ||
import { Module, StrictOptions } from '@linaria/babel-preset'; | ||
|
||
import type { BabelPluginOptions } from '../types'; | ||
|
@@ -55,58 +54,24 @@ const expressionWrapperTpl = template.statement(` | |
}; | ||
`); | ||
|
||
/** | ||
* Functions, call & member expressions should be wrapped with IIFE to ensure that "theme" object will be passed | ||
* without collisions. | ||
* | ||
* @example | ||
* { | ||
* label: foo(), // call expression | ||
* header: typography.header, // could be an object or a function | ||
* } | ||
* | ||
* Outputs following template: | ||
* @example | ||
* wrap(() => typeof foo === 'function' ? foo(theme) : foo) | ||
*/ | ||
export const expressionTpl = template.expression( | ||
`%%wrapName%%(() => typeof %%expression%% === 'function' ? %%expression%%(%%themeVariableName%%) : %%expression%%)`, | ||
); | ||
export const expressionTpl = template.expression(`%%wrapName%%(() => %%expression%%)`); | ||
const exportsPrevalTpl = template.statement(`exports.${EVAL_EXPORT_NAME} = %%expressions%%`); | ||
|
||
/** | ||
* Creates a new program that includes required imports and wrappers to return resolved values. | ||
*/ | ||
function addPreval( | ||
path: NodePath<t.Program>, | ||
themeVariableName: string, | ||
lazyDeps: Array<t.Expression | t.SpreadElement>, | ||
): t.Program { | ||
// Constant __mkPreval with all dependencies | ||
function addPreval(path: NodePath<t.Program>, lazyDeps: Array<t.Expression | t.SpreadElement>): t.Program { | ||
// Constant for "__mkPreval" with all dependencies | ||
const wrapName = findFreeName(path.scope, '_wrap'); | ||
const proxyImportName = path.scope.generateUid('createCSSVariablesProxy'); | ||
|
||
const programNode = path.node; | ||
|
||
return t.program( | ||
// Temporary solution to solve "theme" dependency | ||
[ | ||
t.importDeclaration( | ||
[t.importSpecifier(t.identifier(proxyImportName), t.identifier('createCSSVariablesProxy'))], | ||
t.stringLiteral('@fluentui/make-styles'), | ||
), | ||
|
||
t.variableDeclaration('const', [ | ||
t.variableDeclarator(t.identifier(themeVariableName), t.callExpression(t.identifier(proxyImportName), [])), | ||
]), | ||
Comment on lines
-94
to
-101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we don't have builtin theming (#21239) we don't need to import these anymore. |
||
|
||
...programNode.body, | ||
|
||
expressionWrapperTpl({ wrapName }), | ||
exportsPrevalTpl({ | ||
expressions: t.arrayExpression( | ||
lazyDeps.map(expression => expressionTpl({ expression, wrapName, themeVariableName })), | ||
), | ||
expressions: t.arrayExpression(lazyDeps.map(expression => expressionTpl({ expression, wrapName }))), | ||
}), | ||
], | ||
programNode.directives, | ||
|
@@ -124,8 +89,6 @@ export function evaluatePathsInVM( | |
nodePaths: NodePath<t.Expression | t.SpreadElement>[], | ||
pluginOptions: Required<BabelPluginOptions>, | ||
): void { | ||
const themeVariableName = program.scope.generateUid('theme'); | ||
|
||
const pathsToEvaluate = nodePaths.map(nodePath => { | ||
// spreads ("...fooBar") can't be executed directly, so they are wrapped with an object ("{...fooBar}") | ||
if (nodePath.isSpreadElement()) { | ||
|
@@ -136,23 +99,14 @@ export function evaluatePathsInVM( | |
}); | ||
|
||
// Linaria also performs hoisting of identifiers, we don't need this as all makeStyles() calls should be top level | ||
const modifiedProgram = addPreval(program, themeVariableName, pathsToEvaluate); | ||
const modifiedProgram = addPreval(program, pathsToEvaluate); | ||
|
||
const { code } = generator(modifiedProgram); | ||
const results = evaluate(code, filename, pluginOptions); | ||
|
||
for (let i = 0; i < nodePaths.length; i++) { | ||
const nodePath = nodePaths[i]; | ||
|
||
// 👇 we should resolve proxy values (they are defined as functions) before creating AST from an object with styles | ||
const result = resolveProxyValues(results[i]); | ||
|
||
// 💡 spreads can't replace itself, we should replace it with with properties | ||
if (nodePath.isSpreadElement()) { | ||
nodePath.replaceWithMultiple((astify(result) as t.ObjectExpression).properties); | ||
continue; | ||
} | ||
Comment on lines
-150
to
-154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We evaluate objects now (#21144), we can't meet there a spread. |
||
|
||
nodePath.replaceWith(astify(result)); | ||
nodePath.replaceWith(astify(results[i])); | ||
} | ||
} |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expressions cannot be functions anymore (#21239).