From 5d5b78943496d2f7f7e041bc252bbb5f35be4648 Mon Sep 17 00:00:00 2001 From: Mantas Kantautas Date: Sat, 24 Oct 2026 10:18:23 +0300 Subject: [PATCH] Report files read through composes and @value as dependencies --- src/FileSystemLoader.js | 6 +++++ src/pluginFactory.js | 11 +++++++++ test/test.js | 55 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/FileSystemLoader.js b/src/FileSystemLoader.js index d319ae1..2f8a427 100644 --- a/src/FileSystemLoader.js +++ b/src/FileSystemLoader.js @@ -135,6 +135,12 @@ export default class FileSystemLoader { .filter(Boolean); } + // Files read through `composes` and `@value` imports, including nested ones, + // in the order their CSS is prepended. + get dependencies() { + return this.finalSources.map((entry) => entry.file); + } + get finalSource() { return this.finalSources.map((entry) => entry.source).join(""); } diff --git a/src/pluginFactory.js b/src/pluginFactory.js index d815f1b..5e8b4e7 100644 --- a/src/pluginFactory.js +++ b/src/pluginFactory.js @@ -134,6 +134,17 @@ export function makePlugin(opts) { if (out) css.prepend(out); } + // Report the files read through `composes` and `@value` imports, so + // runners can rebuild this file when one of them changes. + for (const file of loader.dependencies || []) { + result.messages.push({ + type: "dependency", + plugin: PLUGIN_NAME, + file, + parent: result.opts.from || inputFile, + }); + } + if (opts.localsConvention) { const reducer = makeLocalsConventionReducer(opts.localsConvention, inputFile); parser.exportTokens = Object.entries(parser.exportTokens).reduce(reducer, {}); diff --git a/test/test.js b/test/test.js index aa19c09..6d62e7a 100644 --- a/test/test.js +++ b/test/test.js @@ -480,7 +480,60 @@ it("exposes export tokens for other plugins", async () => { from: sourceFile, }); - expect(result.messages).toMatchSnapshot("exposes export tokens for other plugins"); + const exportMessages = result.messages.filter((message) => message.type === "export"); + expect(exportMessages).toMatchSnapshot("exposes export tokens for other plugins"); +}); + +it("reports files read through composes and @value as dependencies", async () => { + const dependenciesOf = async (name) => { + const sourceFile = path.join(fixturesPath, "in", `${name}.css`); + const source = fs.readFileSync(sourceFile).toString(); + const result = await postcss([plugin({ generateScopedName, getJSON: () => {} })]).process( + source, + { from: sourceFile }, + ); + return result.messages + .filter((message) => message.type === "dependency") + .map(({ plugin, file, parent }) => ({ + plugin, + file: path.relative(fixturesPath, file), + parent: path.relative(fixturesPath, parent), + })); + }; + + expect(await dependenciesOf("composes")).toEqual([ + { + plugin: "postcss-modules", + file: path.join("in", "composes.a.css"), + parent: path.join("in", "composes.css"), + }, + { + plugin: "postcss-modules", + file: path.join("in", "composes.mixins.css"), + parent: path.join("in", "composes.css"), + }, + ]); + expect(await dependenciesOf("values")).toEqual([ + { + plugin: "postcss-modules", + file: path.join("in", "values.colors.css"), + parent: path.join("in", "values.css"), + }, + ]); + expect(await dependenciesOf("classes")).toEqual([]); +}); + +it("reports the processed file as the parent of its dependencies", async () => { + const sourceFile = path.join(fixturesPath, "in", "composes.css"); + const processedFile = path.join(fixturesPath, "in", "entry.css"); + const root = postcss.parse(fs.readFileSync(sourceFile).toString(), { from: sourceFile }); + const processor = postcss([plugin({ generateScopedName, getJSON: () => {} })]); + const result = await processor.process(root, { from: processedFile }); + + const parents = result.messages + .filter((message) => message.type === "dependency") + .map((message) => message.parent); + expect(parents).toEqual([processedFile, processedFile]); }); it("processes exportGlobals option", async () => {