Skip to content

Commit 0d9ddac

Browse files
committed
fix: vite plugin css resolution issue
1 parent 69ad7cc commit 0d9ddac

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

integrations/vite/resolvers.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,70 @@ import {
1010
test,
1111
ts,
1212
txt,
13+
yaml,
1314
} from '../utils'
1415

16+
const browserCssPluginFixture = {
17+
'package.json': json`
18+
{
19+
"type": "module",
20+
"dependencies": {
21+
"@tailwindcss/vite": "workspace:^",
22+
"plugin-browser-css": "workspace:*",
23+
"tailwindcss": "workspace:^"
24+
},
25+
"devDependencies": {
26+
"vite": "^8"
27+
}
28+
}
29+
`,
30+
'pnpm-workspace.yaml': yaml`
31+
packages:
32+
- packages/*
33+
`,
34+
'packages/plugin-browser-css/package.json': json`
35+
{
36+
"name": "plugin-browser-css",
37+
"version": "1.0.0",
38+
"type": "module",
39+
"main": "./index.js",
40+
"module": "./index.js",
41+
"browser": "./browser.css"
42+
}
43+
`,
44+
'packages/plugin-browser-css/index.js': js`
45+
export default function ({ addUtilities }) {
46+
addUtilities({ '.browser-css-plugin': { 'border-bottom': '1px solid green' } })
47+
}
48+
`,
49+
'packages/plugin-browser-css/browser.css': css`
50+
.should-not-be-loaded-as-a-plugin {
51+
display: none;
52+
}
53+
`,
54+
'vite.config.ts': ts`
55+
import tailwindcss from '@tailwindcss/vite'
56+
import { defineConfig } from 'vite'
57+
58+
export default defineConfig({
59+
build: { cssMinify: false },
60+
plugins: [tailwindcss()],
61+
})
62+
`,
63+
'index.html': html`
64+
<head>
65+
<link rel="stylesheet" href="./src/index.css" />
66+
</head>
67+
<body>
68+
<div class="browser-css-plugin">Hello, world!</div>
69+
</body>
70+
`,
71+
'src/index.css': css`
72+
@import 'tailwindcss';
73+
@plugin 'plugin-browser-css';
74+
`,
75+
}
76+
1577
test(
1678
'resolves tsconfig paths in production build',
1779
{
@@ -288,6 +350,45 @@ test(
288350
},
289351
)
290352

353+
test(
354+
'resolves package plugins to JS entries in production build when browser points to CSS',
355+
{
356+
fs: browserCssPluginFixture,
357+
},
358+
async ({ fs, exec, expect }) => {
359+
await exec('pnpm vite build')
360+
361+
let files = await fs.glob('dist/**/*.css')
362+
expect(files).toHaveLength(1)
363+
let [filename] = files[0]
364+
365+
await fs.expectFileToContain(filename, [candidate`browser-css-plugin`])
366+
},
367+
)
368+
369+
test(
370+
'resolves package plugins to JS entries in dev mode when browser points to CSS',
371+
{
372+
fs: browserCssPluginFixture,
373+
},
374+
async ({ spawn, expect }) => {
375+
let process = await spawn('pnpm vite dev')
376+
await process.onStdout((m) => m.includes('ready in'))
377+
378+
let url = ''
379+
await process.onStdout((m) => {
380+
let match = /Local:\s*(http.*)\//.exec(m)
381+
if (match) url = match[1]
382+
return Boolean(url)
383+
})
384+
385+
await retryAssertion(async () => {
386+
let styles = await fetchStyles(url, '/index.html')
387+
expect(styles).toContain(candidate`browser-css-plugin`)
388+
})
389+
},
390+
)
391+
291392
describe.each(['postcss', 'lightningcss'])('%s', (transformer) => {
292393
test(
293394
'resolves aliases in production build',

packages/@tailwindcss-vite/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ const DEBUG = env.DEBUG
2525
const SPECIAL_QUERY_RE = /[?&](?:worker|sharedworker|raw|url)\b/
2626
const COMMON_JS_PROXY_RE = /\?commonjs-proxy/
2727
const INLINE_STYLE_ID_RE = /[?&]index=\d+\.css$/
28+
const JS_EXTENSIONS = new Set([
29+
'js',
30+
'cjs',
31+
'mjs',
32+
'ts',
33+
'cts',
34+
'mts',
35+
'jsx',
36+
'tsx',
37+
'json',
38+
'node',
39+
])
2840

2941
export type PluginOptions = {
3042
/**
@@ -75,6 +87,7 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
7587
if (!resolved) return
7688
if (resolved === id) return
7789
if (!path.isAbsolute(resolved)) return
90+
if (!isPotentialJsRootFile(resolved)) return
7891
return resolved
7992
}
8093
} else {
@@ -131,6 +144,7 @@ export default function tailwindcss(opts: PluginOptions = {}): Plugin[] {
131144
if (!resolved) return
132145
if (resolved === id) return
133146
if (!path.isAbsolute(resolved)) return
147+
if (!isPotentialJsRootFile(resolved)) return
134148
return resolved
135149
}
136150
}
@@ -359,6 +373,10 @@ function isPotentialCssRootFile(id: string) {
359373
return isCssFile
360374
}
361375

376+
function isPotentialJsRootFile(id: string) {
377+
return JS_EXTENSIONS.has(getExtension(id))
378+
}
379+
362380
function idToPath(id: string) {
363381
return path.resolve(id.replace(/\?.*$/, ''))
364382
}

0 commit comments

Comments
 (0)