@tailwindcss/webpack: scan imported node_modules files via Webpack module graph (parity with @tailwindcss/vite) #19959
Darshan-Naik
started this conversation in
Ideas
Replies: 1 comment 2 replies
|
Since v4.0.8, |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
@tailwindcss/vitewalks Vite's module graph, so Tailwind classes referencedinside imported files from
node_modulesare discovered automatically. Thesame setup on
@tailwindcss/webpack(official webpack loader) does not dothis — only files under the project's
basedirectory are scanned.This forces component library authors to either (a) ask every Webpack
consumer to add an explicit
@source 'node_modules/<lib>/**/*.js';to theirCSS, or (b) ship a CSS file that internally declares
@sourcefor thelibrary's own dist — which prevents per-component tree-shaking.
Current behavior
@tailwindcss/webpack@4.2.4@import 'tailwindcss';(no explicit@source)import { Button } from 'some-lib';— Button.js containsclassName="bg-red-500"Result:
bg-red-500is missing from the emitted CSS. Same repro on@tailwindcss/viteemits it correctly.Repro:
Data from a real design system
We tested this on an internal component library (~60 components, Tailwind
v4 tokens via
@theme):@tailwindcss/vite+ shim entry (no@source)@tailwindcss/webpack+ shim entry (no@source)@tailwindcss/webpack+ library-internal@sourcescanWhy this matters
Component library authors currently can't offer a tree-shakeable CSS entry
point to Webpack consumers, because
@sourceis static and authors can'tknow ahead of time which of their components a given app imports. The Vite
plugin solves this via module-graph integration; the Webpack loader has no
equivalent.
Looking at
@tailwindcss/webpack's source,this.addDependency()/this.addContextDependency()are used for cache invalidation but not forclass discovery — the scanner still uses the static
basedirectory.Proposal
Expose a way for
@tailwindcss/webpackto read Webpack's compilationmodule graph (
compilation.modulesviacompiler.hooks.thisCompilation)and treat every module under
node_modulesthat's currently in the graphas a scan source. This would match
@tailwindcss/vite's behavior and letlibrary authors ship tree-shakeable CSS entries on any bundler.
Happy to prototype if the team thinks the approach is viable.
All reactions