Skip to content
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

Feat: support tailwind config files #2831

Merged
merged 13 commits into from
Mar 21, 2022
Merged
5 changes: 5 additions & 0 deletions .changeset/rude-panthers-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/tailwind': patch
---

Add support for tailwind config files. These can either be a standard `tailwind.config.js|cjs|mjs`, or a custom filename as specified in your integration config.
1 change: 1 addition & 0 deletions packages/integrations/tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"tailwindcss": "^3.0.23",
"autoprefixer": "^10.4.4",
"@proload/core": "^0.2.2",
"postcss": "^8.4.12"
},
"devDependencies": {
Expand Down
53 changes: 49 additions & 4 deletions packages/integrations/tailwind/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import type { AstroIntegration } from 'astro';
import { fileURLToPath } from 'url';
import path from 'path';
import tailwindPlugin from 'tailwindcss';
import type { TailwindConfig } from 'tailwindcss/tailwind-config';
import autoprefixerPlugin from 'autoprefixer';
import load from '@proload/core';

function getDefaultTailwindConfig(srcUrl: URL) {
function getDefaultTailwindConfig(srcUrl: URL): TailwindConfig {
return {
theme: {
extend: {},
Expand All @@ -14,13 +16,56 @@ function getDefaultTailwindConfig(srcUrl: URL) {
};
}
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved

export default function (): AstroIntegration {
async function getUserConfig(projectRoot: URL, configPath?: string) {
const resolvedProjectRoot = fileURLToPath(projectRoot);
let userConfigPath: string | undefined;

if (configPath) {
const configPathWithLeadingSlash = /^\.*\//.test(configPath) ? configPath : `./${configPath}`;
userConfigPath = fileURLToPath(new URL(configPathWithLeadingSlash, projectRoot));
}

return await load('tailwind', { mustExist: false, cwd: resolvedProjectRoot, filePath: userConfigPath });
}

type TailwindOptions =
| {
config?: {
/**
* Path to your tailwind config file
* @default 'tailwind.config.js'
*/
path?: string;
/**
* Apply Astro's default Tailwind config as a preset
* This is recommended to enable Tailwind across all components and Astro files
* @default true
*/
applyAstroPreset?: boolean;
};
}
| undefined;

export default function tailwindIntegration(options: TailwindOptions): AstroIntegration {
const applyAstroConfigPreset = options?.config?.applyAstroPreset ?? true;
const customConfigPath = options?.config?.path;
return {
name: '@astrojs/tailwind',
hooks: {
'astro:config:setup': ({ config, injectScript }) => {
'astro:config:setup': async ({ config, injectScript }) => {
// Inject the Tailwind postcss plugin
config.styleOptions.postcss.plugins.push(tailwindPlugin(getDefaultTailwindConfig(config.src)));
const userConfig = await getUserConfig(config.projectRoot, customConfigPath);

if (customConfigPath && !userConfig?.value) {
throw new Error(`Could not find a Tailwind config at ${JSON.stringify(customConfigPath)}. Does the file exist?`);
}

const tailwindConfig: TailwindConfig = (userConfig?.value as TailwindConfig) ?? getDefaultTailwindConfig(config.src);
if (applyAstroConfigPreset) {
tailwindConfig.presets = [...(tailwindConfig.presets || []), getDefaultTailwindConfig(config.src)];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I'm reading this correctly, tailwindConfig becomes:

{
  ...tailwindConfig,
  presets: [...tailwindConfig.presets, getDefaultTailwindConfig(config.src)]
}

does this make sense? It feels odd to put the entire default tailwind config here as a preset. If this is expected, a comment would help future readers not get confused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, just realized this was confusing! Added a clarifying comment here, and only applied the preset when a user config is present (otherwise we're applying the default config twice).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: discovered that adding a preset removed Tailwind's default config. Thought they would merge 😕 Used the resolveConfig function to merge those defaults back in! 19f29cb

}

config.styleOptions.postcss.plugins.push(tailwindPlugin(tailwindConfig));
config.styleOptions.postcss.plugins.push(autoprefixerPlugin);

// Inject the Tailwind base import
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.