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-04-13 15:05:00 +00:00
|
|
|
presets: undefined // enable Tailwind's default preset
|
2022-03-21 21:27:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-02 18:29:59 +00:00
|
|
|
async function getUserConfig(root: URL, configPath?: string) {
|
|
|
|
const resolvedRoot = fileURLToPath(root);
|
2022-03-21 21:27:32 +00:00
|
|
|
let userConfigPath: string | undefined;
|
|
|
|
|
|
|
|
if (configPath) {
|
|
|
|
const configPathWithLeadingSlash = /^\.*\//.test(configPath) ? configPath : `./${configPath}`;
|
2022-04-02 18:29:59 +00:00
|
|
|
userConfigPath = fileURLToPath(new URL(configPathWithLeadingSlash, root));
|
2022-03-21 21:27:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 18:29:59 +00:00
|
|
|
return await load('tailwind', { mustExist: false, cwd: resolvedRoot, 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;
|
2022-04-01 13:45:43 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
};
|
|
|
|
}
|
|
|
|
| undefined;
|
|
|
|
|
|
|
|
export default function tailwindIntegration(options: TailwindOptions): AstroIntegration {
|
2022-04-01 13:45:43 +00:00
|
|
|
const applyBaseStyles = options?.config?.applyBaseStyles ?? true;
|
2022-03-21 21:27:32 +00:00
|
|
|
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-04-02 18:29:59 +00:00
|
|
|
const userConfig = await getUserConfig(config.root, customConfigPath);
|
2022-03-21 21:27:32 +00:00
|
|
|
|
|
|
|
if (customConfigPath && !userConfig?.value) {
|
|
|
|
throw new Error(
|
|
|
|
`Could not find a Tailwind config at ${JSON.stringify(
|
|
|
|
customConfigPath
|
|
|
|
)}. Does the file exist?`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-02 18:29:59 +00:00
|
|
|
const tailwindConfig: TailwindConfig =
|
|
|
|
(userConfig?.value as TailwindConfig) ?? getDefaultTailwindConfig(config.srcDir);
|
2022-03-21 21:27:32 +00:00
|
|
|
|
2022-04-02 18:29:59 +00:00
|
|
|
config.style.postcss.plugins.push(tailwindPlugin(tailwindConfig));
|
|
|
|
config.style.postcss.plugins.push(autoprefixerPlugin);
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|