-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(tailwind): support v4 #13049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(tailwind): support v4 #13049
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5619b9d
feat: work on tailwind v4
florian-lefebvre 375f7f9
feat: refactor
florian-lefebvre dc7e433
test
florian-lefebvre 5c9f155
feat: write file
florian-lefebvre c78b6ef
feat: update fixture
florian-lefebvre 6af5580
feat: create css file
florian-lefebvre bc11481
feat: astro add
florian-lefebvre 83eea24
feat: example
florian-lefebvre 8e774c9
feat: todos
florian-lefebvre c397877
Update .changeset/four-chairs-exercise.md
florian-lefebvre 74f8d17
fix: typo
florian-lefebvre b56f2fb
fix: test
florian-lefebvre a2b75db
fix: test
florian-lefebvre 8d14fee
fix: color
florian-lefebvre 7ee87b9
Update .changeset/four-chairs-exercise.md
florian-lefebvre f2da6d8
fix: tests
florian-lefebvre bc0829b
Merge branch 'feat/tailwind-v4' of https://github.com/withastro/astro…
florian-lefebvre 2b523d9
chore: changeset
florian-lefebvre 1990a1d
Update curvy-penguins-act.md
florian-lefebvre 773187e
Merge branch 'main' into feat/tailwind-v4
florian-lefebvre 9de3c4f
Discard changes to packages/integrations/tailwind/base.css
florian-lefebvre 93b81be
Discard changes to packages/integrations/tailwind/package.json
florian-lefebvre c0a90e1
Discard changes to packages/integrations/tailwind/src/index.ts
florian-lefebvre 45e920d
Discard changes to packages/integrations/tailwind/test/basic.test.js
florian-lefebvre 5e18866
Discard changes to packages/integrations/tailwind/test/fixtures/basic…
florian-lefebvre 97a220a
Discard changes to packages/integrations/tailwind/test/fixtures/basic…
florian-lefebvre 079cb5a
feat: update changeset
florian-lefebvre 09f0542
chore: deps
florian-lefebvre f620e48
Update .changeset/curvy-penguins-act.md
florian-lefebvre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/tailwind': major | ||
--- | ||
|
||
aaa |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,50 @@ | ||
import { fileURLToPath } from 'node:url'; | ||
import type { AstroIntegration } from 'astro'; | ||
import autoprefixerPlugin from 'autoprefixer'; | ||
import tailwindPlugin from 'tailwindcss'; | ||
import type { CSSOptions, UserConfig } from 'vite'; | ||
|
||
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 { | ||
postcssConfigResult = null; | ||
} | ||
} | ||
return postcssConfigResult; | ||
} | ||
|
||
async function getViteConfiguration( | ||
tailwindConfigPath: string | undefined, | ||
nesting: boolean, | ||
root: string, | ||
postcssInlineOptions: CSSOptions['postcss'], | ||
): Promise<Partial<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(root, postcssInlineOptions); | ||
|
||
const postcssOptions = postcssConfigResult?.options ?? {}; | ||
const postcssPlugins = postcssConfigResult?.plugins?.slice() ?? []; | ||
|
||
if (nesting) { | ||
const tailwindcssNestingPlugin = (await import('tailwindcss/nesting/index.js')).default; | ||
postcssPlugins.push(tailwindcssNestingPlugin()); | ||
} | ||
|
||
postcssPlugins.push(tailwindPlugin(tailwindConfigPath)); | ||
postcssPlugins.push(autoprefixerPlugin()); | ||
|
||
return { | ||
css: { | ||
postcss: { | ||
...postcssOptions, | ||
plugins: postcssPlugins, | ||
}, | ||
}, | ||
}; | ||
} | ||
import tailwindcss from '@tailwindcss/vite'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { writeFileSync } from 'node:fs'; | ||
|
||
type TailwindOptions = { | ||
/** | ||
* Path to your tailwind config file | ||
* @default 'tailwind.config.mjs' | ||
* @deprecated It is recommended to use the `@config` directive, see https://tailwindcss.com/docs/upgrade-guide#using-a-javascript-config-file | ||
* @default './tailwind.config.mjs' | ||
*/ | ||
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. | ||
* @deprecated It is recommended to use a css file to configure Tailwind, see https://tailwindcss.com/docs/upgrade-guide#removed-tailwind-directives | ||
* @default true | ||
*/ | ||
applyBaseStyles?: boolean; | ||
/** | ||
* Add CSS nesting support using `tailwindcss/nesting`. See {@link https://tailwindcss.com/docs/using-with-preprocessors#nesting Tailwind's docs} | ||
* for how this works with `postcss-nesting` and `postcss-nested`. | ||
*/ | ||
nesting?: boolean; | ||
}; | ||
|
||
export default function tailwindIntegration(options?: TailwindOptions): AstroIntegration { | ||
const applyBaseStyles = options?.applyBaseStyles ?? true; | ||
const customConfigPath = options?.configFile; | ||
const nesting = options?.nesting ?? false; | ||
export default function tailwindIntegration({ | ||
applyBaseStyles = true, | ||
configFile, | ||
}: TailwindOptions = {}): AstroIntegration { | ||
if (applyBaseStyles && !configFile) { | ||
configFile = './tailwind.config.mjs'; | ||
} | ||
|
||
return { | ||
name: '@astrojs/tailwind', | ||
hooks: { | ||
'astro:config:setup': async ({ config, updateConfig, injectScript }) => { | ||
// Inject the Tailwind postcss plugin | ||
'astro:config:setup': async ({ config, updateConfig, injectScript, createCodegenDir }) => { | ||
updateConfig({ | ||
vite: await getViteConfiguration( | ||
customConfigPath, | ||
nesting, | ||
fileURLToPath(config.root), | ||
config.vite.css?.postcss, | ||
), | ||
vite: { plugins: [tailwindcss()] }, | ||
}); | ||
|
||
if (applyBaseStyles) { | ||
// Inject the Tailwind base import | ||
injectScript('page-ssr', `import '@astrojs/tailwind/base.css';`); | ||
const codegenDir = createCodegenDir(); | ||
let content = '@import "tailwindcss";'; | ||
if (configFile) { | ||
content += `\n@config ${JSON.stringify(fileURLToPath(new URL(configFile, config.root)))};`; | ||
} | ||
|
||
const url = new URL('tailwind.css', codegenDir); | ||
writeFileSync(url, content, 'utf-8'); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish I could use a virtual module but it didn't work with vite nor postcss |
||
injectScript('page-ssr', `import ${JSON.stringify(url)};`); | ||
} | ||
}, | ||
}, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
packages/integrations/tailwind/test/fixtures/basic/astro.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import { fileURLToPath } from 'node:url'; | ||
// @ts-check | ||
import tailwind from '@astrojs/tailwind'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
integrations: [ | ||
tailwind({ | ||
configFile: fileURLToPath(new URL('./tailwind.config.js', import.meta.url)), | ||
nesting: true | ||
configFile: "./tailwind.config.js" | ||
}), | ||
] | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.