Skip to content

Commit e46c009

Browse files
Support postcss-import
1 parent 8aa1825 commit e46c009

File tree

3 files changed

+32
-54
lines changed

3 files changed

+32
-54
lines changed

packages/tailwindcss/src/at-import.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ export async function substituteAtImports(
1212
) {
1313
let promises: Promise<void>[] = []
1414

15-
walk(ast, (node, { replaceWith, context: { reference } }) => {
15+
walk(ast, (node, { replaceWith }) => {
1616
if (node.kind === 'at-rule' && node.name === '@import') {
1717
let parsed = parseImportParams(ValueParser.parse(node.params))
1818
if (parsed === null) return
1919

2020
let { uri, layer, media, supports } = parsed
21-
reference ||= parsed.reference ?? false
2221

2322
// Skip importing data or remote URIs
2423
if (uri.startsWith('data:')) return
@@ -40,22 +39,14 @@ export async function substituteAtImports(
4039

4140
let loaded = await loadStylesheet(uri, base)
4241
let ast = CSS.parse(loaded.content)
42+
await substituteAtImports(ast, loaded.base, loadStylesheet, recurseCount + 1)
4343

44-
// if (reference) {
45-
// ast = stripStyleRules(ast)
46-
// }
47-
48-
ast = buildImportNodes(
44+
contextNode.nodes = buildImportNodes(
4945
[context({ base: loaded.base }, ast)],
5046
layer,
5147
media,
5248
supports,
53-
!!reference,
5449
)
55-
56-
await substituteAtImports(ast, loaded.base, loadStylesheet, recurseCount + 1)
57-
58-
contextNode.nodes = ast
5950
})(),
6051
)
6152

@@ -79,7 +70,6 @@ export function parseImportParams(params: ValueParser.ValueAstNode[]) {
7970
let layer: string | null = null
8071
let media: string | null = null
8172
let supports: string | null = null
82-
let reference: true | null = null
8373

8474
for (let i = 0; i < params.length; i++) {
8575
let node = params[i]
@@ -129,26 +119,20 @@ export function parseImportParams(params: ValueParser.ValueAstNode[]) {
129119
continue
130120
}
131121

132-
if (node.kind === 'word' && node.value.toLocaleLowerCase() === 'reference') {
133-
reference = true
134-
continue
135-
}
136-
137122
media = ValueParser.toCss(params.slice(i))
138123
break
139124
}
140125

141126
if (!uri) return null
142127

143-
return { uri, layer, media, supports, reference }
128+
return { uri, layer, media, supports }
144129
}
145130

146131
function buildImportNodes(
147132
importedAst: AstNode[],
148133
layer: string | null,
149134
media: string | null,
150135
supports: string | null,
151-
reference: boolean | null,
152136
): AstNode[] {
153137
let root = importedAst
154138

@@ -164,9 +148,5 @@ function buildImportNodes(
164148
root = [atRule('@supports', supports[0] === '(' ? supports : `(${supports})`, root)]
165149
}
166150

167-
if (reference !== null) {
168-
root = [context({ reference }, root)]
169-
}
170-
171151
return root
172152
}

packages/tailwindcss/src/index.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3138,23 +3138,23 @@ describe('`@import "…" reference`', () => {
31383138
`)
31393139
})
31403140

3141-
test.only('removes styles when the import resolver was handled outside of Tailwind CSS', async () => {
3141+
test('removes styles when the import resolver was handled outside of Tailwind CSS', async () => {
31423142
await expect(
31433143
compileCss(
31443144
`
31453145
@media reference {
3146-
@media print {
3147-
.foo {
3148-
color: red;
3146+
@layer theme {
3147+
@theme {
3148+
--breakpoint-md: 48rem;
31493149
}
3150-
@utility foo {
3150+
.foo {
31513151
color: red;
31523152
}
3153-
@theme {
3154-
--breakpoint-md: 768px;
3155-
}
3156-
@variant hocus (&:hover, &:focus);
31573153
}
3154+
@utility foo {
3155+
color: red;
3156+
}
3157+
@variant hocus (&:hover, &:focus);
31583158
}
31593159
31603160
.bar {
@@ -3164,7 +3164,9 @@ describe('`@import "…" reference`', () => {
31643164
[],
31653165
),
31663166
).resolves.toMatchInlineSnapshot(`
3167-
"@media (width >= 768px) {
3167+
"@layer theme;
3168+
3169+
@media (width >= 48rem) {
31683170
.bar:hover, .bar:focus {
31693171
color: red;
31703172
}

packages/tailwindcss/src/index.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -339,32 +339,34 @@ async function parseCss(
339339

340340
// Handle `@import "…" reference`
341341
else if (param === 'reference') {
342-
let newAst = []
343-
for (let node of ast) {
344-
if (node.kind !== 'at-rule') {
345-
continue
342+
walk(node.nodes, (child, { replaceWith }) => {
343+
if (child.kind !== 'at-rule') {
344+
replaceWith([])
345+
return WalkAction.Skip
346346
}
347-
switch (node.name) {
347+
switch (child.name) {
348348
case '@theme': {
349-
let themeParams = segment(node.params, ' ')
349+
let themeParams = segment(child.params, ' ')
350350
if (!themeParams.includes('reference')) {
351-
node.params += ' reference'
351+
child.params = (child.params === '' ? '' : ' ') + 'reference'
352352
}
353-
newAst.push(node)
354-
continue
353+
return WalkAction.Skip
355354
}
356355
case '@import':
357356
case '@config':
358357
case '@plugin':
359358
case '@variant':
360359
case '@utility': {
361-
newAst.push(node)
362-
continue
360+
return WalkAction.Skip
363361
}
364-
}
365-
}
366362

367-
node.nodes = [contextNode({ reference: true }, newAst)]
363+
// Other at-rules, like `@media`, `@supports`, or `@layer` should
364+
// be recursively traversed as these might be inserted by the
365+
// `@import` resolution.
366+
}
367+
})
368+
console.dir(node.nodes, { depth: null })
369+
node.nodes = [contextNode({ reference: true }, node.nodes)]
368370
}
369371

370372
//
@@ -604,9 +606,3 @@ export default function postcssPluginWarning() {
604606
`It looks like you're trying to use \`tailwindcss\` directly as a PostCSS plugin. The PostCSS plugin has moved to a separate package, so to continue using Tailwind CSS with PostCSS you'll need to install \`@tailwindcss/postcss\` and update your PostCSS configuration.`,
605607
)
606608
}
607-
608-
function stripStyleRules(ast: AstNode[]) {
609-
let newAst = []
610-
611-
return newAst
612-
}

0 commit comments

Comments
 (0)