From cc353d310b5d8854c97ed8ffa8c6c7bac3f27868 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 22 Aug 2024 11:29:13 -0400 Subject: [PATCH] Merge sources from config files --- .../src/compat/config/resolve-config.ts | 21 +++++++++++++++++++ .../tailwindcss/src/compat/config/types.ts | 19 +++++++++++++++++ packages/tailwindcss/src/index.ts | 10 ++++++++- packages/tailwindcss/src/plugin-api.ts | 5 ++++- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss/src/compat/config/resolve-config.ts b/packages/tailwindcss/src/compat/config/resolve-config.ts index f39269d44..fe6dd359a 100644 --- a/packages/tailwindcss/src/compat/config/resolve-config.ts +++ b/packages/tailwindcss/src/compat/config/resolve-config.ts @@ -4,6 +4,7 @@ import { createThemeFn } from '../../theme-fn' import { deepMerge, isPlainObject } from './deep-merge' import { type ResolvedConfig, + type ResolvedContentConfig, type ResolvedThemeValue, type ThemeValue, type UserConfig, @@ -18,6 +19,7 @@ interface ResolutionContext { design: DesignSystem configs: UserConfig[] plugins: PluginWithConfig[] + content: ResolvedContentConfig theme: Record extend: Record result: ResolvedConfig @@ -26,6 +28,9 @@ interface ResolutionContext { let minimal: ResolvedConfig = { theme: {}, plugins: [], + content: { + files: [], + }, } export function resolveConfig(design: DesignSystem, files: ConfigFile[]): ResolvedConfig { @@ -33,6 +38,9 @@ export function resolveConfig(design: DesignSystem, files: ConfigFile[]): Resolv design, configs: [], plugins: [], + content: { + files: [], + }, theme: {}, extend: {}, @@ -48,6 +56,7 @@ export function resolveConfig(design: DesignSystem, files: ConfigFile[]): Resolv mergeTheme(ctx) return { + content: ctx.content, theme: ctx.theme as ResolvedConfig['theme'], plugins: ctx.plugins, } @@ -125,6 +134,18 @@ function resolveInternal(ctx: ResolutionContext, { config, path }: ConfigFile): } } + // Merge in content paths from multiple configs + let content = config.content ?? [] + let files = Array.isArray(content) ? content : content.files + + for (let file of files) { + ctx.content.files.push( + typeof file === 'object' + ? file + : { base: path!, pattern: file } + ) + } + // Then apply the "user" config ctx.configs.push(config) } diff --git a/packages/tailwindcss/src/compat/config/types.ts b/packages/tailwindcss/src/compat/config/types.ts index 6d8489165..e089420f1 100644 --- a/packages/tailwindcss/src/compat/config/types.ts +++ b/packages/tailwindcss/src/compat/config/types.ts @@ -20,3 +20,22 @@ export interface ResolvedConfig { theme: Record> plugins: PluginWithConfig[] } + +// Content support +type ContentFile = string | { raw: string; extension?: string } + +export interface UserConfig { + content?: ContentFile[] | { files: ContentFile[] } +} + +type ResolvedContent = + | { base: string, pattern: string } + | { raw: string; extension?: string } + +export interface ResolvedContentConfig { + files: ResolvedContent[], +} + +export interface ResolvedConfig { + content: ResolvedContentConfig +} diff --git a/packages/tailwindcss/src/index.ts b/packages/tailwindcss/src/index.ts index 49eb2129d..47529a973 100644 --- a/packages/tailwindcss/src/index.ts +++ b/packages/tailwindcss/src/index.ts @@ -312,7 +312,7 @@ async function parseCss( let plugins = await Promise.all(pluginPaths.map(loadPlugin)) - let pluginApi = registerPlugins(plugins, designSystem, ast, configs) + let { pluginApi, resolvedConfig } = registerPlugins(plugins, designSystem, ast, configs) // Replace `@apply` rules with the actual utility classes. if (css.includes('@apply')) { @@ -338,6 +338,14 @@ async function parseCss( return WalkAction.Skip }) + for (let file of resolvedConfig.content.files) { + if ('raw' in file) { + throw new Error('raw content is not currently supported') + } + + globs.push(file.pattern) + } + return { designSystem, pluginApi, diff --git a/packages/tailwindcss/src/plugin-api.ts b/packages/tailwindcss/src/plugin-api.ts index 905d73790..3ad1d0356 100644 --- a/packages/tailwindcss/src/plugin-api.ts +++ b/packages/tailwindcss/src/plugin-api.ts @@ -362,5 +362,8 @@ export function registerPlugins( handler(pluginApi) } - return pluginApi + return { + pluginApi, + resolvedConfig, + } }