modular-css: Processor.file() does not walk CSS dependencies if they are `@import`ed

Expected Behavior

@modular-css’s processor.js has a file function that adds a file to the Processor instance. This part works well.

const extraStyles = path.resolve(aliases.assets, "styles/extraStyles.css");
await processor.file(extraStyles);

The extraStyles.css file in question is simple:

@import "./element.css";

And that file, in turn, contains only a basic CSS rule.

body { background: red; }

I expect that when I consume the processor with @modular-css/rollup in watch mode, saving either the extraStyles.css or the element.css files will trigger a rebuild.

Current Behavior

Rollup will only rebuild extraStyles.css on save. ~Curiously, saving element.css will cause the terminal to produce this single line, but will not trigger a rebuild:~ (this detail was from a custom file watcher in our project, ignore)

If I change extraStyles.css to be as follows, the dependency walking logic does pick up element.css and call this.addWatchFile on it:

@value * from './element.css';
@import './element.css';

Possible Solution

Not a solution, but I’ve done some source code diving and found a few things. First, the _walk method that is used by Process.file does not recognize any CSS @imports as dependencies. Second, @modular-css\processor\plugins\before\graph-nodes.js calls some PostCSS APIs to walkAtRules for @value and @composes, but not @import. Perhaps relevant?

I suspect there may be another means by which to make Rollup watch every @imported dependency that is different from my current approach. What do you think?

Steps to Reproduce (for bugs)

Not sure if this is a bug or a feature request or just a request for guidance; please advise before I spin up a demo.

Context

My application uses Rollup and the Svelte m-css preprocessor. The above TypeScript is run in a tiny custom plugin that runs before both rollup-plugin-svelte and @modular-css/rollup. The bottom line goal is to append more CSS to the generated Rollup bundles, CSS that isn’t referenced anywhere in the remainder of the app (complex app requires dependency inversion, so the bulk of the app cannot reference extraStyles.css directly. More details can be provided if needed).

Your Environment

Executable Version
modular-css 25.8.2
npm --version 6.14.6
node --version 14.19.0
OS Version
Windows 10 1607

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

The include option for @modular-css/rollup doesn’t tell it to pull in arbitrary files, it just tells it which files in the rollup module graph it should look at.

A plugin that adds globbing support to rollup for something like import * from "./extras/**/*.css"; seems more like what you wanted, but glad you’re sorted.

modular-css is an implementation of the CSS Modules Spec which doesn’t call for any special handling of @import. I’ve incorporated a few proposals that aren’t part of the spec because they seemed valuable and it wasn’t something that could be easily be supported by a plugin that didn’t have access to some of the internal data in the m-css processor.

But @import is fairly standalone, handled well by postcss-import, and that plugin is way more feature-rich than any implementation I would make.

That said, I did some digging and the type: "dependency" messaging is actually a postcss convention/standard for runners, so support for that should absolutely be added to m-css.