Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
fix: theme resolution build time
Browse files Browse the repository at this point in the history
  • Loading branch information
ankit-tailor committed Dec 4, 2023
1 parent a17c4fb commit 874d323
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 90 deletions.
137 changes: 60 additions & 77 deletions packages/babel-plugin-styled-resolver/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const traverse = require('@babel/traverse').default;
const types = require('@babel/types');
const { getConfig: buildAndGetConfig } = require('./buildConfig');

let CONFIG;
const isConfigExist = fs.existsSync(
`${process.cwd()}/.gluestack/config-${process.ppid}.js`
);

const {
convertStyledToStyledVerbosed,
convertSxToSxVerbosed,
Expand Down Expand Up @@ -39,6 +44,8 @@ const IMPORT_NAME = '@gluestack-style/react';
let configThemePath = [];
const BUILD_TIME_GLUESTACK_STYLESHEET = new StyleInjector();

let ConfigDefault = CONFIG;

const convertExpressionContainerToStaticObject = (
properties,
result = {},
Expand Down Expand Up @@ -551,28 +558,13 @@ function isImportFromAbsolutePath(
return false;
}

let CONFIG;
const isConfigExist = fs.existsSync(
`${process.cwd()}/.gluestack/config-${process.ppid}.js`
);

let ConfigDefault = CONFIG;

if (!isConfigExist) {
buildAndGetConfig()
.then((res) => {
CONFIG = res;
ConfigDefault = res;
})
.catch((err) => {
// eslint-disable-next-line no-console
console.log(err);
});
}

module.exports = function (b) {
module.exports = async function (b) {
const { types: t } = b;

if (!isConfigExist) {
ConfigDefault = await buildAndGetConfig();
}

function checkWebFileExists(filePath) {
if (filePath.includes('node_modules')) {
return false;
Expand Down Expand Up @@ -608,22 +600,6 @@ module.exports = function (b) {

return {
name: 'gluestack-babel-styled-resolver', // not required
// async pre(state) {
// let plugin;

// state.opts.plugins?.forEach((currentPlugin) => {
// if (currentPlugin.key === 'gluestack-babel-styled-resolver') {
// plugin = currentPlugin;
// }
// });

// const configPath = plugin?.options?.configPath;

// if (!isConfigExist) {
// const res = await buildAndGetConfig(configPath);
// ConfigDefault = res;
// }
// },
visitor: {
ImportDeclaration(importPath, state) {
currentFileName = state.file.opts.filename;
Expand All @@ -643,6 +619,8 @@ module.exports = function (b) {
platform = 'all';
}

// console.log('\n\nCONFIG >>>>>>', ConfigDefault, '\n\nCONFIG >>>>>>');

// `${process.cwd()}/.gluestack/config-${process.ppid}.js`

// if (
Expand Down Expand Up @@ -899,41 +877,44 @@ module.exports = function (b) {
*/
const extendedThemeComponents =
callExpressionPath.node.arguments[0].properties;
extendedThemeComponents.forEach((property) => {
if (
!t.isIdentifier(property.value) &&
!t.isTemplateLiteral(property.value) &&
!t.isConditionalExpression(property.value)
) {
const { themeNode, componentConfigNode } =
findThemeAndComponentConfig(property.value.properties);

let theme = themeNode
? getObjectFromAstNode(themeNode?.value)
: {};
let componentConfig = componentConfigNode
? getObjectFromAstNode(componentConfigNode?.value)
: {};

const resultParamsNode = getBuildTimeParams(
theme,
componentConfig,
{},
outputLibrary,
platform,
'extended'
);

if (resultParamsNode) {
property.value.properties.push(
t.objectProperty(
t.stringLiteral('BUILD_TIME_PARAMS'),
resultParamsNode
)
if (Array.isArray(extendedThemeComponents)) {
extendedThemeComponents.forEach((property) => {
if (
!t.isIdentifier(property.value) &&
!t.isTemplateLiteral(property.value) &&
!t.isConditionalExpression(property.value)
) {
const { themeNode, componentConfigNode } =
findThemeAndComponentConfig(property.value.properties);

let theme = themeNode
? getObjectFromAstNode(themeNode?.value)
: {};
let componentConfig = componentConfigNode
? getObjectFromAstNode(componentConfigNode?.value)
: {};

const resultParamsNode = getBuildTimeParams(
theme,
componentConfig,
{},
outputLibrary,
platform,
'extended'
);

if (resultParamsNode) {
property.value.properties.push(
t.objectProperty(
t.stringLiteral('BUILD_TIME_PARAMS'),
resultParamsNode
)
);
}
}
}
});
});
}
}
}
},
Expand Down Expand Up @@ -970,16 +951,18 @@ module.exports = function (b) {

const prefixedMediaQueries = {};

Object.keys(componentExtendedConfig?.tokens?.mediaQueries).forEach(
(key) => {
prefixedMediaQueries[key] = {
key: `@${key}`,
isMediaQuery: true,
};
}
);
if (componentExtendedConfig?.tokens?.mediaQueries) {
Object.keys(componentExtendedConfig?.tokens?.mediaQueries).forEach(
(key) => {
prefixedMediaQueries[key] = {
key: `@${key}`,
isMediaQuery: true,
};
}
);

Object.assign(reservedKeys, { ...prefixedMediaQueries });
Object.assign(reservedKeys, { ...prefixedMediaQueries });
}

const attr = jsxOpeningElementPath.node.attributes;
attr.forEach((attribute, index) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export const resolveComponentTheme = (config: any, componentTheme: any) => {
component?.componentConfig
);
} else {
GluestackStyleSheet.update(component.BUILD_TIME_PARAMS?.orderedResolved);
const toBeInjected = GluestackStyleSheet.update(
component.BUILD_TIME_PARAMS?.orderedResolved
);
component.BUILD_TIME_PARAMS.toBeInjected = toBeInjected;
resolvedTheme = component;
}

Expand Down
17 changes: 5 additions & 12 deletions packages/react/src/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,6 @@ export function verboseStyled<P, Variants, ComCon>(
// END BASE COLOR MODE RESOLUTION

let CONFIG: any = {};
let isInjected = false;
let plugins: any = [];
let reservedKeys = { ..._reservedKeys };

Expand Down Expand Up @@ -1269,18 +1268,12 @@ export function verboseStyled<P, Variants, ComCon>(
const sxStyleIds: any = React.useRef(BUILD_TIME_VERBOSED_STYLE_IDS);

if (BUILD_TIME_ORDERED_RESOLVED.length > 0 && !isClient.current) {
if (!isInjected) {
const toBeInjected = GluestackStyleSheet.update(
BUILD_TIME_ORDERED_RESOLVED
);
const toBeInjected = GluestackStyleSheet.update(
BUILD_TIME_ORDERED_RESOLVED
);

if (Platform.OS === 'web') {
GluestackStyleSheet.inject(
toBeInjected,
styledContext.inlineStyleMap
);
}
isInjected = true;
if (Platform.OS === 'web') {
GluestackStyleSheet.inject(toBeInjected, styledContext.inlineStyleMap);
}
sxStyleIds.current = BUILD_TIME_VERBOSED_STYLE_IDS;

Expand Down

0 comments on commit 874d323

Please sign in to comment.