2022-10-12 10:36:33 -05:00
|
|
|
import load, { resolve } from '@proload/core';
|
2022-03-18 17:35:45 -05:00
|
|
|
import type { AstroIntegration } from 'astro';
|
2022-06-06 11:49:53 -05:00
|
|
|
import autoprefixerPlugin from 'autoprefixer';
|
2022-10-12 10:36:33 -05:00
|
|
|
import fs from 'fs/promises';
|
2022-03-18 17:35:45 -05:00
|
|
|
import path from 'path';
|
2022-06-29 16:54:40 -05:00
|
|
|
import tailwindPlugin, { Config as TailwindConfig } from 'tailwindcss';
|
2022-03-21 16:27:32 -05:00
|
|
|
import resolveConfig from 'tailwindcss/resolveConfig.js';
|
2022-06-06 11:49:53 -05:00
|
|
|
import { fileURLToPath } from 'url';
|
2022-03-18 17:35:45 -05:00
|
|
|
|
2022-03-21 16:27:32 -05:00
|
|
|
function getDefaultTailwindConfig(srcUrl: URL): TailwindConfig {
|
|
|
|
return resolveConfig({
|
2022-03-18 17:35:45 -05:00
|
|
|
theme: {
|
|
|
|
extend: {},
|
|
|
|
},
|
|
|
|
plugins: [],
|
|
|
|
content: [path.join(fileURLToPath(srcUrl), `**`, `*.{astro,html,js,jsx,svelte,ts,tsx,vue}`)],
|
2022-04-13 10:05:59 -05:00
|
|
|
presets: undefined, // enable Tailwind's default preset
|
2022-06-29 16:54:40 -05:00
|
|
|
}) as TailwindConfig;
|
2022-03-21 16:27:32 -05:00
|
|
|
}
|
|
|
|
|
2022-10-12 10:36:33 -05:00
|
|
|
async function getUserConfig(root: URL, configPath?: string, isRestart = false) {
|
2022-04-02 13:29:59 -05:00
|
|
|
const resolvedRoot = fileURLToPath(root);
|
2022-03-21 16:27:32 -05:00
|
|
|
let userConfigPath: string | undefined;
|
|
|
|
|
|
|
|
if (configPath) {
|
|
|
|
const configPathWithLeadingSlash = /^\.*\//.test(configPath) ? configPath : `./${configPath}`;
|
2022-04-02 13:29:59 -05:00
|
|
|
userConfigPath = fileURLToPath(new URL(configPathWithLeadingSlash, root));
|
2022-03-21 16:27:32 -05:00
|
|
|
}
|
|
|
|
|
2022-10-12 10:36:33 -05:00
|
|
|
if (isRestart) {
|
|
|
|
// Hack: Write config to temporary file at project root
|
|
|
|
// This invalidates and reloads file contents when using ESM imports or "resolve"
|
|
|
|
const resolvedConfigPath = (await resolve('tailwind', {
|
|
|
|
mustExist: false,
|
|
|
|
cwd: resolvedRoot,
|
|
|
|
filePath: userConfigPath,
|
|
|
|
})) as string;
|
|
|
|
|
|
|
|
const { dir, base } = path.parse(resolvedConfigPath);
|
|
|
|
const tempConfigPath = path.join(dir, `.temp.${Date.now()}.${base}`);
|
|
|
|
await fs.copyFile(resolvedConfigPath, tempConfigPath);
|
|
|
|
|
2022-11-01 09:50:53 -05:00
|
|
|
let result: load.Config<Record<any, any>> | undefined;
|
2022-10-12 10:36:33 -05:00
|
|
|
try {
|
2022-11-01 09:50:53 -05:00
|
|
|
result = await load('tailwind', {
|
|
|
|
mustExist: false,
|
|
|
|
cwd: resolvedRoot,
|
|
|
|
filePath: tempConfigPath,
|
|
|
|
});
|
|
|
|
|
2022-10-12 10:36:33 -05:00
|
|
|
await fs.unlink(tempConfigPath);
|
|
|
|
} catch {
|
|
|
|
/** file already removed */
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...result,
|
|
|
|
filePath: resolvedConfigPath,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return await load('tailwind', {
|
|
|
|
mustExist: false,
|
|
|
|
cwd: resolvedRoot,
|
|
|
|
filePath: userConfigPath,
|
|
|
|
});
|
|
|
|
}
|
2022-03-18 17:35:45 -05:00
|
|
|
}
|
|
|
|
|
2022-03-21 16:27:32 -05:00
|
|
|
type TailwindOptions =
|
|
|
|
| {
|
|
|
|
config?: {
|
|
|
|
/**
|
|
|
|
* Path to your tailwind config file
|
|
|
|
* @default 'tailwind.config.js'
|
|
|
|
*/
|
|
|
|
path?: string;
|
2022-04-01 08:45:43 -05: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 16:27:32 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
| undefined;
|
|
|
|
|
2022-06-28 07:49:08 -05:00
|
|
|
export default function tailwindIntegration(options?: TailwindOptions): AstroIntegration {
|
2022-04-01 08:45:43 -05:00
|
|
|
const applyBaseStyles = options?.config?.applyBaseStyles ?? true;
|
2022-03-21 16:27:32 -05:00
|
|
|
const customConfigPath = options?.config?.path;
|
2022-03-18 17:35:45 -05:00
|
|
|
return {
|
|
|
|
name: '@astrojs/tailwind',
|
|
|
|
hooks: {
|
2022-10-12 10:36:33 -05:00
|
|
|
'astro:config:setup': async ({ config, injectScript, addWatchFile, isRestart }) => {
|
2022-03-18 17:35:45 -05:00
|
|
|
// Inject the Tailwind postcss plugin
|
2022-10-12 10:36:33 -05:00
|
|
|
const userConfig = await getUserConfig(config.root, customConfigPath, isRestart);
|
2022-03-21 16:27:32 -05:00
|
|
|
|
|
|
|
if (customConfigPath && !userConfig?.value) {
|
|
|
|
throw new Error(
|
|
|
|
`Could not find a Tailwind config at ${JSON.stringify(
|
|
|
|
customConfigPath
|
|
|
|
)}. Does the file exist?`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-13 15:10:48 -05:00
|
|
|
if (addWatchFile && userConfig?.filePath) {
|
2022-10-12 10:36:33 -05:00
|
|
|
addWatchFile(userConfig.filePath);
|
|
|
|
}
|
|
|
|
|
2022-04-02 13:29:59 -05:00
|
|
|
const tailwindConfig: TailwindConfig =
|
|
|
|
(userConfig?.value as TailwindConfig) ?? getDefaultTailwindConfig(config.srcDir);
|
|
|
|
config.style.postcss.plugins.push(tailwindPlugin(tailwindConfig));
|
|
|
|
config.style.postcss.plugins.push(autoprefixerPlugin);
|
2022-03-18 17:35:45 -05:00
|
|
|
|
2022-04-01 08:45:43 -05:00
|
|
|
if (applyBaseStyles) {
|
|
|
|
// Inject the Tailwind base import
|
|
|
|
injectScript('page-ssr', `import '@astrojs/tailwind/base.css';`);
|
|
|
|
}
|
2022-03-18 17:35:45 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|