Skip to content

Commit

Permalink
add warnOnEmpty plugin option (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke committed Aug 1, 2023
1 parent 3a2e231 commit c02c6c8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
16 changes: 12 additions & 4 deletions README.md
Expand Up @@ -125,7 +125,7 @@ Checkout the [tests](test) for more examples.

### Options

### `filter`
#### `filter`
Type: `Function`
Default: `() => true`

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -20,6 +20,7 @@ function AtImport(options) {
plugins: [],
addModulesDirectories: [],
nameLayer: null,
warnOnEmpty: true,
...options,
}

Expand Down
2 changes: 1 addition & 1 deletion lib/parse-styles.js
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions test/helpers/check-fixture.js
Expand Up @@ -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
}`
)
})
}
7 changes: 7 additions & 0 deletions test/import.js
Expand Up @@ -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())
Expand Down

0 comments on commit c02c6c8

Please sign in to comment.