Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add warnOnEmpty plugin option #541

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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