Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject @config "..." when a tailwind.config.{js,ts,...} is detected #14635

Merged
merged 15 commits into from
Oct 10, 2024

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Oct 9, 2024

This PR injects a @config "…" in the CSS file if a JS based config has been found.

We will try to inject the @config in a sensible place:

  1. Above the very first @theme
  2. If that doesn't work, below the last @import
  3. If that doesn't work, at the top of the file (as a last resort)

@thecrypticace thecrypticace force-pushed the feat/codemod-stylesheet-graphs branch from efe9ac4 to faed6a8 Compare October 9, 2024 16:54
@thecrypticace thecrypticace force-pushed the feat/codemod-inject-at-config branch from f5978b6 to b406485 Compare October 9, 2024 17:08
@thecrypticace thecrypticace force-pushed the feat/codemod-stylesheet-graphs branch from 1b2705f to bc60358 Compare October 9, 2024 17:25
@thecrypticace thecrypticace force-pushed the feat/codemod-inject-at-config branch from b406485 to 7a39ad7 Compare October 9, 2024 17:26
@thecrypticace thecrypticace force-pushed the feat/codemod-stylesheet-graphs branch from bc60358 to 15bea44 Compare October 9, 2024 17:36
@thecrypticace thecrypticace force-pushed the feat/codemod-inject-at-config branch 2 times, most recently from 088e23f to bfcbcb5 Compare October 9, 2024 17:42
@RobinMalfait RobinMalfait force-pushed the feat/codemod-inject-at-config branch from cf0be89 to f1513ab Compare October 10, 2024 10:55
Copy link
Member

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff! Some thoughts on how we can reuse this for the CSS we generate from the JS config too. For the relative path rewrite: If you orient yourself on the other places where we do that, it should be good 👍

Comment on lines 39 to 42
// Inject the `@config` in a sensible place
// 1. Above the first `@theme`
// 2. Below the last `@import`
// 3. At the top of the file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be the place for adding the CSS config if we can migrate the JS config. Maybe we can make the "what is injected" here dynamic so we can decide in the JS config migrator what we need in the css?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I think once this PR is merged that we can abstract this to a function if we want. Not sure if that's necessary for this PR yet.

There might be some additional logic where we might want to control the order of @config, @theme, injected @variants and so on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha yep I would need it right away, let's pair on this afterwards 👍

@thecrypticace thecrypticace force-pushed the feat/codemod-stylesheet-graphs branch from 15bea44 to 589d0e8 Compare October 10, 2024 13:18
@thecrypticace thecrypticace force-pushed the feat/codemod-inject-at-config branch from d242cca to baa6ffb Compare October 10, 2024 13:19
Base automatically changed from feat/codemod-stylesheet-graphs to next October 10, 2024 13:44

--- ./src/input.css ---
@import 'tailwindcss';
@config "../tailwind.config.js";"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@config "../tailwind.config.js";"
@config "../tailwind.config.js";

Inject the `@config` in a sensible place:

1. Above the first `@theme`
2. Below the last `@import`
3. At the top of the file
If you have a setup where you have an index.css file, which in turn
imports 2 new files with Tailwind CSS imports. In that case we want to
inject the `@config` into the root, not in the individual files.

Input:
```
--- ./src/root.4.css ---
/* Inject missing @config due to nested imports with tailwind imports */
@import './root.4/base.css';
@import './root.4/utilities.css';

--- ./src/root.4/base.css ---
@import 'tailwindcss/base';

--- ./src/root.4/utilities.css ---
@import 'tailwindcss/utilities';
```

Output:
```
--- ./src/root.4.css ---
/* Inject missing @config due to nested imports with tailwind imports */
@import './root.4/base.css';
@import './root.4/utilities.css';
@config "../tailwind.config.ts";

--- ./src/root.4/base.css ---
@import 'tailwindcss/theme' layer(theme);
@import 'tailwindcss/preflight' layer(base);

--- ./src/root.4/utilities.css ---
@import 'tailwindcss/utilities' layer(utilities);"
```
This is the same setup as in `./packages/@tailwindcss-vite/src/index.ts`
If a non-root file contains `@import "tailwindcss"` (without its
individual imports), then we can inject the `@config` in this file as
well.

This makes it more co-located with where you setup Tailwind CSS
@thecrypticace thecrypticace force-pushed the feat/codemod-inject-at-config branch from baa6ffb to 754190e Compare October 10, 2024 13:47
If not, some files could look weird:
```
@import "tailwind.css";"
```

Which feels wrong, but the last `"` is coming from the
`toMatchInlineSnapshot()` function.
@RobinMalfait RobinMalfait enabled auto-merge (squash) October 10, 2024 13:59
@RobinMalfait RobinMalfait merged commit fb1731a into next Oct 10, 2024
1 check passed
@RobinMalfait RobinMalfait deleted the feat/codemod-inject-at-config branch October 10, 2024 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants