Add Vite polyfills option

This commit is contained in:
Lubomír Blažek 2026-05-11 09:35:36 +02:00
parent 50daebded0
commit 21bdf8507b
3 changed files with 92 additions and 0 deletions

View file

@ -1560,6 +1560,64 @@ test(
},
)
test(
'polyfills option: disabled',
{
fs: {
'package.json': txt`
{
"type": "module",
"dependencies": {
"@tailwindcss/vite": "workspace:^",
"tailwindcss": "workspace:^"
},
"devDependencies": {
"vite": "^7"
}
}
`,
'vite.config.ts': ts`
import tailwindcss from '@tailwindcss/vite'
import { Polyfills } from 'tailwindcss'
import { defineConfig } from 'vite'
export default defineConfig({
build: { cssMinify: false },
plugins: [tailwindcss({ optimize: false, polyfills: Polyfills.None })],
})
`,
'index.html': html`
<head>
<link rel="stylesheet" href="./src/index.css" />
</head>
<body>
<div class="underline">Hello, world!</div>
</body>
`,
'src/index.css': css`
@import 'tailwindcss/utilities';
@property --no-inherit-value {
syntax: '*';
inherits: false;
initial-value: red;
}
`,
},
},
async ({ exec, expect, fs }) => {
await exec('pnpm vite build')
let files = await fs.glob('dist/**/*.css')
expect(files).toHaveLength(1)
let [filename] = files[0]
let content = await fs.read(filename)
expect(content).toContain('@property --no-inherit-value')
expect(content).not.toContain('@layer properties')
},
)
test(
`the plugin works when using the environment API`,
{

View file

@ -74,3 +74,22 @@ export default defineConfig({
],
})
```
## Controlling Tailwind polyfills
By default, Tailwind emits all supported CSS polyfills. You can customize this behavior using the `polyfills` option:
```js
import tailwindcss from '@tailwindcss/vite'
import { defineConfig } from 'vite'
import { Polyfills } from 'tailwindcss'
export default defineConfig({
plugins: [
tailwindcss({
// Disable all Tailwind polyfills
polyfills: Polyfills.None,
}),
],
})
```

View file

@ -5,6 +5,7 @@ import {
Instrumentation,
normalizePath,
optimize,
Polyfills,
toSourceMap,
} from '@tailwindcss/node'
import { clearRequireCache } from '@tailwindcss/node/require-cache'
@ -28,6 +29,13 @@ const INLINE_STYLE_ID_RE = /[?&]index=\d+\.css$/
const JS_EXTENSIONS_RE = /^\.[cm]?[jt]sx?$/
export type PluginOptions = {
/**
* Control CSS polyfills emitted by Tailwind.
*
* Defaults to `Polyfills.All`.
*/
polyfills?: Polyfills
/**
* Optimize and minify the output CSS.
*/
@ -185,6 +193,7 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
// Currently, Vite only supports CSS source maps in development and they
// are off by default. Check to see if we need them or not.
config?.css.devSourcemap ?? false,
opts.polyfills ?? Polyfills.All,
customCssResolver,
customJsResolver,
)
@ -517,6 +526,7 @@ class Root {
private base: string,
private enableSourceMaps: boolean,
private polyfills: Polyfills,
private customCssResolver: (id: string, base: string) => Promise<string | false | undefined>,
private customJsResolver: (id: string, base: string) => Promise<string | false | undefined>,
) {}
@ -566,12 +576,17 @@ class Root {
this.addBuildDependency(idToPath(inputPath))
// CSS Modules cannot safely receive the `@property` fallback polyfill
// because it emits global `*` rules, which Vite treats as non-pure.
DEBUG && I.start('Setup compiler')
let addBuildDependenciesPromises: Promise<void>[] = []
this.compiler = await compile(content, {
from: this.enableSourceMaps ? this.id : undefined,
base: inputBase,
shouldRewriteUrls: true,
polyfills: inputPath.endsWith('.module.css')
? this.polyfills & ~Polyfills.AtProperty
: this.polyfills,
onDependency: (path) => {
addWatchFile(path)
addBuildDependenciesPromises.push(this.addBuildDependency(path))