Skip to content

Commit 108ee47

Browse files
committed
Ust postcss instance provided to plugin
If we import `postcss` there’s a chance that our code is using a different version than the one the plugin was provided to. This can cause checks like `instanceof` in external tools to fail.
1 parent 6d2bf5b commit 108ee47

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

packages/@tailwindcss-postcss/src/ast.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import postcss, {
2-
Input,
1+
import {
2+
type Postcss,
33
type ChildNode as PostCssChildNode,
44
type Container as PostCssContainerNode,
5+
type Input as PostCssInput,
56
type Root as PostCssRoot,
67
type Source as PostcssSource,
78
} from 'postcss'
@@ -12,9 +13,13 @@ import { DefaultMap } from '../../tailwindcss/src/utils/default-map'
1213

1314
const EXCLAMATION_MARK = 0x21
1415

15-
export function cssAstToPostCssAst(ast: AstNode[], source: PostcssSource | undefined): PostCssRoot {
16-
let inputMap = new DefaultMap<Source, Input>((src) => {
17-
return new Input(src.code, {
16+
export function cssAstToPostCssAst(
17+
ast: AstNode[],
18+
source: PostcssSource | undefined,
19+
postcss: Postcss,
20+
): PostCssRoot {
21+
let inputMap = new DefaultMap<Source, PostCssInput>((src) => {
22+
return new postcss.Input(src.code, {
1823
map: source?.input.map,
1924
from: src.file ?? undefined,
2025
})
@@ -126,7 +131,7 @@ export function cssAstToPostCssAst(ast: AstNode[], source: PostcssSource | undef
126131
}
127132

128133
export function postCssAstToCssAst(root: PostCssRoot): AstNode[] {
129-
let inputMap = new DefaultMap<Input, Source>((input) => ({
134+
let inputMap = new DefaultMap<PostCssInput, Source>((input) => ({
130135
file: input.file ?? input.id ?? null,
131136
code: input.css,
132137
}))

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { clearRequireCache } from '@tailwindcss/node/require-cache'
1111
import { Scanner } from '@tailwindcss/oxide'
1212
import fs from 'node:fs'
1313
import path, { relative } from 'node:path'
14-
import postcss, { type AcceptedPlugin, type PluginCreator } from 'postcss'
14+
import type { AcceptedPlugin, PluginCreator, Postcss, Root } from 'postcss'
1515
import { toCss, type AstNode } from '../../tailwindcss/src/ast'
1616
import { cssAstToPostCssAst, postCssAstToCssAst } from './ast'
1717
import fixRelativePathsPlugin from './postcss-fix-relative-paths'
@@ -23,13 +23,13 @@ interface CacheEntry {
2323
compiler: null | ReturnType<typeof compileAst>
2424
scanner: null | Scanner
2525
tailwindCssAst: AstNode[]
26-
cachedPostCssAst: postcss.Root
27-
optimizedPostCssAst: postcss.Root
26+
cachedPostCssAst: Root
27+
optimizedPostCssAst: Root
2828
fullRebuildPaths: string[]
2929
}
3030
const cache = new QuickLRU<string, CacheEntry>({ maxSize: 50 })
3131

32-
function getContextFromCache(inputFile: string, opts: PluginOptions): CacheEntry {
32+
function getContextFromCache(inputFile: string, opts: PluginOptions, postcss: Postcss): CacheEntry {
3333
let key = `${inputFile}:${opts.base ?? ''}:${JSON.stringify(opts.optimize)}`
3434
if (cache.has(key)) return cache.get(key)!
3535
let entry = {
@@ -69,7 +69,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
6969

7070
{
7171
postcssPlugin: 'tailwindcss',
72-
async Once(root, { result }) {
72+
async Once(root, { result, postcss }) {
7373
using I = new Instrumentation()
7474

7575
let inputFile = result.opts.from ?? ''
@@ -100,7 +100,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
100100
DEBUG && I.end('Quick bail check')
101101
}
102102

103-
let context = getContextFromCache(inputFile, opts)
103+
let context = getContextFromCache(inputFile, opts, postcss)
104104
let inputBasePath = path.dirname(path.resolve(inputFile))
105105

106106
// Whether this is the first build or not, if it is, then we can
@@ -296,7 +296,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
296296
} else {
297297
// Convert our AST to a PostCSS AST
298298
DEBUG && I.start('Transform Tailwind CSS AST into PostCSS AST')
299-
context.cachedPostCssAst = cssAstToPostCssAst(tailwindCssAst, root.source)
299+
context.cachedPostCssAst = cssAstToPostCssAst(tailwindCssAst, root.source, postcss)
300300
DEBUG && I.end('Transform Tailwind CSS AST into PostCSS AST')
301301
}
302302
}

0 commit comments

Comments
 (0)