Skip to content

Commit 7f1ea32

Browse files
Don't block on module dependency resolution
1 parent 9c845f7 commit 7f1ea32

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

packages/@tailwindcss-cli/src/commands/build/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
132132

133133
let inputFile = args['--input'] && args['--input'] !== '-' ? args['--input'] : process.cwd()
134134
let inputBasePath = path.dirname(path.resolve(inputFile))
135-
let fullRebuildPaths = [...cssImportPaths]
135+
let fullRebuildPaths: Promise<string[]>[] = [Promise.resolve(cssImportPaths)]
136136

137137
function compile(css: string) {
138138
return tailwindcss.compile(css, {
@@ -142,8 +142,8 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
142142
}
143143

144144
let resolvedPath = path.resolve(inputBasePath, pluginPath)
145-
fullRebuildPaths.push(resolvedPath)
146-
fullRebuildPaths.push(...(await getModuleDependencies(resolvedPath)))
145+
fullRebuildPaths.push(Promise.resolve([resolvedPath]))
146+
fullRebuildPaths.push(getModuleDependencies(resolvedPath))
147147
return import(pathToFileURL(resolvedPath).href + '?id=' + Date.now()).then(
148148
(m) => m.default ?? m,
149149
)
@@ -155,8 +155,8 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
155155
}
156156

157157
let resolvedPath = path.resolve(inputBasePath, configPath)
158-
fullRebuildPaths.push(resolvedPath)
159-
fullRebuildPaths.push(...(await getModuleDependencies(resolvedPath)))
158+
fullRebuildPaths.push(Promise.resolve([resolvedPath]))
159+
fullRebuildPaths.push(getModuleDependencies(resolvedPath))
160160
return import(pathToFileURL(resolvedPath).href + '?id=' + Date.now()).then(
161161
(m) => m.default ?? m,
162162
)
@@ -187,11 +187,13 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
187187
let changedFiles: ChangedContent[] = []
188188
let rebuildStrategy: 'incremental' | 'full' = 'incremental'
189189

190+
let resolvedFullRebuildPaths = (await Promise.all(fullRebuildPaths)).flat()
191+
190192
for (let file of files) {
191193
// If one of the changed files is related to the input CSS or JS
192194
// config/plugin files, then we need to do a full rebuild because
193195
// the theme might have changed.
194-
if (fullRebuildPaths.includes(file)) {
196+
if (resolvedFullRebuildPaths.includes(file)) {
195197
rebuildStrategy = 'full'
196198

197199
// No need to check the rest of the events, because we already know we
@@ -226,8 +228,8 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
226228
`,
227229
args['--input'] ?? base,
228230
)
229-
clearRequireCache(fullRebuildPaths)
230-
fullRebuildPaths = cssImportPaths
231+
clearRequireCache(resolvedFullRebuildPaths)
232+
fullRebuildPaths = [Promise.resolve(cssImportPaths)]
231233

232234
// Create a new compiler, given the new `input`
233235
compiler = await compile(input)

packages/@tailwindcss-node/src/get-module-dependencies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,5 @@ export async function getModuleDependencies(absoluteFilePath: string) {
102102
path.extname(absoluteFilePath),
103103
)
104104

105-
return seen
105+
return Array.from(seen)
106106
}

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
4949
compiler: null as null | Awaited<ReturnType<typeof compile>>,
5050
css: '',
5151
optimizedCss: '',
52-
fullRebuildPaths: [] as string[],
52+
fullRebuildPaths: [] as Promise<string[]>[],
5353
}
5454
})
5555

@@ -84,8 +84,10 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
8484
let context = cache.get(inputFile)
8585
let inputBasePath = path.dirname(path.resolve(inputFile))
8686

87-
function createCompiler() {
88-
clearRequireCache(context.fullRebuildPaths)
87+
async function createCompiler() {
88+
for (let files of await Promise.all(context.fullRebuildPaths)) {
89+
clearRequireCache(files)
90+
}
8991
context.fullRebuildPaths = []
9092
return compile(root.toString(), {
9193
loadPlugin: async (pluginPath) => {
@@ -94,8 +96,8 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
9496
}
9597

9698
let resolvedPath = path.resolve(inputBasePath, pluginPath)
97-
context.fullRebuildPaths.push(resolvedPath)
98-
context.fullRebuildPaths.push(...(await getModuleDependencies(resolvedPath)))
99+
context.fullRebuildPaths.push(Promise.resolve([resolvedPath]))
100+
context.fullRebuildPaths.push(getModuleDependencies(resolvedPath))
99101
return import(pathToFileURL(resolvedPath).href + '?id=' + Date.now()).then(
100102
(m) => m.default ?? m,
101103
)
@@ -107,8 +109,8 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
107109
}
108110

109111
let resolvedPath = path.resolve(inputBasePath, configPath)
110-
context.fullRebuildPaths.push(resolvedPath)
111-
context.fullRebuildPaths.push(...(await getModuleDependencies(resolvedPath)))
112+
context.fullRebuildPaths.push(Promise.resolve([resolvedPath]))
113+
context.fullRebuildPaths.push(getModuleDependencies(resolvedPath))
112114
return import(pathToFileURL(resolvedPath).href + '?id=' + Date.now()).then(
113115
(m) => m.default ?? m,
114116
)
@@ -124,13 +126,15 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
124126

125127
// Track file modification times to CSS files
126128
{
127-
for (let file of context.fullRebuildPaths) {
128-
result.messages.push({
129-
type: 'dependency',
130-
plugin: '@tailwindcss/postcss',
131-
file,
132-
parent: result.opts.from,
133-
})
129+
for (let files of await Promise.all(context.fullRebuildPaths)) {
130+
for (let file of files) {
131+
result.messages.push({
132+
type: 'dependency',
133+
plugin: '@tailwindcss/postcss',
134+
file,
135+
parent: result.opts.from,
136+
})
137+
}
134138
}
135139

136140
let files = result.messages.flatMap((message) => {

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,18 @@ export default function tailwindcss(): Plugin[] {
9595
}
9696

9797
let resolvedPath = path.resolve(inputBasePath, pluginPath)
98+
let [module, moduleDependencies] = await Promise.all([
99+
import(pathToFileURL(resolvedPath).href + '?id=' + Date.now()),
100+
getModuleDependencies(resolvedPath),
101+
])
102+
98103
addWatchFile(resolvedPath)
99104
fullRebuildPaths.push(resolvedPath)
100-
for (let file of await getModuleDependencies(resolvedPath)) {
105+
for (let file of moduleDependencies) {
101106
addWatchFile(file)
102107
fullRebuildPaths.push(file)
103108
}
104-
return import(pathToFileURL(resolvedPath).href + '?id=' + Date.now()).then(
105-
(m) => m.default ?? m,
106-
)
109+
return module.default ?? module
107110
},
108111

109112
loadConfig: async (configPath) => {
@@ -112,15 +115,18 @@ export default function tailwindcss(): Plugin[] {
112115
}
113116

114117
let resolvedPath = path.resolve(inputBasePath, configPath)
118+
let [module, moduleDependencies] = await Promise.all([
119+
import(pathToFileURL(resolvedPath).href + '?id=' + Date.now()),
120+
getModuleDependencies(resolvedPath),
121+
])
122+
115123
addWatchFile(resolvedPath)
116124
fullRebuildPaths.push(resolvedPath)
117-
for (let file of await getModuleDependencies(resolvedPath)) {
125+
for (let file of moduleDependencies) {
118126
addWatchFile(file)
119127
fullRebuildPaths.push(file)
120128
}
121-
return import(pathToFileURL(resolvedPath).href + '?id=' + Date.now()).then(
122-
(m) => m.default ?? m,
123-
)
129+
return module.default ?? module
124130
},
125131
})
126132

0 commit comments

Comments
 (0)