Skip to content

Commit d68a780

Browse files
RobinMalfaitthecrypticaceadamwathan
authored
Auto source detection improvements (#14820)
This PR introduces a new `source(…)` argument and improves on the existing `@source`. The goal of this PR is to make the automatic source detection configurable, let's dig in. By default, we will perform automatic source detection starting at the current working directory. Auto source detection will find plain text files (no binaries, images, ...) and will ignore git-ignored files. If you want to start from a different directory, you can use the new `source(…)` next to the `@import "tailwindcss/utilities" layer(utilities) source(…)`. E.g.: ```css /* ./src/styles/index.css */ @import 'tailwindcss/utilities' layer(utilities) source('../../'); ``` Most people won't split their source files, and will just use the simple `@import "tailwindcss";`, because of this reason, you can use `source(…)` on the import as well: E.g.: ```css /* ./src/styles/index.css */ @import 'tailwindcss' source('../../'); ``` Sometimes, you want to rely on auto source detection, but also want to look in another directory for source files. In this case, yuo can use the `@source` directive: ```css /* ./src/index.css */ @import 'tailwindcss'; /* Look for `blade.php` files in `../resources/views` */ @source '../resources/views/**/*.blade.php'; ``` However, you don't need to specify the extension, instead you can just point the directory and all the same automatic source detection rules will apply. ```css /* ./src/index.css */ @import 'tailwindcss'; @source '../resources/views'; ``` If, for whatever reason, you want to disable the default source detection feature entirely, and only want to rely on very specific glob patterns you define, then you can disable it via `source(none)`. ```css /* Completely disable the default auto source detection */ @import 'tailwindcss' source(none); /* Only look at .blade.php files, nothing else */ @source "../resources/views/**/*.blade.php"; ``` Note: even with `source(none)`, if your `@source` points to a directory, then auto source detection will still be performed in that directory. If you don't want that, then you can simply add explicit files in the globs as seen in the previous example. ```css /* Completely disable the default auto source detection */ @import 'tailwindcss' source(none); /* Run auto source detection in `../resources/views` */ @source "../resources/views"; ``` --------- Co-authored-by: Jordan Pittman <jordan@cryptica.me> Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
1 parent c439cdf commit d68a780

File tree

24 files changed

+2339
-604
lines changed

24 files changed

+2339
-604
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Support specifying the base path for automatic source detection using a `source(…)` function on `@tailwind utilities` or `@import "tailwindcss"` ([#14820](https://github.com/tailwindlabs/tailwindcss/pull/14820))
13+
- Support disabling automatic source detection with `source(none)` ([#14820](https://github.com/tailwindlabs/tailwindcss/pull/14820))
14+
- Support passing directories to `@source` without needing to pass a complete glob ([#14820](https://github.com/tailwindlabs/tailwindcss/pull/14820))
1215
- _Upgrade (experimental)_: Bump `prettier-plugin-tailwindcss` to latest version during upgrade ([#14808](https://github.com/tailwindlabs/tailwindcss/pull/14808))
1316

1417
### Fixed
@@ -18,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1821
### Changed
1922

2023
- Use logical `*-inline` and `*-block` properties for all x/y utilities like `px-*`, `my-*`, `scroll-px-*`, and `inset-y-*` ([#14805](https://github.com/tailwindlabs/tailwindcss/pull/14805))
24+
- Respect automatic source detection heuristics in sources registered with `@source` ([#14820](https://github.com/tailwindlabs/tailwindcss/pull/14820))
2125

2226
## [4.0.0-alpha.30] - 2024-10-24
2327

Cargo.lock

+46-67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/node/src/lib.rs

+1-18
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()),
@@ -128,7 +111,7 @@ impl Scanner {
128111
input: ChangedContent,
129112
) -> Vec<CandidateWithPosition> {
130113
let content = input.content.unwrap_or_else(|| {
131-
std::fs::read_to_string(&input.file.unwrap()).expect("Failed to read file")
114+
std::fs::read_to_string(input.file.unwrap()).expect("Failed to read file")
132115
});
133116

134117
let input = ChangedContent {

crates/node/src/utf16.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,18 @@ impl<'a> IndexConverter<'a> {
3131
// will only ever be incremented up to the length of the input string.
3232
//
3333
// This eliminates a "potential" panic that cannot actually happen
34-
let slice = unsafe {
35-
self.input.get_unchecked(self.curr_utf8..)
36-
};
34+
let slice = unsafe { self.input.get_unchecked(self.curr_utf8..) };
3735

3836
for c in slice.chars() {
3937
if self.curr_utf8 >= pos {
40-
break
38+
break;
4139
}
4240

4341
self.curr_utf8 += c.len_utf8();
4442
self.curr_utf16 += c.len_utf16();
4543
}
4644

47-
return self.curr_utf16 as i64;
45+
self.curr_utf16 as i64
4846
}
4947
}
5048

@@ -66,19 +64,16 @@ mod test {
6664
(4, 4),
6765
(5, 5),
6866
(6, 6),
69-
7067
// inside the 🔥
7168
(7, 8),
7269
(8, 8),
7370
(9, 8),
7471
(10, 8),
75-
7672
// inside the 🥳
7773
(11, 10),
7874
(12, 10),
7975
(13, 10),
8076
(14, 10),
81-
8277
// <space>world!
8378
(15, 11),
8479
(16, 12),
@@ -87,7 +82,6 @@ mod test {
8782
(19, 15),
8883
(20, 16),
8984
(21, 17),
90-
9185
// Past the end should return the last utf-16 character index
9286
(22, 17),
9387
(100, 17),

crates/oxide/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ tracing = { version = "0.1.40", features = [] }
1414
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
1515
walkdir = "2.5.0"
1616
ignore = "0.4.23"
17-
glob-match = "0.2.1"
1817
dunce = "1.0.5"
18+
bexpand = "1.2.0"
19+
glob-match = "0.2.1"
1920

2021
[dev-dependencies]
2122
tempfile = "3.13.0"

0 commit comments

Comments
 (0)