Skip to content

Commit d6c4e72

Browse files
Add @reference "…" (#15565)
This PR adds a new `@reference "…"` API as an replacement for the previously added [`@import "…" reference`](#15228). The motivation for a distinct at rule is that `@import` is already handled outside of Tailwind in some scenarios (e.g. when using in combination with postcss-import, other pre-processors, or frameworks like Svelte). While our implementation of hijacking the `media` attribute _works in this cases_, it can cause annoying linter issues because tooling build around `@import` does not know about our behavior. To fix this, we've decided to move this mode into a separate at rule that is passed-through in the other tooling. Here's an example of how this would look like in Svelte: ```svelte <h1>Hello world!</h1> <style> @reference './theme.css'; h1 { color: var(--theme-color); } </style> ``` With this change, the Svelte linter would not be detecting unused CSS from the `theme.css` file as it would if we'd rely on `@import`.
1 parent 02cfc45 commit d6c4e72

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add `@tailwindcss/browser` package to run Tailwind CSS in the browser ([#15558](https://github.com/tailwindlabs/tailwindcss/pull/15558))
13+
- Add `@reference "…"` API as a replacement for the previous `@import "…" reference` option ([#15565](https://github.com/tailwindlabs/tailwindcss/pull/15565))
1314

1415
### Fixed
1516

packages/tailwindcss/src/at-import.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,36 @@ test('it crashes when inside a cycle', async () => {
553553
`[Error: Exceeded maximum recursion depth while resolving \`foo.css\` in \`/root\`)]`,
554554
)
555555
})
556+
557+
test('resolves @reference as `@import "…" reference`', async () => {
558+
let loadStylesheet = async (id: string, base: string) => {
559+
expect(base).toBe('/root')
560+
expect(id).toBe('./foo/bar.css')
561+
return {
562+
content: css`
563+
@theme {
564+
--color-red-500: red;
565+
}
566+
.foo {
567+
color: red;
568+
}
569+
`,
570+
base: '/root/foo',
571+
}
572+
}
573+
574+
await expect(
575+
run(
576+
css`
577+
@reference './foo/bar.css';
578+
@tailwind utilities;
579+
`,
580+
{ loadStylesheet, candidates: ['text-red-500'] },
581+
),
582+
).resolves.toMatchInlineSnapshot(`
583+
".text-red-500 {
584+
color: var(--color-red-500);
585+
}
586+
"
587+
`)
588+
})

packages/tailwindcss/src/at-import.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ export async function substituteAtImports(
1515
let promises: Promise<void>[] = []
1616

1717
walk(ast, (node, { replaceWith }) => {
18-
if (node.kind === 'at-rule' && node.name === '@import') {
18+
if (node.kind === 'at-rule' && (node.name === '@import' || node.name === '@reference')) {
1919
let parsed = parseImportParams(ValueParser.parse(node.params))
2020
if (parsed === null) return
21+
if (node.name === '@reference') {
22+
parsed.media = 'reference'
23+
}
2124

2225
features |= Features.AtImport
2326

0 commit comments

Comments
 (0)