From 3a9605937547cb4bf44a65c1a6e18c8b7b2c99b0 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Wed, 9 Nov 2016 18:21:21 -0500 Subject: [PATCH] Add dependency message support (#241) --- index.js | 16 ++++++++++++++- test/{callback.js => import-events.js} | 28 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) rename test/{callback.js => import-events.js} (62%) diff --git a/index.js b/index.js index 6d2d4db7..e8a7deb8 100755 --- a/index.js +++ b/index.js @@ -218,7 +218,11 @@ function resolveImportId( state ) { var atRule = stmt.node - var base = atRule.source && atRule.source.input && atRule.source.input.file + var sourceFile + if (atRule.source && atRule.source.input && atRule.source.input.file) { + sourceFile = atRule.source.input.file + } + var base = sourceFile ? path.dirname(atRule.source.input.file) : options.root @@ -227,6 +231,16 @@ function resolveImportId( if (!Array.isArray(resolved)) { resolved = [ resolved ] } + + // Add dependency messages: + resolved.forEach(function(file) { + result.messages.push({ + type: "dependency", + file: file, + parent: sourceFile, + }) + }) + return Promise.all(resolved.map(function(file) { return loadImportContent( result, diff --git a/test/callback.js b/test/import-events.js similarity index 62% rename from test/callback.js rename to test/import-events.js index cca2b62b..42468a2e 100644 --- a/test/callback.js +++ b/test/import-events.js @@ -52,3 +52,31 @@ test("should have a callback shortcut for webpack", t => { ) }) }) + +test("should add dependency message for each import", t => { + return postcss() + .use(atImport({ + path: "fixtures/imports", + })) + .process(readFileSync("fixtures/media-import.css"), { + from: "fixtures/media-import.css", + }) + .then((result) => { + var deps = result.messages.filter( + message => message.type === "dependency" + ) + var expected = [ + { + type: "dependency", + file: resolve("fixtures/imports/media-import-level-2.css"), + parent: resolve("fixtures/media-import.css"), + }, + { + type: "dependency", + file: resolve("fixtures/imports/media-import-level-3.css"), + parent: resolve("fixtures/imports/media-import-level-2.css"), + }, + ] + t.deepEqual(deps, expected) + }) +})