Skip to content

Commit

Permalink
fix(@astrojs/tailwind): manually load postcss config file (#5908)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoustaphaDev authored and matthewp committed Jan 26, 2023
1 parent f5c1122 commit 2dd458d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-paws-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/tailwind': patch
---

Fix vite not picking up Postcss config files because of the tailwind integration
6 changes: 4 additions & 2 deletions packages/integrations/tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
"dependencies": {
"@proload/core": "^0.3.2",
"autoprefixer": "^10.4.7",
"postcss": "^8.4.14"
"postcss": "^8.4.14",
"postcss-load-config": "^4.0.1"
},
"devDependencies": {
"astro": "workspace:*",
"astro-scripts": "workspace:*",
"tailwindcss": "^3.0.24"
"tailwindcss": "^3.0.24",
"vite": "^4.0.3"
},
"peerDependencies": {
"tailwindcss": "^3.0.24",
Expand Down
42 changes: 39 additions & 3 deletions packages/integrations/tailwind/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import load, { resolve } from '@proload/core';
import type { AstroIntegration } from 'astro';
import type { CSSOptions, UserConfig } from 'vite';
import autoprefixerPlugin from 'autoprefixer';
import fs from 'fs/promises';
import path from 'path';
Expand Down Expand Up @@ -66,14 +67,46 @@ async function getUserConfig(root: URL, configPath?: string, isRestart = false)
}
}

function getViteConfiguration(isBuild: boolean, tailwindConfig: TailwindConfig) {
const postcssPlugins = [tailwindPlugin(tailwindConfig)];
async function getPostCssConfig(
root: UserConfig['root'],
postcssInlineOptions: CSSOptions['postcss']
) {
let postcssConfigResult;
// Check if postcss config is not inlined
if (!(typeof postcssInlineOptions === 'object' && postcssInlineOptions !== null)) {
let { default: postcssrc } = await import('postcss-load-config');
const searchPath = typeof postcssInlineOptions === 'string' ? postcssInlineOptions : root!;
try {
postcssConfigResult = await postcssrc({}, searchPath);
} catch (e) {
postcssConfigResult = null;
}
}
return postcssConfigResult;
}

async function getViteConfiguration(
isBuild: boolean,
tailwindConfig: TailwindConfig,
viteConfig: UserConfig
) {
// We need to manually load postcss config files because when inlining the tailwind and autoprefixer plugins,
// that causes vite to ignore postcss config files
const postcssConfigResult = await getPostCssConfig(viteConfig.root, viteConfig.css?.postcss);

const postcssOptions = (postcssConfigResult && postcssConfigResult.options) || {};

const postcssPlugins =
postcssConfigResult && postcssConfigResult.plugins ? postcssConfigResult.plugins.slice() : [];
postcssPlugins.push(tailwindPlugin(tailwindConfig));

if (isBuild) {
postcssPlugins.push(autoprefixerPlugin());
}
return {
css: {
postcss: {
options: postcssOptions,
plugins: postcssPlugins,
},
},
Expand Down Expand Up @@ -131,7 +164,10 @@ export default function tailwindIntegration(options?: TailwindOptions): AstroInt

const tailwindConfig =
(userConfig?.value as TailwindConfig) ?? getDefaultTailwindConfig(config.srcDir);
updateConfig({ vite: getViteConfiguration(command === 'build', tailwindConfig) });

updateConfig({
vite: await getViteConfiguration(command === 'build', tailwindConfig, config.vite),
});

if (applyBaseStyles) {
// Inject the Tailwind base import
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 2dd458d

Please sign in to comment.