Skip to content

Commit

Permalink
Add @reference "…" (#15565)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
philipp-spiess authored Jan 7, 2025
1 parent 02cfc45 commit d6c4e72
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

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

### Fixed

Expand Down
33 changes: 33 additions & 0 deletions packages/tailwindcss/src/at-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,36 @@ test('it crashes when inside a cycle', async () => {
`[Error: Exceeded maximum recursion depth while resolving \`foo.css\` in \`/root\`)]`,
)
})

test('resolves @reference as `@import "…" reference`', async () => {
let loadStylesheet = async (id: string, base: string) => {
expect(base).toBe('/root')
expect(id).toBe('./foo/bar.css')
return {
content: css`
@theme {
--color-red-500: red;
}
.foo {
color: red;
}
`,
base: '/root/foo',
}
}

await expect(
run(
css`
@reference './foo/bar.css';
@tailwind utilities;
`,
{ loadStylesheet, candidates: ['text-red-500'] },
),
).resolves.toMatchInlineSnapshot(`
".text-red-500 {
color: var(--color-red-500);
}
"
`)
})
5 changes: 4 additions & 1 deletion packages/tailwindcss/src/at-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ export async function substituteAtImports(
let promises: Promise<void>[] = []

walk(ast, (node, { replaceWith }) => {
if (node.kind === 'at-rule' && node.name === '@import') {
if (node.kind === 'at-rule' && (node.name === '@import' || node.name === '@reference')) {
let parsed = parseImportParams(ValueParser.parse(node.params))
if (parsed === null) return
if (node.name === '@reference') {
parsed.media = 'reference'
}

features |= Features.AtImport

Expand Down

0 comments on commit d6c4e72

Please sign in to comment.