Skip to content

Commit

Permalink
Add theme to compiled next project
Browse files Browse the repository at this point in the history
  • Loading branch information
dabbott committed Dec 6, 2023
1 parent 49eca0e commit 2083ae2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/noya-compiler/src/__tests__/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { loadDesignSystem } from 'noya-module-loader';
import { tailwindColors } from 'noya-tailwind';
import { uuid } from 'noya-utils';
import { generateThemeTransformer } from '../compileTheme';
import { generateThemeFile } from '../compileTheme';
import { print } from '../print';

const HeroComponent = Model.component({
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('theme', () => {
},
};

const transformer = generateThemeTransformer(ChakraDesignSystem, { theme });
const transformer = generateThemeFile(ChakraDesignSystem, { theme });

expect(transformer).toMatchSnapshot();
});
Expand Down
10 changes: 5 additions & 5 deletions packages/noya-compiler/src/compileTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import { format, print } from './print';
* });
* ```
*/
export function generateThemeTransformer(
export function generateThemeFile(
DesignSystem: DesignSystemDefinition,
themeValue: any,
): string {
if (!DesignSystem.themeTransformer) return '';

const Components = buildNamespaceMap(DesignSystem.imports);
const namespaceMap = buildNamespaceMap(DesignSystem.imports);
const imports: { name: string; source: string }[] = [];

function convert(transformer: any): ts.Expression {
Expand All @@ -51,7 +51,7 @@ export function generateThemeTransformer(
);
}
case 'function': {
const func = Components.get(transformer.value);
const func = namespaceMap.get(transformer.value);

if (!func) return ts.factory.createNull();

Expand Down Expand Up @@ -112,7 +112,7 @@ export function generateThemeTransformer(
return format([print(importsAst), print(themeExportAst)].join('\n\n'));
}

export function accessPath(data: any, path: string): any {
function accessPath(data: any, path: string): any {
if (path === '.') return data;
const parts = path.split('.');
let currentValue = data;
Expand All @@ -122,7 +122,7 @@ export function accessPath(data: any, path: string): any {
return currentValue;
}

export function isTransformer(value: unknown): value is Transformer {
function isTransformer(value: unknown): value is Transformer {
return (
typeof value === 'object' &&
value !== null &&
Expand Down
24 changes: 22 additions & 2 deletions packages/noya-compiler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DesignSystemDefinition,
Theme,
component,
} from '@noya-design-system/protocol';
import { DS } from 'noya-api';
Expand All @@ -9,6 +10,7 @@ import {
renderResolvedNode,
} from 'noya-component';
import { loadDesignSystem } from 'noya-module-loader';
import { tailwindColors } from 'noya-tailwind';
import { groupBy, unique } from 'noya-utils';
import React, { ReactNode, isValidElement } from 'react';
import { defineTree, flat } from 'tree-visit';
Expand All @@ -23,6 +25,7 @@ import {
isSimpleElement,
simpleElement,
} from './common';
import { generateThemeFile } from './compileTheme';
import { LayoutNode, LayoutNodeAttributes } from './parseComponentLayout';
import { removeEmptyStyles } from './passes/removeEmptyStyles';
import { removeUndefinedStyles } from './passes/removeUndefinedStyles';
Expand Down Expand Up @@ -413,7 +416,9 @@ export function compile(configuration: CompilerConfiguration) {
{},
DesignSystem.createElement(
DesignSystem.components[component.id.Provider],
{},
{
theme: createPassthrough(ts.factory.createIdentifier('theme')),
},
createPassthrough(
ts.factory.createJsxExpression(
undefined,
Expand Down Expand Up @@ -453,12 +458,26 @@ export function compile(configuration: CompilerConfiguration) {

const layoutSource = [
"'use client'",
"import React from 'react'\n" + print(layoutImports),
[
"import React from 'react'",
print(layoutImports),
'import { theme } from "./theme"',
].join('\n'),
print(layoutComponentFunc),
]
.map(clean)
.join('\n');

const theme: Theme = {
colorMode: configuration.ds.config.colorMode ?? 'light',
colors: {
primary: (tailwindColors as any)[configuration.ds.config.colors.primary],
neutral: tailwindColors.slate,
},
};

const themeFile = generateThemeFile(DesignSystem, { theme });

const files = {
...Object.fromEntries(
componentPageItems.map(({ name, source }) => [
Expand All @@ -469,6 +488,7 @@ export function compile(configuration: CompilerConfiguration) {
source,
]),
),
'src/app/components/theme.ts': themeFile,
'src/app/components/layout.tsx': layoutSource,
'package.json': JSON.stringify(
{
Expand Down

0 comments on commit 2083ae2

Please sign in to comment.