From c02c6c856e302d08f595a4fd3866edaf16b3cc5f Mon Sep 17 00:00:00 2001
From: Romain Menke <11521496+romainmenke@users.noreply.github.com>
Date: Tue, 1 Aug 2023 18:36:11 +0200
Subject: [PATCH] add `warnOnEmpty` plugin option (#541)

---
 README.md                     | 16 ++++++++++++----
 index.js                      |  1 +
 lib/parse-styles.js           |  2 +-
 test/helpers/check-fixture.js |  8 ++++++++
 test/import.js                |  7 +++++++
 5 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/README.md b/README.md
index aba46eff..c7e40816 100644
--- a/README.md
+++ b/README.md
@@ -125,7 +125,7 @@ Checkout the [tests](test) for more examples.
 
 ### Options
 
-### `filter`
+#### `filter`
 Type: `Function`  
 Default: `() => true`
 
@@ -204,15 +204,23 @@ this value will be ignored.
 
 #### `nameLayer`
 
-Type: `Function`
+Type: `Function`  
 Default: `null`
 
-You can provide a custom naming function for anonymous layers (`@import 'baz.css' layer;`).
+You can provide a custom naming function for anonymous layers (`@import 'baz.css' layer;`).  
 This function gets `(index, rootFilename)` arguments and should return a unique string.
 
-This option only influences imports without a layer name.
+This option only influences imports without a layer name.  
 Without this option the plugin will warn on anonymous layers.
 
+#### `warnOnEmpty`
+
+Type: `Boolean`  
+Default: `true`
+
+By default `postcss-import` warns when an empty file is imported.  
+Set this option to `false` to disable this warning.
+
 #### Example with some options
 
 ```js
diff --git a/index.js b/index.js
index 1871a639..12ebe824 100755
--- a/index.js
+++ b/index.js
@@ -20,6 +20,7 @@ function AtImport(options) {
     plugins: [],
     addModulesDirectories: [],
     nameLayer: null,
+    warnOnEmpty: true,
     ...options,
   }
 
diff --git a/lib/parse-styles.js b/lib/parse-styles.js
index 4c3c893f..49a54286 100644
--- a/lib/parse-styles.js
+++ b/lib/parse-styles.js
@@ -170,7 +170,7 @@ async function loadImportContent(
 
   const content = await options.load(filename, options)
 
-  if (content.trim() === "") {
+  if (content.trim() === "" && options.warnOnEmpty) {
     result.warn(`${filename} is empty`, { node: atRule })
     return
   }
diff --git a/test/helpers/check-fixture.js b/test/helpers/check-fixture.js
index 480d04b0..de667398 100644
--- a/test/helpers/check-fixture.js
+++ b/test/helpers/check-fixture.js
@@ -39,5 +39,13 @@ module.exports = function (t, file, opts, postcssOpts, warnings) {
           `unexpected warning: "${warning.text}"`
         )
       })
+
+      t.is(
+        warnings.length,
+        result.warnings().length,
+        `expected ${warnings.length} warning(s), got ${
+          result.warnings().length
+        }`
+      )
     })
 }
diff --git a/test/import.js b/test/import.js
index ea4a9c1f..f13d921f 100644
--- a/test/import.js
+++ b/test/import.js
@@ -130,6 +130,13 @@ test(
   [`${path.resolve("test/fixtures/imports/empty.css")} is empty`]
 )
 
+test(
+  "should be able to disable warnings for empty files",
+  checkFixture,
+  "empty-and-useless",
+  { path: "test/fixtures/imports", warnOnEmpty: false }
+)
+
 test("should work with no styles without throwing an error", t => {
   return postcss()
     .use(atImport())