{"name":"postcss-prefix-selector","version":"2.2.0","keywords":["postcss","prefix","selectors","postcss-plugin","css","selector","plugin"],"author":{"url":"http://jongleberry.com","name":"Jonathan Ong","email":"me@jongleberry.com"},"license":"MIT","_id":"postcss-prefix-selector@2.2.0","maintainers":[{"name":"valentin-radulescu-hs","email":"radulescu.valentin@gmail.com"}],"contributors":[{"url":"https://valentin.io","name":"Valentin Radulescu","email":"hi@valentin.io"}],"homepage":"https://github.com/RadValentin/postcss-prefix-selector","bugs":{"url":"https://github.com/RadValentin/postcss-prefix-selector/issues"},"dist":{"shasum":"3b7846f011cabbf21733e9b94a52484f11d506c6","tarball":"http://registry-npm.ywwl.com/postcss-prefix-selector/download/postcss-prefix-selector-2.2.0.tgz","fileCount":5,"integrity":"sha512-pGKK57v2SXKo5CD41tAnFh82Y+cxvS3lgTtt7e2YatH1nS3ADTI+U3Lk/NfG+xBz9fyU0KAYoBs1YVwqmGZ9nA==","signatures":[{"sig":"MEUCICluUr+Nykd5CQfQBtX76opHElbiEQneShl5sP+IJirEAiEAzdIFAxXh0EFdrUW9V6LsTXClT48wjmvv3memtyXwVIc=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":13753,"size":4696,"noattachment":false,"key":"/postcss-prefix-selector/-/postcss-prefix-selector-2.2.0.tgz"},"types":"index.d.ts","gitHead":"03e6e328b30c65c7869f5947741582aa601f79d2","scripts":{"lint":"prettier --write '**/*.{js,css}'","test":"mocha","prepack":"npm run build:types","prepare":"husky","test-cov":"nyc mocha","build:types":"tsc --project tsconfig.types.json"},"_npmUser":{"name":"valentin-radulescu-hs","email":"radulescu.valentin@gmail.com"},"prettier":{"endOfLine":"auto","printWidth":100,"arrowParens":"avoid","singleQuote":true,"trailingComma":"es5"},"repository":{"url":"git+https://github.com/RadValentin/postcss-prefix-selector.git","type":"git"},"_npmVersion":"11.16.0","description":"Prefix all CSS rules with a selector","directories":{},"lint-staged":{"*.{js,css}":"prettier --write"},"_nodeVersion":"24.18.0","_hasShrinkwrap":false,"devDependencies":{"nyc":"^18.0.0","husky":"^9.1.7","mocha":"^11.8.0","postcss":"^8.0.0","prettier":"^3.9.6","typescript":"^5.9.2","lint-staged":"^17.3.0","mocha-multi":"^1.1.7","postcss-nested":"^7.0.2","mocha-junit-reporter":"^2.2.1"},"peerDependencies":{"postcss":"^8.0.0"},"_npmOperationalInternal":{"tmp":"tmp/postcss-prefix-selector_2.2.0_1788173330955_0.7871592166191577","host":"s3://npm-registry-packages-npm-production"},"readme":"# postcss-prefix-selector\r\n\r\n[![NPM version][npm-image]][npm-url]\r\n[![Test coverage][coveralls-image]][coveralls-url]\r\n[![License][license-image]][license-url]\r\n[![Downloads][downloads-image]][downloads-url]\r\n[![Gitpod ready-to-code][gitpod-image]][gitpod-url]\r\n\r\n> Prefix every CSS selector with a custom namespace `.a => .prefix .a`\r\n\r\n## Table of Contents\r\n\r\n- [Install](#install)\r\n- [Usage with PostCSS](#usage-with-postcss)\r\n- [Usage with Webpack](#usage-with-webpack)\r\n- [Usage with Vite](#usage-with-vite)\r\n- [Options](#options)\r\n- [Maintainer](#maintainer)\r\n- [Contribute](#contribute)\r\n- [License](#license)\r\n\r\n## Install\r\n\r\n```console\r\n$ npm install postcss-prefix-selector\r\n```\r\n\r\n## Usage with PostCSS\r\n\r\nA prefix is added before most selectors. Below is an example of how CSS will be transformed by adding a prefix called `.namespace`.\r\n\r\n```js\r\nconst prefixer = require('postcss-prefix-selector')\r\n\r\n// css to be processed\r\nconst css = fs.readFileSync(\"input.css\", \"utf8\")\r\n\r\nconst out = postcss().use(prefixer({\r\n  prefix: '.namespace',\r\n  exclude: ['.c'],\r\n})).process(css).css\r\n```\r\n\r\n```css\r\n/* Input */\r\n.a, .b {\r\n  color: aqua;\r\n}\r\n\r\n.c {\r\n  color: coral;\r\n}\r\n\r\n/* Output */\r\n.namespace .a, .namespace .b {\r\n  color: aqua;\r\n}\r\n\r\n.c {\r\n  color: coral;\r\n}\r\n```\r\n\r\nPlease note that global selectors (`html`, `body`, `:root`) cannot be prefixed so instead they will be replaced with the prefix. This behaviour can be disabled with the `skipGlobalSelectors` option.\r\n\r\n```css\r\n/* Input */\r\n:root { --bs-blue:#0d6efd; }\r\nhtml { padding: 0; }\r\nbody { margin: 0; }\r\n\r\n/* Output */\r\n.namespace { --bs-blue:#0d6efd; }\r\n.namespace { padding: 0; }\r\n.namespace { margin: 0; }\r\n```\r\n\r\nIt's also possible to customize the way prefixing is done by defining a transform function:\r\n\r\n```js\r\nconst out = postcss().use(prefixer({\r\n  prefix: '.namespace',\r\n  // Optional transform callback for case-by-case overrides\r\n  transform: function (prefix, selector, prefixedSelector, filePath, rule) {\r\n    if (selector === 'body') {\r\n      return 'body' + prefix;\r\n    } else {\r\n      return prefixedSelector;\r\n    }\r\n  }\r\n})).process(css).css\r\n```\r\n\r\n```css\r\n/* Input */\r\nbody {\r\n  background: red;\r\n}\r\n\r\n/* Output */\r\nbody.namespace {\r\n  background: red;\r\n}\r\n```\r\n\r\n## Usage with webpack\r\n\r\nUse it like you'd use any other PostCSS plugin. If you also have `autoprefixer` in your webpack config then make sure that `postcss-prefix-selector` is called first. This is needed to avoid running the prefixer twice on both standard selectors and vendor specific ones (ex: `@keyframes` and `@webkit-keyframes`).\r\n\r\n```js\r\nmodule: {\r\n  rules: [{\r\n    test: /\\.css$/,\r\n    use: [\r\n      require.resolve('style-loader'),\r\n      require.resolve('css-loader'),\r\n      {\r\n        loader: require.resolve('postcss-loader'),\r\n        options: {\r\n          postcssOptions: {\r\n            plugins: {\r\n              \"postcss-prefix-selector\": {\r\n                prefix: '.my-prefix',\r\n                transform(prefix, selector, prefixedSelector, filePath, rule) {\r\n                  if (selector.match(/^(html|body)/)) {\r\n                    return selector.replace(/^([^\\s]*)/, `$1 ${prefix}`);\r\n                  }\r\n                  \r\n                  if (filePath.match(/node_modules/)) {\r\n                    return selector; // Do not prefix styles imported from node_modules\r\n                  }\r\n                  \r\n                  const annotation = rule.prev();\r\n                  if (annotation?.type === 'comment' && annotation.text.trim() === 'no-prefix') {\r\n                    return selector; // Do not prefix style rules that are preceded by: /* no-prefix */\r\n                  }\r\n\r\n                  return prefixedSelector;\r\n                },\r\n              },\r\n              autoprefixer: {\r\n                browsers: ['last 4 versions']\r\n              }\r\n            }\r\n          }\r\n        }\r\n      }\r\n    ]\r\n  }]\r\n}\r\n```\r\n\r\n\r\n## Usage with Vite\r\n\r\nFollowing the same way of Webpack but for Vite:\r\n\r\n```js\r\nimport prefixer from 'postcss-prefix-selector';\r\nimport autoprefixer from 'autoprefixer';\r\n\r\n...\r\n\r\nexport default defineConfig({\r\n...\r\n  css: {\r\n    postcss: {\r\n      plugins: [\r\n        prefixer({\r\n          prefix: '.my-prefix',\r\n          transform(prefix, selector, prefixedSelector, filePath, rule) {\r\n            if (selector.match(/^(html|body)/)) {\r\n              return selector.replace(/^([^\\s]*)/, `$1 ${prefix}`);\r\n            }\r\n\r\n            if (filePath.match(/node_modules/)) {\r\n              return selector; // Do not prefix styles imported from node_modules\r\n            }\r\n\r\n            const annotation = rule.prev();\r\n            if (annotation?.type === 'comment' && annotation.text.trim() === 'no-prefix') {\r\n              return selector; // Do not prefix style rules that are preceded by: /* no-prefix */\r\n            }\r\n\r\n            return prefixedSelector;\r\n          },\r\n        }),\r\n\r\n        autoprefixer({}) // add options if needed\r\n      ],\r\n    }\r\n  },\r\n...\r\n});  \r\n```\r\n\r\n## Options\r\n\r\n| Name | Type | Description |\r\n|-|-|-|\r\n| `prefix` | `string` | This string is added before every CSS selector |\r\n| `exclude` | `string[]` or `RegExp[]` | It's possible to avoid prefixing some selectors by passing an array of selectors |\r\n| `transform` | `Function` | In cases where you may want to use the prefix differently for different selectors, it is possible to pass in a custom transform method |\r\n| `ignoreFiles` | `string[]` or `RegExp[]` | List of ignored files for processing |\r\n| `includeFiles` | `string[]` or `RegExp[]` | List of included files for processing |\r\n| `skipGlobalSelectors` | `boolean` |  When enabled, global selectors (`html`, `body`, `root`) won't be modified by the prefixer. Default: `false`. | \r\n\r\n## Maintainer\r\n\r\nThis project was originally created by [@jongleberry](https://github.com/jonathanong) and is being maintained by [@RadValentin](https://github.com/RadValentin). If you have any questions, feel free to ping the latter.\r\n\r\n## Contribute\r\n\r\nPlease contribute! If you have any questions or bugs, [open an issue](https://github.com/RadValentin/postcss-prefix-selector/issues/new). Or, open a pull request with a fix.\r\n\r\nThis project uses Mocha. If you submit a PR, please add appropriate tests and make sure that everything is green for your branch.\r\n\r\n## License\r\n\r\n[MIT](LICENSE) © 2015-2024 Jonathan Ong.\r\n\r\n[gitpod-image]: https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod\r\n[gitpod-url]: https://gitpod.io/#https://github.com/RadValentin/postcss-prefix-selector\r\n[npm-image]: https://img.shields.io/npm/v/postcss-prefix-selector.svg?style=flat-square\r\n[npm-url]: https://npmjs.org/package/postcss-prefix-selector\r\n[coveralls-image]: https://img.shields.io/coveralls/jonathanong/postcss-prefix-selector.svg?style=flat-square\r\n[coveralls-url]: https://coveralls.io/r/jonathanong/postcss-prefix-selector\r\n[license-image]: http://img.shields.io/npm/l/postcss-prefix-selector.svg?style=flat-square\r\n[license-url]: LICENSE\r\n[downloads-image]: http://img.shields.io/npm/dm/postcss-prefix-selector.svg?style=flat-square\r\n[downloads-url]: https://npmjs.org/package/postcss-prefix-selector\r\n","publish_time":1788173331088,"_cnpm_publish_time":1788173331088,"dist-tags":{"latest":"2.2.1"}}