Skip to content

Commit

Permalink
First version of this plugin with new option
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasferreira committed Apr 28, 2023
1 parent 14a4804 commit d159bd0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,18 @@ Checkout the [tests](test) for more examples.

### Options

### `filter`
#### `anywhereImport`

Type: `Boolean`
Default: `true`

Allow this PostCSS plugin to consider any `@import` mentions and using anywhere/anyplace
inside your .css files. If you want to regret to default version of this plugin
like [postcss-import](https://github.com/postcss/postcss-import) turn this `anywhereImport`
option to `false`.

#### `filter`

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

Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function AtImport(options) {
options = {
root: process.cwd(),
path: [],
anywhereImport: true,
skipDuplicates: true,
resolve: resolveId,
load: loadContent,
Expand Down Expand Up @@ -218,7 +219,9 @@ function AtImport(options) {
}

function parseStyles(result, styles, options, state, media, layer) {
const statements = parseStatements(result, styles)
const statements = parseStatements(result, styles, {
anywhereImport: options.anywhereImport || false,
})

return Promise.resolve(statements)
.then(stmts => {
Expand Down
10 changes: 6 additions & 4 deletions lib/parse-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ function split(params, start) {
return list
}

module.exports = function (result, styles) {
module.exports = function (result, styles, options) {
const statements = []
let nodes = []

styles.each(node => {
let stmt
if (node.type === "atrule") {
if (node.name === "import") stmt = parseImport(result, node)
if (node.name === "import") stmt = parseImport(result, node, options)
else if (node.name === "media") stmt = parseMedia(result, node)
else if (node.name === "charset") stmt = parseCharset(result, node)
}
Expand Down Expand Up @@ -82,9 +82,11 @@ function parseCharset(result, atRule) {
}
}

function parseImport(result, atRule) {
function parseImport(result, atRule, options) {
options = { anywhereImport: true, ...options }

let prev = atRule.prev()
if (prev) {
if (!options.anywhereImport && prev) {
do {
if (
prev.type !== "comment" &&
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"ci": "eslint . && ava",
"lint": "eslint . --fix",
"pretest": "npm run lint",
"test": "ava"
"test": "ava --verbose"
},
"eslintConfig": {
"extends": "eslint-config-problems",
Expand Down
29 changes: 28 additions & 1 deletion test/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const postcss = require("postcss")
// plugin
const atImport = require("..")

const processor = postcss().use(atImport())
const processor = postcss().use(
atImport({
anywhereImport: false,
})
)

test("should warn when not @charset and not @import statement before", t => {
return Promise.all([
Expand Down Expand Up @@ -45,6 +49,29 @@ test("should warn about all imports after some other CSS declaration", t => {
})
})

test("should NOT warn about all imports after some other CSS declaration [anywhereImport = true]", t => {
return postcss()
.use(
atImport({
anywhereImport: true,
})
)
.process(
`
x {}
z {}
@import "bar.css";
@import "foo.css";
`,
{ from: "test/fixtures/imports/baz.css" }
)
.then(result => {
const warnings = result.warnings()
t.is(warnings.length, 0)
})
})

test("should warn if non-empty @layer before @import", t => {
return processor
.process(`@layer { a {} } @import "a.css";`, { from: undefined })
Expand Down

0 comments on commit d159bd0

Please sign in to comment.