diff --git a/index.js b/index.js index c331764..429a496 100644 --- a/index.js +++ b/index.js @@ -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', + }; + } + const prefix = options.prefix; const prefixWithSpace = /\s+$/.test(prefix) ? prefix : `${prefix} `; const ignoreFiles = options.ignoreFiles ? [].concat(options.ignoreFiles) : []; @@ -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; diff --git a/package.json b/package.json index 960a2fe..0a1d5b3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/test.js b/test/test.js index 66a7ad5..9c369aa 100644 --- a/test/test.js +++ b/test/test.js @@ -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(