Skip to content

Commit a0fad5a

Browse files
committed
map globs to Vec<GlobEntry>
We also made sure to store the actual "compiler" in the cache. This way we keep track of both the `globs` and `build` function. Whenever we need to do a full rebuild, we can create a new compiler instance.
1 parent f31fff2 commit a0fad5a

File tree

1 file changed

+55
-42
lines changed
  • packages/@tailwindcss-postcss/src

1 file changed

+55
-42
lines changed

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

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
4343
let cache = new DefaultMap(() => {
4444
return {
4545
mtimes: new Map<string, number>(),
46-
build: null as null | ReturnType<typeof compile>['build'],
46+
compiler: null as null | ReturnType<typeof compile>,
4747
css: '',
4848
optimizedCss: '',
4949
}
@@ -76,6 +76,23 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
7676
OnceExit(root, { result }) {
7777
let inputFile = result.opts.from ?? ''
7878
let context = cache.get(inputFile)
79+
let inputBasePath = path.dirname(path.resolve(inputFile))
80+
81+
function createCompiler() {
82+
return compile(root.toString(), {
83+
loadPlugin: (pluginPath) => {
84+
if (pluginPath[0] === '.') {
85+
return require(path.resolve(inputBasePath, pluginPath))
86+
}
87+
88+
return require(pluginPath)
89+
},
90+
})
91+
}
92+
93+
// Setup the compiler if it doesn't exist yet. This way we can
94+
// guarantee a `compile()` function is available.
95+
context.compiler ??= createCompiler()
7996

8097
let rebuildStrategy: 'full' | 'incremental' = 'incremental'
8198

@@ -106,50 +123,46 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
106123
// Do nothing if neither `@tailwind` nor `@apply` is used
107124
if (!hasTailwind && !hasApply) return
108125

109-
let css = ''
110-
111-
// Look for candidates used to generate the CSS
112-
let { candidates, files, globs } = scanDir({ base })
126+
let css = ''
113127

114-
// Add all found files as direct dependencies
115-
for (let file of files) {
116-
result.messages.push({
117-
type: 'dependency',
118-
plugin: '@tailwindcss/postcss',
119-
file,
120-
parent: result.opts.from,
128+
// Look for candidates used to generate the CSS
129+
let scanDirResult = scanDir({
130+
base, // Root directory, mainly used for auto content detection
131+
contentPaths: context.compiler.globs.map((glob) => ({
132+
base: inputBasePath, // Globs are relative to the input.css file
133+
glob,
134+
})),
121135
})
122-
}
123-
124-
// Register dependencies so changes in `base` cause a rebuild while
125-
// giving tools like Vite or Parcel a glob that can be used to limit
126-
// the files that cause a rebuild to only those that match it.
127-
for (let { base, glob } of globs) {
128-
result.messages.push({
129-
type: 'dir-dependency',
130-
plugin: '@tailwindcss/postcss',
131-
dir: base,
132-
glob,
133-
parent: result.opts.from,
134-
})
135-
}
136-
137-
if (rebuildStrategy === 'full') {
138-
let basePath = path.dirname(path.resolve(inputFile))
139-
let { build } = compile(root.toString(), {
140-
loadPlugin: (pluginPath) => {
141-
if (pluginPath[0] === '.') {
142-
return require(path.resolve(basePath, pluginPath))
143-
}
144136

145-
return require(pluginPath)
146-
},
147-
})
148-
context.build = build
149-
css = build(hasTailwind ? candidates : [])
150-
} else if (rebuildStrategy === 'incremental') {
151-
css = context.build!(candidates)
152-
}
137+
// Add all found files as direct dependencies
138+
for (let file of scanDirResult.files) {
139+
result.messages.push({
140+
type: 'dependency',
141+
plugin: '@tailwindcss/postcss',
142+
file,
143+
parent: result.opts.from,
144+
})
145+
}
146+
147+
// Register dependencies so changes in `base` cause a rebuild while
148+
// giving tools like Vite or Parcel a glob that can be used to limit
149+
// the files that cause a rebuild to only those that match it.
150+
for (let { base, glob } of scanDirResult.globs) {
151+
result.messages.push({
152+
type: 'dir-dependency',
153+
plugin: '@tailwindcss/postcss',
154+
dir: base,
155+
glob,
156+
parent: result.opts.from,
157+
})
158+
}
159+
160+
if (rebuildStrategy === 'full') {
161+
context.compiler = createCompiler()
162+
css = context.compiler.build(hasTailwind ? scanDirResult.candidates : [])
163+
} else if (rebuildStrategy === 'incremental') {
164+
css = context.compiler.build!(scanDirResult.candidates)
165+
}
153166

154167
// Replace CSS
155168
if (css !== context.css && optimize) {

0 commit comments

Comments
 (0)