Skip to content

Commit 77e8bf9

Browse files
committed
merge globs and detectSources
Now that we can promote `@source "../path/to/folder"` to enable auto source detection, it means that we can just merge the globs together and add the base folder as if it was `@source "../path/to/folder"`
1 parent dea08ba commit 77e8bf9

File tree

4 files changed

+41
-72
lines changed

4 files changed

+41
-72
lines changed

crates/node/src/lib.rs

-17
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ pub struct ChangedContent {
1818
pub extension: String,
1919
}
2020

21-
#[derive(Debug, Clone)]
22-
#[napi(object)]
23-
pub struct DetectSources {
24-
/// Base path to start scanning from
25-
pub base: String,
26-
}
27-
2821
#[derive(Debug, Clone)]
2922
#[napi(object)]
3023
pub struct GlobEntry {
@@ -62,20 +55,11 @@ impl From<tailwindcss_oxide::GlobEntry> for GlobEntry {
6255
}
6356
}
6457

65-
impl From<DetectSources> for tailwindcss_oxide::scanner::detect_sources::DetectSources {
66-
fn from(detect_sources: DetectSources) -> Self {
67-
Self::new(detect_sources.base.into())
68-
}
69-
}
70-
7158
// ---
7259

7360
#[derive(Debug, Clone)]
7461
#[napi(object)]
7562
pub struct ScannerOptions {
76-
/// Automatically detect sources in the base path
77-
pub detect_sources: Option<DetectSources>,
78-
7963
/// Glob sources
8064
pub sources: Option<Vec<GlobEntry>>,
8165
}
@@ -102,7 +86,6 @@ impl Scanner {
10286
pub fn new(opts: ScannerOptions) -> Self {
10387
Self {
10488
scanner: tailwindcss_oxide::Scanner::new(
105-
opts.detect_sources.map(Into::into),
10689
opts
10790
.sources
10891
.map(|x| x.into_iter().map(Into::into).collect()),

crates/oxide/src/lib.rs

+34-47
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ pub struct GlobEntry {
6262

6363
#[derive(Debug, Clone, Default)]
6464
pub struct Scanner {
65-
/// Auto content configuration
66-
detect_sources: Option<DetectSources>,
67-
6865
/// Glob sources
6966
sources: Option<Vec<GlobEntry>>,
7067

@@ -86,9 +83,8 @@ pub struct Scanner {
8683
}
8784

8885
impl Scanner {
89-
pub fn new(detect_sources: Option<DetectSources>, sources: Option<Vec<GlobEntry>>) -> Self {
86+
pub fn new(sources: Option<Vec<GlobEntry>>) -> Self {
9087
Self {
91-
detect_sources,
9288
sources,
9389
..Default::default()
9490
}
@@ -206,51 +202,11 @@ impl Scanner {
206202
return;
207203
}
208204

209-
self.detect_sources();
210205
self.scan_sources();
211206

212207
self.ready = true;
213208
}
214209

215-
#[tracing::instrument(skip_all)]
216-
fn detect_sources(&mut self) {
217-
if let Some(detect_sources) = &self.detect_sources {
218-
let (files, globs) = detect_sources.detect();
219-
self.files.extend(files);
220-
self.globs.extend(globs);
221-
}
222-
223-
// Find all `@source` globs that point to a directory. If so, promote the source to auto
224-
// source detection instead.
225-
if let Some(sources) = &mut self.sources {
226-
for source in sources {
227-
// If a glob ends with `**/*`, then we just want to register the base path as a new
228-
// base.
229-
if source.pattern.ends_with("**/*") {
230-
source.pattern = source.pattern.trim_end_matches("**/*").to_owned();
231-
}
232-
233-
let path = PathBuf::from(&source.base).join(&source.pattern);
234-
let Some(folder_name) = path.file_name() else {
235-
continue;
236-
};
237-
238-
// Contains a file extension, e.g.: `foo.html`, therefore we don't want to
239-
// detect sources here.
240-
if folder_name.to_str().unwrap().contains(".") {
241-
continue;
242-
}
243-
244-
// Promote to auto source detection
245-
let detect_sources = DetectSources::new(path.clone());
246-
247-
let (files, globs) = detect_sources.detect();
248-
self.files.extend(files);
249-
self.globs.extend(globs);
250-
}
251-
}
252-
}
253-
254210
#[tracing::instrument(skip_all)]
255211
fn scan_sources(&mut self) {
256212
let Some(sources) = &self.sources else {
@@ -261,7 +217,38 @@ impl Scanner {
261217
return;
262218
}
263219

264-
let resolved_files: Vec<_> = match fast_glob(sources) {
220+
// Partition sources into sources that should be promoted to auto source detection and
221+
// sources that should be resolved as globs.
222+
let (auto_sources, glob_sources): (Vec<_>, Vec<_>) = sources.iter().partition(|source| {
223+
// If a glob ends with `/**/*`, then we just want to register the base path as a new
224+
// base. Essentially converting it to use auto source detection.
225+
if source.pattern.ends_with("**/*") {
226+
return true;
227+
}
228+
229+
// Directories should be promoted to auto source detection
230+
if PathBuf::from(&source.base).join(&source.pattern).is_dir() {
231+
return true;
232+
}
233+
234+
false
235+
});
236+
237+
// Turn `Vec<&GlobEntry>` in `Vec<GlobEntry>`
238+
let glob_sources: Vec<_> = glob_sources.into_iter().cloned().collect();
239+
240+
for path in auto_sources
241+
.iter()
242+
.map(|source| PathBuf::from(&source.base).join(source.pattern.trim_end_matches("**/*")))
243+
{
244+
let detect_sources = DetectSources::new(path);
245+
246+
let (files, globs) = detect_sources.detect();
247+
self.files.extend(files);
248+
self.globs.extend(globs);
249+
}
250+
251+
let resolved_files: Vec<_> = match fast_glob(&glob_sources) {
265252
Ok(matches) => matches
266253
.filter_map(|x| dunce::canonicalize(&x).ok())
267254
.collect(),
@@ -272,7 +259,7 @@ impl Scanner {
272259
};
273260

274261
self.files.extend(resolved_files);
275-
self.globs.extend(sources.clone());
262+
self.globs.extend(glob_sources);
276263

277264
// Re-optimize the globs to reduce the number of patterns we have to scan.
278265
self.globs = get_fast_patterns(&self.globs)

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,22 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
138138
},
139139
})
140140

141-
let detectSources = (() => {
141+
let sources = (() => {
142142
// Disable auto source detection
143143
if (compiler.root === 'none') {
144-
return undefined
144+
return []
145145
}
146146

147147
// No root specified, use the base directory
148148
if (compiler.root === null) {
149-
return { base }
149+
return [{ base, pattern: '**/*' }]
150150
}
151151

152152
// Use the specified root
153-
return { base: path.resolve(compiler.root.base, compiler.root.pattern) }
154-
})()
153+
return [{ base: path.resolve(compiler.root.base, compiler.root.pattern), pattern: '**/*' }]
154+
})().concat(compiler.globs)
155155

156-
let scanner = new Scanner({ detectSources, sources: compiler.globs })
156+
let scanner = new Scanner({ sources })
157157
env.DEBUG && console.timeEnd('[@tailwindcss/cli] Setup compiler')
158158

159159
return [compiler, scanner] as const

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
136136
if (context.scanner === null || rebuildStrategy === 'full') {
137137
// Look for candidates used to generate the CSS
138138
context.scanner = new Scanner({
139-
detectSources: { base },
140-
sources: context.compiler.globs,
139+
sources: [{ base, pattern: '**/*' }].concat(context.compiler.globs),
141140
})
142141
}
143142

0 commit comments

Comments
 (0)