11 Commits

Author SHA1 Message Date
Philipp Spiess
1467dab59e
Fix template migration issues (#14600)
This PR fixes two issues we found when testing the candidate codemodes:

1. Sometimes, core would emit the same candidate twice. This would
result into rewriting a range multiple times, without realizing that
this change might already be applied, causing it to swallow or duplicate
some bytes.
2. The codemods were mutating the `Candidate` object, however since the
`Candidate` parsing is _cached_ in core, it would sometimes return the
same instance. This is an issue especially since we monkey patch the
prefix to `null` when migrating prefixed candidates. This means that a
candidate would be cached that would be _invalid relative to the real
design system_. We fixed this by making sure the mutations would only be
applied to clones of the `Candidate` and I changed the `DesignSystem`
API to return `ReadOnly<T>` versions of these candidates. A better
solution would maybe be to disable the cache at all but this requires
broader changes in Core.
2024-10-08 18:06:43 +02:00
Robin Malfait
35b84cc313
Improve @tailwindcss/postcss performance for initial builds (#14565)
This PR improves the performance of the `@tailwindcss/postcss` plugin.
Before this change we created 2 compiler instances instead of a single
one. On a project where a `tailwindcss.config.ts` file is used, this
means that the timings look like this:

```
[@tailwindcss/postcss] Setup compiler: 137.525ms
⋮
[@tailwindcss/postcss] Setup compiler: 43.95ms
```

This means that with this small change, we can easily shave of ~50ms for
initial PostCSS builds.

---------

Co-authored-by: Philipp Spiess <hello@philippspiess.com>
Co-authored-by: Jordan Pittman <jordan@cryptica.me>
2024-10-03 16:21:54 +02:00
Philipp Spiess
bbe08c3b84
Add binary extensions found in macOS traces (#14584)
While we were doing some tracing for Rust memory issues, we noticed that
the builds became slower and slower. Turns out we did store the macOS
`.trace` dirs within the auto content directory of the Tailwind CSS
instance we were profiling and that _some of the files were massive
binary files that we were now scanning_.

Here's the anatomy of a single trace:


```
[ 320]  .
├── [497K]  form.template
├── [5.1K]  open.creq
├── [ 261]  UI_state_metadata.bin
├── [ 160]  corespace
│   ├── [1.2K]  MANIFEST.plist
│   ├── [  96]  currentRun
│   │   └── [  96]  core
│   │       └── [ 128]  uniquing
│   │           ├── [ 128]  arrayUniquer
│   │           │   ├── [10.0K]  integeruniquer.data
│   │           │   └── [   0]  integeruniquer.index
│   │           └── [ 128]  typedArrayUniquer
│   │               ├── [10.0K]  integeruniquer.data
│   │               └── [   0]  integeruniquer.index
│   └── [  96]  run1
│       └── [ 192]  core
│           ├── [ 224]  uniquing
│           │   ├── [6.5K]  EngineeringTypes.etypes
│           │   ├── [3.5K]  strings
│           │   ├── [ 344]  TOC
│           │   ├── [ 128]  arrayUniquer
│           │   │   ├── [1.1K]  integeruniquer.data
│           │   │   └── [  96]  integeruniquer.index
│           │   └── [ 128]  typedArrayUniquer
│           │       ├── [1.0K]  integeruniquer.data
│           │       └── [  28]  integeruniquer.index
│           ├── [ 192]  stores
│           │   ├── [ 224]  indexed-store-0
│           │   │   ├── [1.6K]  bulkstore
│           │   │   ├── [1.2K]  spindex.0
│           │   │   ├── [1.1K]  bulkstore_descriptor
│           │   │   ├── [ 433]  spec.plist
│           │   │   └── [ 188]  schema.xml
│           │   ├── [ 224]  indexed-store-1
│           │   │   ├── [7.5K]  bulkstore
│           │   │   ├── [7.0K]  spindex.0
│           │   │   ├── [1.6K]  bulkstore_descriptor
│           │   │   ├── [ 522]  spec.plist
│           │   │   └── [ 352]  schema.xml
│           │   ├── [ 224]  indexed-store-2
│           │   │   ├── [ 17K]  bulkstore
│           │   │   ├── [7.2K]  spindex.0
│           │   │   ├── [2.0K]  bulkstore_descriptor
│           │   │   ├── [ 532]  spec.plist
│           │   │   └── [ 412]  schema.xml
│           │   └── [ 192]  indexed-store-3
│           │       ├── [1.8K]  bulkstore_descriptor
│           │       ├── [ 426]  spec.plist
│           │       ├── [ 399]  schema.xml
│           │       └── [  50]  bulkstore
│           ├── [  96]  core-config
│           │   └── [2.0K]  exposedTableInfo.plist
│           └── [  96]  table-manager
│               └── [1.6K]  tables.plist
├── [ 128]  Trace1.run
│   ├── [966M]  event_data_52237.oa
│   └── [ 52K]  RunIssues.storedata
├── [ 128]  instrument_data
│   ├── [  96]  EF4DC038-8A17-421A-8050-39DD0980C06F
│   │   └── [  96]  run_data
│   │       └── [ 17K]  1.run.zip
│   └── [  96]  F9F2B147-A042-43F0-A791-EA6D63C7C1E8
│       └── [  96]  run_data
│           └── [2.2K]  1.run.zip
├── [ 128]  symbols
│   ├── [ 608]  stores
│   │   ├── [453K]  D14A8304-5F09-385D-9AA6-1B0C815B6356.symbolsarchive
│   │   ├── [ 36K]  4E9DB999-EFF4-3C83-B4E8-E1914D2C331E.symbolsarchive
│   │   ├── [3.7K]  B5D897DF-D536-3668-BBAD-A17119439EF0.symbolsarchive
│   │   ├── [3.1K]  57FFCB9D-A6C9-3E9A-AA82-40F192626527.symbolsarchive
│   │   ├── [2.9K]  69AA9AB7-C5DE-3CAE-BF1A-384F9F36A3E0.symbolsarchive
│   │   ├── [2.8K]  F453C5AE-3568-3AAA-AAA0-D2FDFBB9BC7A.symbolsarchive
│   │   ├── [2.6K]  62D27203-665F-3AA7-8BE9-7E3C3A847353.symbolsarchive
│   │   ├── [2.4K]  9896C713-054D-377D-88B4-45E1DE3FB6C5.symbolsarchive
│   │   ├── [2.1K]  249D8F21-72A2-3A80-ADC1-7BEAF24B5B58.symbolsarchive
│   │   ├── [2.1K]  FA954AC0-FCC5-3711-800B-432011ACD89E.symbolsarchive
│   │   ├── [2.0K]  E7ED11EE-AFB0-3B96-90A8-F1835726B9B8.symbolsarchive
│   │   ├── [1.3K]  5B476F9B-DF8B-356A-8582-615D4AD08504.symbolsarchive
│   │   ├── [1.3K]  6102110F-7ED8-34C2-95D3-C5ACCB41497E.symbolsarchive
│   │   ├── [1.1K]  EC86EDBF-30B9-3BF8-A358-C8D51805B016.symbolsarchive
│   │   ├── [1.0K]  6740FF57-8D20-3FC7-97F5-B2AFB6E5F48A.symbolsarchive
│   │   ├── [ 963]  9A72FD37-D827-3D6D-B6F4-422621E36C94.symbolsarchive
│   │   └── [ 948]  67A46592-439B-36DA-8A08-A7CD777A43A8.symbolsarchive
│   └── [ 290]  MANIFEST.plist
└── [  96]  shared_data
    └── [  96]  1.run
        └── [ 14M]  533F36D4-2C90-4030-95CA-74077F2E26D7.zip

29 directories, 59 files
```

Note that the biggest binary file is a `.oa` but I've also added
`.storedata` and `.symbolsarchive` as binary extensions to this PR.
2024-10-03 14:40:07 +02:00
Bartłomiej Maryńczak
fad5c81045
Use official FxHash crate (#14530) 2024-09-26 17:50:31 -04:00
Philipp Spiess
732147a761
Add setup for template migrations (#14502)
This PR adds the initial setup and a first codemod for the template
migrations. These are a new set of migrations that operate on files
defined in the Tailwind v3 config as part of the `content` option (so
your HTML, JavaScript, TSX files etc.).

The migration for this is integrated in the new `@tailwindcss/upgrade`
package and will require pointing the migration to an input JavaScript
config file, like this:

```
npx @tailwindcss/upgrade --config tailwind.config.js
```

The idea of template migrations is to apply breaking changes from the v3
to v4 migration within your template files.

## Migrating !important syntax

The first migration that I’m adding with this PR is to ensure we use the
v4 important syntax that has the exclamation mark at the end of the
utility.

For example, this:

```html
<div class="!flex sm:!block"></div>
```

Will now turn into:

```html
<div class="flex! sm:block!"></div>
```

## Architecture considerations

Implementation wise, we make use of Oxide to scan the content files fast
and efficiently. By relying on the same scanner als Tailwind v4, we
guarantee that all candidates that are part of the v4 output will have
gone through a migration.

Migrations itself operate on the abstract `Candidate` type, similar to
the type we use in the v4 codebase. It will parse the candidate into its
parts so they can easily be introspected/modified. Migrations are typed
as:

```ts
type TemplateMigration = (candidate: Candidate) => Candidate | null
``` 

`null` should be returned if the `Candidate` does not need a migration. 

We currently use the v4 `parseCandidate` function to get an abstract
definition of the candidate rule that we can operate on. _This will
likely need to change in the future as we need to fork `parseCandidate`
for v3 specific syntax_.

Additionally, we're inlining a `printCandidate` function that can
stringify the abstract `Candidate` type. It is not guaranteed that this
is an identity function since some information can be lost during the
parse step. This is not a problem though, because migrations will only
run selectively and if none of the selectors trigger, the candidates are
not updated. h/t to @RobinMalfait for providing the printer.

So the overall flow of a migration looks like this:

- Scan the config file for `content` files
- Use Oxide to extract a list of candidate and their positions from
these `content` files
- Run a few migrations that operate on the `Candidate` abstract type.
- Print the updated `Candidate` back into the original `content` file.

---------

Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
Co-authored-by: Jordan Pittman <jordan@cryptica.me>
2024-09-25 16:20:14 +02:00
Robin Malfait
a902128640
Improve Oxide scanner API (#14187)
This PR updates the API for interacting with the Oxide API. Until now,
we used the name `scanDir(…)` which is fine, but we do way more work
right now.

We now have features such as:

1. Auto source detection (can be turned off, e.g.: `@tailwindcss/vite`
doesn't need it)
2. Scan based on `@source`s found in CSS files
3. Do "incremental" rebuilds (which means that the `scanDir(…)` result
was stateful).

To solve these issues, this PR introduces a new `Scanner` class where
you can pass in the `detectSources` and `sources` options. E.g.:

```ts
let scanner = new Scanner({
  // Optional, omitting `detectSources` field disables automatic source detection
  detectSources: { base: __dirname }, 

  // List of glob entries to scan. These come from `@source` directives in CSS.
  sources: [
    { base: __dirname, pattern: "src/**/*.css" },
    // …
  ],
});
```

The scanner object has the following API:

```ts
export interface ChangedContent {
  /** File path to the changed file */
  file?: string
  /** Contents of the changed file */
  content?: string
  /** File extension */
  extension: string
}
export interface DetectSources {
  /** Base path to start scanning from */
  base: string
}
export interface GlobEntry {
  /** Base path of the glob */
  base: string
  /** Glob pattern */
  pattern: string
}
export interface ScannerOptions {
  /** Automatically detect sources in the base path */
  detectSources?: DetectSources
  /** Glob sources */
  sources?: Array<GlobEntry>
}
export declare class Scanner {
  constructor(opts: ScannerOptions)
  scan(): Array<string>
  scanFiles(input: Array<ChangedContent>): Array<string>
  get files(): Array<string>
  get globs(): Array<GlobEntry>
}
```

The `scanFiles(…)` method is used for incremental rebuilds. It takes the
`ChangedContent` array for all the new/changes files. It returns whether
we scanned any new candidates or not.

Note that the `scanner` object is stateful, this means that we don't
have to track candidates in a `Set` anymore. We can just call
`getCandidates()` when we need it.

This PR also removed some unused code that we had in the `scanDir(…)`
function to allow for sequential or parallel `IO`, and sequential or
parallel `Parsing`. We only used the same `IO` and `Parsing` strategies
for all files, so I just got rid of it.

---------

Co-authored-by: Jordan Pittman <jordan@cryptica.me>
2024-08-16 15:05:42 +02:00
Martin Feckie
cc4689deef
Add Glimmer template extensions to Oxide content detection (#14199)
Co-authored-by: Jordan Pittman <jordan@cryptica.me>
2024-08-15 09:34:19 -04:00
Robin Malfait
541d84a3bb
Add @source support (#14078)
This PR is an umbrella PR where we will add support for the new
`@source` directive. This will allow you to add explicit content glob
patterns if you want to look for Tailwind classes in other files that
are not automatically detected yet.

Right now this is an addition to the existing auto content detection
that is automatically enabled in the `@tailwindcss/postcss` and
`@tailwindcss/cli` packages. The `@tailwindcss/vite` package doesn't use
the auto content detection, but uses the module graph instead.

From an API perspective there is not a lot going on. There are only a
few things that you have to know when using the `@source` directive, and
you probably already know the rules:

1. You can use multiple `@source` directives if you want.
2. The `@source` accepts a glob pattern so that you can match multiple
files at once
3. The pattern is relative to the current file you are in
4. The pattern includes all files it is matching, even git ignored files
1. The motivation for this is so that you can explicitly point to a
`node_modules` folder if you want to look at `node_modules` for whatever
reason.
6. Right now we don't support negative globs (starting with a `!`) yet,
that will be available in the near future.

Usage example:

```css
/* ./src/input.css */
@import "tailwindcss";
@source "../laravel/resources/views/**/*.blade.php";
@source "../../packages/monorepo-package/**/*.js";
```

It looks like the PR introduced a lot of changes, but this is a side
effect of all the other plumbing work we had to do to make this work.
For example:

1. We added dedicated integration tests that run on Linux and Windows in
CI (just to make sure that all the `path` logic is correct)
2. We Have to make sure that the glob patterns are always correct even
if you are using `@import` in your CSS and use `@source` in an imported
file. This is because we receive the flattened CSS contents where all
`@import`s are inlined.
3. We have to make sure that we also listen for changes in the files
that match any of these patterns and trigger a rebuild.

PRs:

- [x] https://github.com/tailwindlabs/tailwindcss/pull/14063
- [x] https://github.com/tailwindlabs/tailwindcss/pull/14085
- [x] https://github.com/tailwindlabs/tailwindcss/pull/14079
- [x] https://github.com/tailwindlabs/tailwindcss/pull/14067
- [x] https://github.com/tailwindlabs/tailwindcss/pull/14076
- [x] https://github.com/tailwindlabs/tailwindcss/pull/14080
- [x] https://github.com/tailwindlabs/tailwindcss/pull/14127
- [x] https://github.com/tailwindlabs/tailwindcss/pull/14135

Once all the PRs are merged, then this umbrella PR can be merged. 

> [!IMPORTANT]  
> Make sure to merge this without rebasing such that each individual PR
ends up on the main branch.

---------

Co-authored-by: Philipp Spiess <hello@philippspiess.com>
Co-authored-by: Jordan Pittman <jordan@cryptica.me>
Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
2024-08-07 16:38:44 +02:00
Philipp Spiess
a2159e80f5
Add Windows CI (#14065)
This PR changes the GitHub action workflow for V4 to start running all
unit tests and build on both Ubuntu (our current default) _and_ Windows.
This is to ensure we catch issues with paths and other Windows-specific
things sooner. It does, however, not replace testing on Windows.
2024-07-29 16:50:06 +02:00
Robin Malfait
19c8fe34fd
remove Rust benchmarks and fixtures (#13335)
We currently don't use these, and they are tested via the benchmarks we
run as an end-to-end benchmark.
2024-03-23 16:16:40 +01:00
Robin Malfait
1c48683a23
Hoist oxide/crates to just crates (#13333)
* move `oxide/crates` to `crates`

* ignore `target/` folder

* ensure pnpm points to `crates` instead of `oxide/crates`

* ensure all paths point to `crates` instead of `oxide/crates`

* update `oxide/crates` -> `crates` path in workflows

* use correct path in .prettierignore

* rename `crates/core` to `crates/oxide`

* remove oxide folder

* fix test script to run `cargo test` directly
2024-03-23 09:00:48 -04:00