Skip to content

Watch files that a full rebuild adds in @tailwindcss/webpack and @tailwindcss/turbopack - #20762

Open
RobinMalfait wants to merge 2 commits into
mainfrom
robin/open-pr-watch-new-dependencies-after-a-full-rebuild-in-webpack/turbopack
Open

RobinMalfait wants to merge 2 commits into
mainfrom
robin/open-pr-watch-new-dependencies-after-a-full-rebuild-in-webpack/turbopack

Conversation

@RobinMalfait

Copy link
Copy Markdown
Contributor

This PR fixes watch mode in @tailwindcss/webpack and @tailwindcss/turbopack for files that a CSS file starts to depend on, like a new @import, @config or @plugin.

Problem

The loaders reported the dependencies of the previous compiler to the bundler, and only then did a full rebuild. Files that only the new compiler found were not reported for that build. webpack and Turbopack reset the dependencies of a module on each build, so these files were not watched. A change to such a file did not cause a rebuild, until something else rebuilt the CSS module.

Example, with styles outside of the source(…) folder:

/* styles/index.css */
@import 'tailwindcss' source('../src');
@import './buttons.css'; /* Just added */

The new @import works. But a change to styles/buttons.css after that did nothing.

Files that the scanner finds were not affected, because the loaders report those separately. So the bug shows for files outside of the scanned sources, like files outside of source(…), in another workspace package, or ignored by git.

Fix

Report each dependency to the bundler as soon as the compiler finds it, in onDependency. @tailwindcss/vite already does this. Each path is reported once per build.

This also covers a full rebuild that fails after it found a new file. For example, a new buttons.css with @apply of an unknown class: a fix in buttons.css now causes a rebuild.

The new files get their mtime on the next build, like before. So that build is still a full rebuild.

Test plan

New integration tests:

  • @tailwindcss/webpack loader watches files that a full rebuild adds (watch)
  • @tailwindcss/webpack loader watches files that a failed full rebuild adds (watch)
  • @tailwindcss/turbopack loader watches files that a full rebuild adds (dev)

All 3 fail on main with the old loaders, and pass with this PR. The other webpack and Turbopack integration tests still pass.

Notes

  • @tailwindcss/postcss has the same bug when its dependencies go to a bundler (for example webpack with postcss-loader). Its error path already reports the new files, so it needs a different change. This PR does not change it.
  • Both loaders have the same code. A shared internal helper could prevent drift, but that is a larger change, so it is not part of this PR.

…ders

The loaders reported the config files, plugins and imported stylesheets
of the previous compiler, and only then did a full rebuild. Files that
only the new compiler found, like a new `@import`, `@config` or
`@plugin`, were not watched after that build. Bundlers reset the
dependencies of a module on each build, so a change to such a file did
not cause a rebuild until something else changed the CSS module.

Report each dependency as soon as the compiler finds it. This also
covers a new file that makes the compile fail: a fix in that file now
causes a rebuild.
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.

1 participant