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

fix(@astrojs/tailwind): manually load postcss config file #5908

Merged
merged 7 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 41 additions & 3 deletions packages/integrations/tailwind/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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';
import tailwindPlugin, { Config as TailwindConfig } from 'tailwindcss';
import resolveConfig from 'tailwindcss/resolveConfig.js';
import { fileURLToPath } from 'url';

type AddWatchFile = (path: string | URL) => void;
MoustaphaDev marked this conversation as resolved.
Show resolved Hide resolved

function getDefaultTailwindConfig(srcUrl: URL): TailwindConfig {
return resolveConfig({
theme: {
Expand Down Expand Up @@ -66,14 +69,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 +166,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
89 changes: 86 additions & 3 deletions pnpm-lock.yaml

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