Skip to content

v4: Support loading bundled versions of some first-party plugins #1240

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 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/tailwindcss-language-server/src/util/v4/design-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Resolver } from '../../resolver'
import { pathToFileURL } from '../../utils'
import type { Jiti } from 'jiti/lib/types'
import { assets } from './assets'
import { plugins } from './plugins'

const HAS_V4_IMPORT = /@import\s*(?:'tailwindcss'|"tailwindcss")/
const HAS_V4_THEME = /@theme\s*\{/
Expand Down Expand Up @@ -58,6 +59,28 @@ function createLoader<T>({

return await jiti.import(url.href, { default: true })
} catch (err) {
// If the request was to load a first-party plugin and we can't resolve it
// locally, then fall back to the built-in plugins that we know about.
if (resourceType === 'plugin' && id in plugins) {
console.log('Loading bundled plugin for: ', id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you keep these logs on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. We send these to the "Tailwind CSS IntelliSense" output panel in VSCode. It's useful for triaging issues.

return await plugins[id]()
}

// This checks for an error thrown by enhanced-resolve
if (err && typeof err.details === 'string') {
let details: string = err.details
let pattern = /^resolve '([^']+)'/
let match = details.match(pattern)
if (match) {
let [_, importee] = match
if (importee in plugins) {
console.log(
`[error] Cannot load '${id}' plugins inside configs or plugins is not currently supported`,
)
}
}
}

return onError(id, err, resourceType)
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/tailwindcss-language-server/src/util/v4/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const plugins = {
'@tailwindcss/forms': () => import('@tailwindcss/forms').then((m) => m.default),
'@tailwindcss/aspect-ratio': () => import('@tailwindcss/aspect-ratio').then((m) => m.default),
'@tailwindcss/typography': () => import('@tailwindcss/typography').then((m) => m.default),
}
39 changes: 39 additions & 0 deletions packages/tailwindcss-language-server/tests/env/v4.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,45 @@ defineTest({
},
})

defineTest({
name: 'v4, no npm, bundled plugins',
fs: {
'app.css': css`
@import 'tailwindcss';
@plugin "@tailwindcss/aspect-ratio";
@plugin "@tailwindcss/forms";
@plugin "@tailwindcss/typography";
`,
},

// Note this test MUST run in spawn mode because Vitest hooks into import,
// require, etc… already and we need to test that any hooks are working
// without outside interference.
prepare: async ({ root }) => ({ client: await createClient({ root }) }),

handle: async ({ client }) => {
let doc = await client.open({
lang: 'html',
text: '<div class="prose-slate form-select aspect-w-2"></div>',
})

// <div class="prose-slate form-select aspect-w-2"></div>
// ^
let hover = await doc.hover({ line: 0, character: 13 })
expect(hover).not.toEqual(null)

// <div class="prose-slate form-select aspect-w-2"></div>
// ^
hover = await doc.hover({ line: 0, character: 25 })
expect(hover).not.toEqual(null)

// <div class="prose-slate form-select aspect-w-2"></div>
// ^
hover = await doc.hover({ line: 0, character: 37 })
expect(hover).not.toEqual(null)
},
})

defineTest({
/**
* Plugins and configs that import stuff from the `tailwindcss` package do
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Prerelease

- Nothing yet!
- v4: Support loading bundled versions of some first-party plugins ([#1240](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1240))

# 0.14.8

Expand Down