Skip to content

Add options to ignore specific at-rules and properties #58

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 4.1.0 - 2024-10-04
* Add `ignoredAtRules` option.
* Add `ignoredProps` option.

# 4.0.2 - 2020-11-08
* Update to PostCSS 8.
* Remove `glob` option.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-functions",
"version": "4.0.2",
"version": "4.1.0",
"description": "PostCSS plugin for exposing JavaScript functions",
"main": "dest/index.js",
"scripts": {
Expand Down
30 changes: 30 additions & 0 deletions src/__tests__/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ test(
}
);

test(
'should skip ignored props',
testFixture,
'a{foo:bar()}',
null,
{
functions: {
'bar': function () {
return 'baz';
}
},
ignoredProps: ['foo']
}
);

test(
'should accept deferred functions',
testFixture,
Expand Down Expand Up @@ -104,6 +119,21 @@ test(
}
);

test(
'should skip ignored at-rules',
testFixture,
'@foo bar(){bat:qux}',
null,
{
functions: {
'bar': function () {
return 'baz';
}
},
ignoredAtRules: ['foo']
}
);

test(
'should invoke a function in a rule',
testFixture,
Expand Down
10 changes: 6 additions & 4 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { transformAtRule, transformDecl, transformRule } from './lib/transformer.mjs';

function plugin(opts = {}) {
const functions = opts.functions || {};
opts.functions = opts.functions || {}
opts.ignoredAtRules = opts.ignoredAtRules || []
opts.ignoredProps = opts.ignoredProps || []

return {
postcssPlugin: 'postcss-functions',
AtRule(node) {
return transformAtRule(node, functions);
return transformAtRule(node, opts);
},
Declaration(node) {
return transformDecl(node, functions);
return transformDecl(node, opts);
},
Rule(node) {
return transformRule(node, functions);
return transformRule(node, opts);
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/lib/transformer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,23 @@ function extractArgs(nodes, functions) {
});
}

export function transformDecl(node, functions) {
export function transformDecl(node, { functions, ignoredProps }) {
if (ignoredProps.includes(node.prop)) return node;

return then(transformString(node.value, functions), value => {
node.value = value;
});
}

export function transformAtRule(node, functions) {
export function transformAtRule(node, { functions, ignoredAtRules }) {
if (ignoredAtRules.includes(node.name)) return node

return then(transformString(node.params, functions), value => {
node.params = value;
});
}

export function transformRule(node, functions) {
export function transformRule(node, { functions }) {
return then(transformString(node.selector, functions), value => {
node.selector = value;
});
Expand Down