2022-03-18 22:35:45 +00:00
|
|
|
import type { AstroIntegration } from 'astro';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import path from 'path';
|
|
|
|
import tailwindPlugin from 'tailwindcss';
|
2022-03-21 21:27:32 +00:00
|
|
|
import type { TailwindConfig } from 'tailwindcss/tailwind-config';
|
|
|
|
import resolveConfig from 'tailwindcss/resolveConfig.js';
|
2022-03-18 22:35:45 +00:00
|
|
|
import autoprefixerPlugin from 'autoprefixer';
|
2022-03-21 21:27:32 +00:00
|
|
|
import load from '@proload/core';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2022-03-21 21:27:32 +00:00
|
|
|
function getDefaultTailwindConfig(srcUrl: URL): TailwindConfig {
|
|
|
|
return resolveConfig({
|
2022-03-18 22:35:45 +00:00
|
|
|
theme: {
|
|
|
|
extend: {},
|
|
|
|
},
|
|
|
|
plugins: [],
|
|
|
|
content: [path.join(fileURLToPath(srcUrl), `**`, `*.{astro,html,js,jsx,svelte,ts,tsx,vue}`)],
|
2022-03-21 21:27:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
2022-03-18 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 21:27:32 +00:00
|
|
|
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;
|
2022-03-18 22:35:45 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/tailwind',
|
|
|
|
hooks: {
|
2022-03-21 21:27:32 +00:00
|
|
|
'astro:config:setup': async ({ config, injectScript }) => {
|
2022-03-18 22:35:45 +00:00
|
|
|
// Inject the Tailwind postcss plugin
|
2022-03-21 21:27:32 +00:00
|
|
|
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 && userConfig?.value) {
|
|
|
|
// apply Astro config as a preset to user config
|
|
|
|
// this avoids merging or applying nested spread operators ourselves
|
|
|
|
tailwindConfig.presets = [getDefaultTailwindConfig(config.src), ...(tailwindConfig.presets || [])];
|
|
|
|
}
|
|
|
|
|
|
|
|
config.styleOptions.postcss.plugins.push(tailwindPlugin(tailwindConfig));
|
2022-03-18 22:35:45 +00:00
|
|
|
config.styleOptions.postcss.plugins.push(autoprefixerPlugin);
|
|
|
|
|
|
|
|
// Inject the Tailwind base import
|
|
|
|
injectScript('page-ssr', `import '@astrojs/tailwind/base.css';`);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|