2023-06-14 09:55:37 +00:00
|
|
|
import type { AstroIntegration } from 'astro';
|
2022-06-06 16:49:53 +00:00
|
|
|
import autoprefixerPlugin from 'autoprefixer';
|
2023-06-14 09:55:37 +00:00
|
|
|
import type { ResultPlugin } from 'postcss-load-config';
|
|
|
|
import tailwindPlugin from 'tailwindcss';
|
2023-01-23 14:49:20 +00:00
|
|
|
import type { CSSOptions, UserConfig } from 'vite';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2023-01-23 14:46:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:03:58 +00:00
|
|
|
async function getViteConfiguration(
|
2023-06-14 09:55:37 +00:00
|
|
|
tailwindConfigPath: string | undefined,
|
|
|
|
viteConfig: UserConfig
|
2023-04-05 16:03:58 +00:00
|
|
|
) {
|
2023-01-23 14:46:19 +00:00
|
|
|
// 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);
|
|
|
|
|
2023-06-14 09:55:37 +00:00
|
|
|
const postcssOptions = postcssConfigResult?.options ?? {};
|
|
|
|
const postcssPlugins = postcssConfigResult?.plugins?.slice() ?? [];
|
2023-01-23 14:46:19 +00:00
|
|
|
|
2023-09-13 14:49:22 +00:00
|
|
|
// @ts-expect-error Tailwind plugin types are wrong
|
2023-06-14 09:55:37 +00:00
|
|
|
postcssPlugins.push(tailwindPlugin(tailwindConfigPath) as ResultPlugin);
|
2023-01-27 15:14:19 +00:00
|
|
|
postcssPlugins.push(autoprefixerPlugin());
|
2023-06-14 09:55:37 +00:00
|
|
|
|
2023-01-05 08:02:35 +00:00
|
|
|
return {
|
|
|
|
css: {
|
|
|
|
postcss: {
|
2023-01-23 14:46:19 +00:00
|
|
|
options: postcssOptions,
|
2023-01-05 08:02:35 +00:00
|
|
|
plugins: postcssPlugins,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-15 15:40:42 +00:00
|
|
|
type TailwindOptions = {
|
|
|
|
/**
|
|
|
|
* Path to your tailwind config file
|
|
|
|
* @default 'tailwind.config.js'
|
|
|
|
*/
|
|
|
|
configFile?: string;
|
|
|
|
/**
|
|
|
|
* Apply Tailwind's base styles
|
|
|
|
* Disabling this is useful when further customization of Tailwind styles
|
|
|
|
* and directives is required. See {@link https://tailwindcss.com/docs/functions-and-directives#tailwind Tailwind's docs}
|
|
|
|
* for more details on directives and customization.
|
|
|
|
* @default true
|
|
|
|
*/
|
|
|
|
applyBaseStyles?: boolean;
|
|
|
|
};
|
2022-03-21 21:27:32 +00:00
|
|
|
|
2022-06-28 12:49:08 +00:00
|
|
|
export default function tailwindIntegration(options?: TailwindOptions): AstroIntegration {
|
2023-06-15 15:40:42 +00:00
|
|
|
const applyBaseStyles = options?.applyBaseStyles ?? true;
|
|
|
|
const customConfigPath = options?.configFile;
|
2022-03-18 22:35:45 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/tailwind',
|
|
|
|
hooks: {
|
2023-06-14 09:55:37 +00:00
|
|
|
'astro:config:setup': async ({ config, updateConfig, injectScript }) => {
|
2022-03-18 22:35:45 +00:00
|
|
|
// Inject the Tailwind postcss plugin
|
2023-01-23 14:46:19 +00:00
|
|
|
updateConfig({
|
2023-06-14 09:55:37 +00:00
|
|
|
vite: await getViteConfiguration(customConfigPath, config.vite),
|
2023-01-23 14:46:19 +00:00
|
|
|
});
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2022-04-01 13:45:43 +00:00
|
|
|
if (applyBaseStyles) {
|
|
|
|
// Inject the Tailwind base import
|
|
|
|
injectScript('page-ssr', `import '@astrojs/tailwind/base.css';`);
|
|
|
|
}
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|