Skip to content
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
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
* skipGlobalSelectors?: boolean
* }>}
*/
const prefixPlugin = (options = {}) => {
const prefixPlugin = options => {
// If options isn't set or prefix not provided, do nothing.
if (!options || !isObj(options) || !options.prefix) {
return {
postcssPlugin: 'postcss-prefix-selector',
};
}
Comment thread
RadValentin marked this conversation as resolved.

const prefix = options.prefix;
const prefixWithSpace = /\s+$/.test(prefix) ? prefix : `${prefix} `;
const ignoreFiles = options.ignoreFiles ? [].concat(options.ignoreFiles) : [];
Expand Down Expand Up @@ -93,6 +100,10 @@ function excludeSelector(selector, excludeArr) {
});
}

function isObj(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}

prefixPlugin.postcss = true;

module.exports = prefixPlugin;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"scripts": {
"test": "mocha",
"test-cov": "nyc mocha",
"test:cov": "nyc mocha",
"build:types": "tsc --project tsconfig.types.json",
"lint": "prettier --write '**/*.{js,css}'",
"prepack": "npm run build:types",
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ const fs = require('fs');
const prefixer = require('../index.js');
const postcssNested = require('postcss-nested');

it('should not make changes if no options are set', function () {
const out = postcss().use(prefixer()).process(getFixtureContents('single-selector.css')).css;
const expected = getFixtureContents('single-selector.css');

assert.equal(out, expected);
});

it('should not make changes if no prefix is set', function () {
const out = postcss().use(prefixer({})).process(getFixtureContents('single-selector.css')).css;
const expected = getFixtureContents('single-selector.css');

assert.equal(out, expected);
});

it('should prefix a selector', () => {
const out = postcss()
.use(
Expand Down