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

This commit is contained in:
Happydev 2023-01-23 15:46:19 +01:00 committed by GitHub
parent 77ae7a597a
commit 9e57268f13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/tailwind': patch
---
Fix vite not picking up Postcss config files because of the tailwind integration

View file

@ -30,12 +30,14 @@
"dependencies": { "dependencies": {
"@proload/core": "^0.3.2", "@proload/core": "^0.3.2",
"autoprefixer": "^10.4.7", "autoprefixer": "^10.4.7",
"postcss": "^8.4.14" "postcss": "^8.4.14",
"postcss-load-config": "^4.0.1"
}, },
"devDependencies": { "devDependencies": {
"astro": "workspace:*", "astro": "workspace:*",
"astro-scripts": "workspace:*", "astro-scripts": "workspace:*",
"tailwindcss": "^3.0.24" "tailwindcss": "^3.0.24",
"vite": "^4.0.3"
}, },
"peerDependencies": { "peerDependencies": {
"tailwindcss": "^3.0.24", "tailwindcss": "^3.0.24",

View file

@ -1,5 +1,6 @@
import load, { resolve } from '@proload/core'; import load, { resolve } from '@proload/core';
import type { AstroIntegration } from 'astro'; import type { AstroIntegration } from 'astro';
import type { CSSOptions, UserConfig } from 'vite';
import autoprefixerPlugin from 'autoprefixer'; import autoprefixerPlugin from 'autoprefixer';
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; import path from 'path';
@ -66,14 +67,46 @@ async function getUserConfig(root: URL, configPath?: string, isRestart = false)
} }
} }
function getViteConfiguration(isBuild: boolean, tailwindConfig: TailwindConfig) { async function getPostCssConfig(
const postcssPlugins = [tailwindPlugin(tailwindConfig)]; 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) { if (isBuild) {
postcssPlugins.push(autoprefixerPlugin()); postcssPlugins.push(autoprefixerPlugin());
} }
return { return {
css: { css: {
postcss: { postcss: {
options: postcssOptions,
plugins: postcssPlugins, plugins: postcssPlugins,
}, },
}, },
@ -131,7 +164,10 @@ export default function tailwindIntegration(options?: TailwindOptions): AstroInt
const tailwindConfig = const tailwindConfig =
(userConfig?.value as TailwindConfig) ?? getDefaultTailwindConfig(config.srcDir); (userConfig?.value as TailwindConfig) ?? getDefaultTailwindConfig(config.srcDir);
updateConfig({ vite: getViteConfiguration(command === 'build', tailwindConfig) });
updateConfig({
vite: await getViteConfiguration(command === 'build', tailwindConfig, config.vite),
});
if (applyBaseStyles) { if (applyBaseStyles) {
// Inject the Tailwind base import // Inject the Tailwind base import

View file

@ -3261,15 +3261,19 @@ importers:
astro-scripts: workspace:* astro-scripts: workspace:*
autoprefixer: ^10.4.7 autoprefixer: ^10.4.7
postcss: ^8.4.14 postcss: ^8.4.14
postcss-load-config: ^4.0.1
tailwindcss: ^3.0.24 tailwindcss: ^3.0.24
vite: ^4.0.3
dependencies: dependencies:
'@proload/core': 0.3.3 '@proload/core': 0.3.3
autoprefixer: 10.4.13_postcss@8.4.21 autoprefixer: 10.4.13_postcss@8.4.21
postcss: 8.4.21 postcss: 8.4.21
postcss-load-config: 4.0.1_postcss@8.4.21
devDependencies: devDependencies:
astro: link:../../astro astro: link:../../astro
astro-scripts: link:../../../scripts astro-scripts: link:../../../scripts
tailwindcss: 3.2.4_postcss@8.4.21 tailwindcss: 3.2.4_postcss@8.4.21
vite: 4.0.4
packages/integrations/turbolinks: packages/integrations/turbolinks:
specifiers: specifiers:
@ -12802,6 +12806,23 @@ packages:
postcss: 8.4.21 postcss: 8.4.21
yaml: 1.10.2 yaml: 1.10.2
/postcss-load-config/4.0.1_postcss@8.4.21:
resolution: {integrity: sha512-vEJIc8RdiBRu3oRAI0ymerOn+7rPuMvRXslTvZUKZonDHFIczxztIyJ1urxM1x9JXEikvpWWTUUqal5j/8QgvA==}
engines: {node: '>= 14'}
peerDependencies:
postcss: '>=8.0.9'
ts-node: '>=9.0.0'
peerDependenciesMeta:
postcss:
optional: true
ts-node:
optional: true
dependencies:
lilconfig: 2.0.6
postcss: 8.4.21
yaml: 2.2.1
dev: false
/postcss-logical/5.0.4_postcss@8.4.21: /postcss-logical/5.0.4_postcss@8.4.21:
resolution: {integrity: sha512-RHXxplCeLh9VjinvMrZONq7im4wjWGlRJAqmAVLXyZaXwfDWP73/oq4NdIp+OZwhQUMj0zjqDfM5Fj7qby+B4g==} resolution: {integrity: sha512-RHXxplCeLh9VjinvMrZONq7im4wjWGlRJAqmAVLXyZaXwfDWP73/oq4NdIp+OZwhQUMj0zjqDfM5Fj7qby+B4g==}
engines: {node: ^12 || ^14 || >=16} engines: {node: ^12 || ^14 || >=16}
@ -15693,6 +15714,11 @@ packages:
resolution: {integrity: sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==} resolution: {integrity: sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==}
engines: {node: '>= 6'} engines: {node: '>= 6'}
/yaml/2.2.1:
resolution: {integrity: sha512-e0WHiYql7+9wr4cWMx3TVQrNwejKaEe7/rHNmQmqRjazfOP5W8PB6Jpebb5o6fIapbz9o9+2ipcaTM2ZwDI6lw==}
engines: {node: '>= 14'}
dev: false
/yargs-parser/18.1.3: /yargs-parser/18.1.3:
resolution: {integrity: sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==} resolution: {integrity: sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==}
engines: {node: '>=6'} engines: {node: '>=6'}