{"name":"postcss-loader","version":"8.2.1","keywords":["css","postcss","postcss-runner","webpack","webpack-loader"],"author":{"name":"Andrey Sitnik","email":"andrey@sitnik.ru"},"license":"MIT","_id":"postcss-loader@8.2.1","maintainers":[{"name":"evilebottnawi","email":"sheo13666q@gmail.com"},{"name":"sokra","email":"tobias.koppers@googlemail.com"},{"name":"jhnns","email":"mail@johannesewald.de"},{"name":"15000621931","email":"784487301@qq.com"},{"name":"ev1stensberg","email":"evenstensberg@gmail.com"},{"name":"__hai","email":"haijie0619@gmail.com"},{"name":"ai","email":"andrey@sitnik.ru"}],"homepage":"https://github.com/webpack/postcss-loader","bugs":{"url":"https://github.com/webpack/postcss-loader/issues"},"dist":{"shasum":"c3d9b35498af906fe6c25eb62583c06f619f92fc","tarball":"http://registry-npm.ywwl.com/postcss-loader/download/postcss-loader-8.2.1.tgz","fileCount":8,"integrity":"sha512-k98jtRzthjj3f76MYTs9JTpRqV1RaaMhEU0Lpw9OTmQZQdppg4B30VZ74BojuBHt3F4KyubHJoXCMUeM8Bqeow==","signatures":[{"sig":"MEYCIQCwsFP2vq9ZZtXazsMy7e/VQX14hrGkeNwC8mAN0RrwygIhAOA6D8pZ4wwhMMJwP32kFLxT139TiKFn/3OOfaEzo/fc","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":51743,"size":12918,"noattachment":false,"key":"/postcss-loader/-/postcss-loader-8.2.1.tgz"},"main":"dist/cjs.js","engines":{"node":">= 18.12.0"},"funding":{"url":"https://opencollective.com/webpack","type":"opencollective"},"gitHead":"583677e2500b33ed273e1562c89daf07a188eb94","scripts":{"fix":"npm run fix:code && npm run fmt","fmt":"npm run lint:prettier -- --write","lint":"npm-run-all -l -p \"lint:**\"","test":"npm run test:coverage","build":"cross-env NODE_ENV=production babel src -d dist --copy-files","clean":"del-cli dist","start":"npm run build -- -w","prepare":"husky && npm run build","pretest":"npm run lint","release":"standard-version","fix:code":"npm run lint:code -- --fix","prebuild":"npm run clean","security":"npm audit --omit=dev","lint:code":"eslint --cache .","test:only":"cross-env NODE_ENV=test jest","commitlint":"commitlint --from=main","test:watch":"npm run test:only -- --watch","lint:prettier":"prettier --cache --list-different .","lint:spelling":"cspell --cache --no-must-find-files --quiet \"**/*.*\"","test:coverage":"npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage"},"_npmUser":{"name":"evilebottnawi","email":"sheo13666q@gmail.com"},"repository":{"url":"git+https://github.com/webpack/postcss-loader.git","type":"git"},"_npmVersion":"11.6.2","description":"PostCSS loader for webpack","directories":{},"_nodeVersion":"24.11.1","dependencies":{"jiti":"^2.5.1","semver":"^7.6.2","cosmiconfig":"^9.0.0"},"_hasShrinkwrap":false,"devDependencies":{"del":"^7.1.0","jest":"^30.0.5","less":"^4.2.0","sass":"^1.77.6","husky":"^9.1.3","memfs":"^4.9.3","midas":"^2.0.3","cspell":"^9.6.2","eslint":"^9.32.0","cssnano":"^7.1.2","del-cli":"^6.0.0","postcss":"^8.4.39","sugarss":"^5.0.1","ts-jest":"^29.4.0","ts-node":"^10.9.1","webpack":"^5.92.1","prettier":"^3.3.2","cross-env":"^7.0.3","@babel/cli":"^7.24.7","babel-jest":"^30.0.5","postcss-js":"^4.0.1","strip-ansi":"^7.1.0","@babel/core":"^7.24.7","less-loader":"^12.2.0","lint-staged":"^16.2.7","npm-run-all":"^4.1.5","sass-loader":"^14.2.1","postcss-short":"^5.0.0","postcss-import":"^16.1.0","postcss-nested":"^6.0.1","@commitlint/cli":"^20.3.1","standard-version":"^9.3.2","@babel/preset-env":"^7.24.7","postcss-load-config":"^5.1.0","eslint-config-webpack":"^4.9.1","postcss-dark-theme-class":"^1.3.0","@commitlint/config-conventional":"^20.3.1"},"peerDependencies":{"postcss":"^7.0.0 || ^8.0.1","webpack":"^5.0.0","@rspack/core":"0.x || ^1.0.0 || ^2.0.0-0"},"peerDependenciesMeta":{"webpack":{"optional":true},"@rspack/core":{"optional":true}},"_npmOperationalInternal":{"tmp":"tmp/postcss-loader_8.2.1_1771184678102_0.04612337310593917","host":"s3://npm-registry-packages-npm-production"},"readme":"<div align=\"center\">\n  <img \n    width=\"180\" \n    height=\"180\" \n    hspace=\"10\"\n    alt=\"PostCSS Logo\"\n    src=\"https://api.postcss.org/logo.svg\">\n  <a href=\"https://github.com/webpack/webpack\">\n    <img \n      width=\"200\" \n      height=\"200\" \n      hspace=\"10\"\n      src=\"https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg\">\n  </a>\n  <div align=\"center\">\n    <a href=\"https://evilmartians.com/?utm_source=postcss\">\n      <img \n        src=\"https://evilmartians.com/badges/sponsored-by-evil-martians.svg\"\n        alt=\"Sponsored by Evil Martians\" \n        width=\"236\" \n        height=\"54\" \n        vspace=\"10\">\n    </a>\n  </div>\n</div>\n\n[![npm][npm]][npm-url]\n[![node][node]][node-url]\n[![tests][tests]][tests-url]\n[![coverage][cover]][cover-url]\n[![size][size]][size-url]\n\nWebpack discussion: [![discussion][discussion]][discussion-url]\n\nPostCSS chat: [![chat-postcss][chat-postcss]][chat-postcss-url]\n\n# postcss-loader\n\nA loader to process CSS using [`PostCSS`](https://github.com/postcss/postcss).\n\n## Getting Started\n\nYou need webpack v5 to use the latest version. For Webpack v4, you have to install postcss-loader v4.\n\nTo begin, you'll need to install `postcss-loader` and `postcss`:\n\n```console\nnpm install --save-dev postcss-loader postcss\n```\n\nor\n\n```console\nyarn add -D postcss-loader postcss\n```\n\nor\n\n```console\npnpm add -D postcss-loader postcss\n```\n\nThen add the loader to your `webpack` configuration. For example:\n\n> In the following configuration the plugin [`postcss-preset-env`](https://github.com/csstools/postcss-preset-env) is used, which is not installed by default.\n\n**file.js**\n\n```js\nimport css from \"file.css\";\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"postcss-loader\",\n            options: {\n              postcssOptions: {\n                plugins: [\n                  [\n                    \"postcss-preset-env\",\n                    {\n                      // Options\n                    },\n                  ],\n                ],\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\nAlternative use with [config files](#config):\n\n**postcss.config.js**\n\n```js\nmodule.exports = {\n  plugins: [\n    [\n      \"postcss-preset-env\",\n      {\n        // Options\n      },\n    ],\n  ],\n};\n```\n\nThe loader **automatically** searches for configuration files.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\"style-loader\", \"css-loader\", \"postcss-loader\"],\n      },\n    ],\n  },\n};\n```\n\nFinally, run `webpack` using the method you normally use (e.g., via CLI or an npm script).\n\n## Options\n\n- [`execute`](#execute)\n- [`postcssOptions`](#postcssOptions)\n- [`sourceMap`](#sourcemap)\n- [`implementation`](#implementation)\n\n### `execute`\n\nType:\n\n```ts\ntype execute = boolean;\n```\n\nDefault: `undefined`\n\nEnable PostCSS parser support for `CSS-in-JS`.\nIf you use JS styles the [`postcss-js`](https://github.com/postcss/postcss-js) parser, add the `execute` option.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.style.js$/,\n        use: [\n          \"style-loader\",\n          { loader: \"css-loader\" },\n          {\n            loader: \"postcss-loader\",\n            options: {\n              postcssOptions: { parser: \"postcss-js\" },\n              execute: true,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `postcssOptions`\n\nSee the file [`./src/config.d.ts`](./src/config.d.ts).\n\nType:\n\n```ts\nimport { type Config as PostCSSConfig } from \"postcss-load-config\";\nimport { type LoaderContext } from \"webpack\";\n\ntype PostCSSLoaderContext = LoaderContext<PostCSSConfig>;\n\ninterface PostCSSLoaderAPI {\n  mode: PostCSSLoaderContext[\"mode\"];\n  file: PostCSSLoaderContext[\"resourcePath\"];\n  webpackLoaderContext: PostCSSLoaderContext;\n  env: PostCSSLoaderContext[\"mode\"];\n  options: PostCSSConfig;\n}\n\nexport type PostCSSLoaderOptions =\n  | PostCSSConfig\n  | ((api: PostCSSLoaderAPI) => PostCSSConfig);\n```\n\nDefault: `undefined`\n\nAllows you to set [`PostCSS options`](https://postcss.org/api/#processoptions) and plugins.\n\nAll `PostCSS` options are supported.\nThere is the special `config` option for config files. How it works and how it can be configured is described below.\n\nWe recommend do not specify `from`, `to` and `map` options, because this can lead to wrong path in source maps.\nIf you need source maps please use the [`sourcemap`](#sourcemap) option instead.\n\nFor large projects, to optimize performance of the loader, it is better to provide `postcssOptions` in loader config and specify `config: false`.\nThis approach removes the need to lookup and load external config files multiple times during compilation.\n\n#### `object`\n\nSetup `plugins`:\n\n**webpack.config.js** (**recommended**)\n\n```js\nconst myOtherPostcssPlugin = require(\"postcss-my-plugin\");\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.sss$/i,\n        loader: \"postcss-loader\",\n        options: {\n          postcssOptions: {\n            plugins: [\n              \"postcss-import\",\n              [\"postcss-short\", { prefix: \"x\" }],\n              require.resolve(\"my-postcss-plugin\"),\n              myOtherPostcssPlugin({ myOption: true }),\n              // Deprecated and will be removed in the next major release\n              { \"postcss-nested\": { preserveEmpty: true } },\n            ],\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n**webpack.config.js** (**deprecated**, will be removed in the next major release)\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.sss$/i,\n        loader: \"postcss-loader\",\n        options: {\n          postcssOptions: {\n            plugins: { \"postcss-import\": {}, \"postcss-short\": { prefix: \"x\" } },\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nSetup `syntax`:\n\n**webpack.config.js**\n\n<!-- eslint-skip -->\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.sss$/i,\n        loader: \"postcss-loader\",\n        options: {\n          postcssOptions: {\n            // Can be `string`\n            syntax: \"sugarss\",\n            // Can be `object`\n            syntax: require(\"sugarss\"),\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nSetup `parser`:\n\n**webpack.config.js**\n\n<!-- eslint-skip -->\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.sss$/i,\n        loader: \"postcss-loader\",\n        options: {\n          postcssOptions: {\n            // Can be `string`\n            parser: \"sugarss\",\n            // Can be `object`\n            parser: require(\"sugarss\"),\n            // Can be `function`\n            parser: require(\"sugarss\").parse,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nSetup `stringifier`:\n\n**webpack.config.js**\n\n<!-- eslint-skip -->\n\n```js\nconst Midas = require(\"midas\");\nconst midas = new Midas();\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.sss$/i,\n        loader: \"postcss-loader\",\n        options: {\n          postcssOptions: {\n            // Can be `string`\n            stringifier: \"sugarss\",\n            // Can be `object`\n            stringifier: require(\"sugarss\"),\n            // Can be `function`\n            stringifier: midas.stringifier,\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n#### `function`\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.(css|sss)$/i,\n        loader: \"postcss-loader\",\n        options: {\n          postcssOptions: (loaderContext) => {\n            if (/\\.sss$/.test(loaderContext.resourcePath)) {\n              return {\n                parser: \"sugarss\",\n                plugins: [\n                  [\"postcss-short\", { prefix: \"x\" }],\n                  \"postcss-preset-env\",\n                ],\n              };\n            }\n\n            return {\n              plugins: [\n                [\"postcss-short\", { prefix: \"x\" }],\n                \"postcss-preset-env\",\n              ],\n            };\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n#### `config`\n\nType:\n\n```ts\ntype config = boolean | string;\n```\n\nDefault: `true`\n\nAllows you to set options using config files.\nOptions specified in the config file are combined with options passed to the loader, the loader options overwrite options from config.\n\n##### Config Files\n\nThe loader will search up the directory tree for configuration in the following places:\n\n- A `postcss` property in `package.json`\n- A `.postcssrc` file in JSON or YAML format\n- A `.postcssrc.json`, `.postcssrc.yaml`, `.postcssrc.yml`, `.postcssrc.js`, or `.postcssrc.cjs` file\n- A `postcss.config.js` or `postcss.config.cjs` CommonJS module exporting an object (**recommended**)\n\n##### Examples of Config Files\n\nUsing `object` notation:\n\n**postcss.config.js** (**recommend**)\n\n```js\nmodule.exports = {\n  // You can specify any options from https://postcss.org/api/#processoptions here\n  // parser: 'sugarss',\n  plugins: [\n    // Plugins for PostCSS\n    [\"postcss-short\", { prefix: \"x\" }],\n    \"postcss-preset-env\",\n  ],\n};\n```\n\nUsing `function` notation:\n\n**postcss.config.js** (**recommend**)\n\n```js\nmodule.exports = (api) => {\n  // `api.file` - path to the file\n  // `api.mode` - `mode` value of webpack, please read https://webpack.js.org/configuration/mode/\n  // `api.webpackLoaderContext` - loader context for complex use cases\n  // `api.env` - alias `api.mode` for compatibility with `postcss-cli`\n  // `api.options` - the `postcssOptions` options\n\n  if (/\\.sss$/.test(api.file)) {\n    return {\n      // You can specify any options from https://postcss.org/api/#processoptions here\n      parser: \"sugarss\",\n      plugins: [\n        // Plugins for PostCSS\n        [\"postcss-short\", { prefix: \"x\" }],\n        \"postcss-preset-env\",\n      ],\n    };\n  }\n\n  return {\n    // You can specify any options from https://postcss.org/api/#processoptions here\n    plugins: [\n      // Plugins for PostCSS\n      [\"postcss-short\", { prefix: \"x\" }],\n      \"postcss-preset-env\",\n    ],\n  };\n};\n```\n\n**postcss.config.js** (**deprecated**, will be removed in the next major release)\n\n```js\nmodule.exports = {\n  // You can specify any options from https://postcss.org/api/#processoptions here\n  // parser: 'sugarss',\n  plugins: {\n    // Plugins for PostCSS\n    \"postcss-short\": { prefix: \"x\" },\n    \"postcss-preset-env\": {},\n  },\n};\n```\n\n##### Config Cascade\n\nYou can use different `postcss.config.js` files in different directories.\nConfig lookup starts from `path.dirname(file)` and walks the file tree upwards until a config file is found.\n\n```\n|– components\n| |– component\n| | |– index.js\n| | |– index.png\n| | |– style.css (1)\n| | |– postcss.config.js (1)\n| |– component\n| | |– index.js\n| | |– image.png\n| | |– style.css (2)\n|\n|– postcss.config.js (1 && 2 (recommended))\n|– webpack.config.js\n|\n|– package.json\n```\n\nAfter setting up your `postcss.config.js`, add `postcss-loader` to your `webpack.config.js`.\nYou can use it standalone or in conjunction with `css-loader` (recommended).\n\nUse `postcss-loader` **before** `css-loader` and `style-loader`, but **after** other preprocessor loaders like e.g `sass|less|stylus-loader`, if you use any (since [webpack loaders evaluate right to left/bottom to top](https://webpack.js.org/concepts/loaders/#configuration)).\n\n**webpack.config.js** (**recommended**)\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/,\n        use: [\n          \"style-loader\",\n          { loader: \"css-loader\", options: { importLoaders: 1 } },\n          \"postcss-loader\",\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### `boolean`\n\nEnables/Disables autoloading config.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"postcss-loader\",\n        options: { postcssOptions: { config: false } },\n      },\n    ],\n  },\n};\n```\n\n#### String\n\nAllows to specify the path to the config file.\n\n**webpack.config.js**\n\n```js\nconst path = require(\"node:path\");\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        loader: \"postcss-loader\",\n        options: {\n          postcssOptions: {\n            config: path.resolve(__dirname, \"custom.config.js\"),\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\n### `sourceMap`\n\nType:\n\n```ts\ntype sourceMap = boolean;\n```\n\nDefault: depends on the `compiler.devtool` value\n\nBy default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option.\nAll values enable source map generation except `eval` and `false` value.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          { loader: \"style-loader\" },\n          { loader: \"css-loader\", options: { sourceMap: true } },\n          { loader: \"postcss-loader\", options: { sourceMap: true } },\n          { loader: \"sass-loader\", options: { sourceMap: true } },\n        ],\n      },\n    ],\n  },\n};\n```\n\nAlternative setup:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  devtool: \"source-map\",\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          { loader: \"style-loader\" },\n          { loader: \"css-loader\" },\n          { loader: \"postcss-loader\" },\n          { loader: \"sass-loader\" },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### `implementation`\n\nType:\n\n```ts\ntype implementation = object;\n```\n\ntype of `implementation` should be the same as [postcss.d.ts](https://github.com/postcss/postcss/blob/main/lib/postcss.d.ts)\n\nDefault: `postcss`\n\nThe special `implementation` option determines which implementation of PostCSS to use. Overrides the locally installed `peerDependency` version of `postcss`.\n\n**This option is only really useful for downstream tooling authors to ease the PostCSS 7-to-8 transition.**\n\n#### `function`\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          { loader: \"style-loader\" },\n          { loader: \"css-loader\" },\n          {\n            loader: \"postcss-loader\",\n            options: { implementation: require(\"postcss\") },\n          },\n          { loader: \"sass-loader\" },\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### String\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          { loader: \"style-loader\" },\n          { loader: \"css-loader\" },\n          {\n            loader: \"postcss-loader\",\n            options: { implementation: require.resolve(\"postcss\") },\n          },\n          { loader: \"sass-loader\" },\n        ],\n      },\n    ],\n  },\n};\n```\n\n## Examples\n\n### SugarSS\n\n`SugarSS` is a whitespace-based syntax for PostCSS.\n\nYou'll need to install `sugarss`:\n\n```console\nnpm install --save-dev sugarss\n```\n\nUsing [`SugarSS`](https://github.com/postcss/sugarss) syntax.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.sss$/i,\n        use: [\n          \"style-loader\",\n          { loader: \"css-loader\", options: { importLoaders: 1 } },\n          {\n            loader: \"postcss-loader\",\n            options: { postcssOptions: { parser: \"sugarss\" } },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### Autoprefixer\n\nYou'll need to install `autoprefixer`:\n\n```console\nnpm install --save-dev autoprefixer\n```\n\nAutomatically add vendor prefixes to CSS rules using [`autoprefixer`](https://github.com/postcss/autoprefixer).\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          \"style-loader\",\n          { loader: \"css-loader\", options: { importLoaders: 1 } },\n          {\n            loader: \"postcss-loader\",\n            options: {\n              postcssOptions: {\n                plugins: [\n                  [\n                    \"autoprefixer\",\n                    {\n                      // Autoprefixer options (optional)\n                    },\n                  ],\n                ],\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n> [!WARNING]\n>\n> [`postcss-preset-env`](https://github.com/csstools/postcss-preset-env) includes [`autoprefixer`](https://github.com/postcss/autoprefixer), so adding it separately is not necessary if you already use the preset. More [information](https://github.com/csstools/postcss-preset-env#autoprefixer)\n\n### PostCSS Preset Env\n\nYou'll need to install `postcss-preset-env`:\n\n```console\nnpm install --save-dev postcss-preset-env\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          \"style-loader\",\n          { loader: \"css-loader\", options: { importLoaders: 1 } },\n          {\n            loader: \"postcss-loader\",\n            options: {\n              postcssOptions: {\n                plugins: [\n                  [\n                    \"postcss-preset-env\",\n                    {\n                      // Options\n                    },\n                  ],\n                ],\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n### CSS Modules\n\n> What are `CSS Modules`? Please [read here](https://github.com/webpack/css-loader#modules).\n\nNo additional options required on the `postcss-loader` side to support CSS Modules.\nTo make them work properly, either add the `css-loader`’s `importLoaders` option.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          \"style-loader\",\n          {\n            loader: \"css-loader\",\n            options: { modules: true, importLoaders: 1 },\n          },\n          \"postcss-loader\",\n        ],\n      },\n    ],\n  },\n};\n```\n\n### CSS-in-JS and [`postcss-js`](https://github.com/postcss/postcss-js)\n\nTo process styles written in JavaScript, you can use `postcss-js` as the parser.\n\nYou'll need to install `postcss-js`:\n\n```console\nnpm install --save-dev postcss-js\n```\n\nIf you want to process styles written in JavaScript, use the [`postcss-js`](https://github.com/postcss/postcss-js) parser.\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.style.js$/,\n        use: [\n          \"style-loader\",\n          { loader: \"css-loader\", options: { importLoaders: 2 } },\n          {\n            loader: \"postcss-loader\",\n            options: {\n              postcssOptions: { parser: \"postcss-js\" },\n              execute: true,\n            },\n          },\n          \"babel-loader\",\n        ],\n      },\n    ],\n  },\n};\n```\n\nAs result you will be able to write styles in the following way:\n\n```js\nimport colors from \"./styles/colors\";\n\nexport default {\n  \".menu\": { color: colors.main, height: 25, \"&_link\": { color: \"white\" } },\n};\n```\n\n> [!WARNING]\n>\n> If you are using Babel you need to do the following in order for the setup to work\n>\n> 1. Add [`babel-plugin-add-module-exports`](https://github.com/59naga/babel-plugin-add-module-exports) to your configuration.\n> 2. You need to have only one **default** export per style module.\n\n### Extract CSS\n\nTo extract CSS into separate files, use [`mini-css-extract-plugin`](https://github.com/webpack/mini-css-extract-plugin).\n\n**webpack.config.js**\n\n```js\nconst isProductionMode = process.env.NODE_ENV === \"production\";\n\nconst MiniCssExtractPlugin = require(\"mini-css-extract-plugin\");\n\nmodule.exports = {\n  mode: isProductionMode ? \"production\" : \"development\",\n  module: {\n    rules: [\n      {\n        test: /\\.css$/,\n        use: [\n          isProductionMode ? MiniCssExtractPlugin.loader : \"style-loader\",\n          \"css-loader\",\n          \"postcss-loader\",\n        ],\n      },\n    ],\n  },\n  plugins: [\n    new MiniCssExtractPlugin({\n      filename: isProductionMode ? \"[name].[contenthash].css\" : \"[name].css\",\n    }),\n  ],\n};\n```\n\n> 💡 Use this setup to extract and cache CSS in production while keeping fast style injection during development.\n\n### Emit assets\n\nTo emit an asset from PostCSS plugin to the webpack, need to add a message in `result.messages`.\n\nThe message should contain the following fields:\n\n- `type` = `asset` - Message type (require, should be equal `asset`)\n- `file` - file name (require)\n- `content` - file content (require)\n- `sourceMap` - sourceMap\n- `info` - asset info\n\n**webpack.config.js**\n\n```js\nconst postcssCustomPlugin = (opts = {}) => ({\n  postcssPlugin: \"postcss-custom-plugin\",\n  Once: (root, { result }) => {\n    result.messages.push({\n      type: \"asset\",\n      file: \"sprite.svg\",\n      content: \"<svg>...</svg>\",\n    });\n  },\n});\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"postcss-loader\",\n            options: { postcssOptions: { plugins: [postcssCustomPlugin()] } },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n> ℹ️ This allows your plugin to generate additional files as part of the build process, and Webpack will handle them like any other emitted asset.\n\n### Add dependencies, contextDependencies, buildDependencies, missingDependencies\n\nThe dependencies are necessary for webpack to understand when it needs to run recompilation on the changed files.\n\nThere are two way to add dependencies:\n\n1. (Recommended). The plugin may emit messages in `result.messages`.\n\nThe message should contain the following fields:\n\n- `type` = `dependency` - Message type (require, should be equal `dependency`, `context-dependency`, `build-dependency` or `missing-dependency`)\n- `file` - absolute file path (require)\n\n**webpack.config.js**\n\n```js\nconst path = require(\"node:path\");\n\nconst postcssCustomPlugin = (opts = {}) => ({\n  postcssPlugin: \"postcss-custom-plugin\",\n  Once: (root, { result }) => {\n    result.messages.push({\n      type: \"dependency\",\n      file: path.resolve(__dirname, \"path\", \"to\", \"file\"),\n    });\n  },\n});\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"postcss-loader\",\n            options: { postcssOptions: { plugins: [postcssCustomPlugin()] } },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n> 💡 You can use ready-made plugin [postcss-add-dependencies](https://www.npmjs.com/package/postcss-add-dependencies) to simplify this process.\n\n2. Pass `loaderContext` in plugin (for advanced setups).\n\n**webpack.config.js**\n\n```js\nconst path = require(\"node:path\");\n\nmodule.exports = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/i,\n        use: [\n          \"style-loader\",\n          \"css-loader\",\n          {\n            loader: \"postcss-loader\",\n            options: {\n              postcssOptions: {\n                config: path.resolve(__dirname, \"path/to/postcss.config.js\"),\n              },\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n> ⚠️ Use this approach only when managing dependencies via custom PostCSS configurations with dynamic imports or external files.\n\n**postcss.config.js**\n\nPass the `webpackLoaderContext` through the PostCSS `api` object:\n\n```js\nmodule.exports = (api) => ({\n  plugins: [\n    require(\"path/to/postcssCustomPlugin.js\")({\n      loaderContext: api.webpackLoaderContext,\n    }),\n  ],\n});\n```\n\n**postcssCustomPlugin.js**\n\nRegister a file dependency using `loaderContext.addDependency`:\n\n```js\nconst path = require(\"node:path\");\n\nconst postcssCustomPlugin = (opts = {}) => ({\n  postcssPlugin: \"postcss-custom-plugin\",\n  Once: (root, { result }) => {\n    opts.loaderContext.addDependency(\n      path.resolve(__dirname, \"path\", \"to\", \"file\"),\n    );\n  },\n});\n\npostcssCustomPlugin.postcss = true;\n\nmodule.exports = postcssCustomPlugin;\n```\n\n> ✅ This method is ideal when you want to dynamically declare dependencies without relying on result.messages, especially in more complex setups or shared plugin configurations.\n\n## Contributing\n\nWe welcome all contributions!\nIf you're new here, please take a moment to review our contributing guidelines before submitting issues or pull requests.\n\n[CONTRIBUTING](https://github.com/webpack/postcss-loader?tab=contributing-ov-file#contributing)\n\n## License\n\n[MIT](./LICENSE)\n\n[npm]: https://img.shields.io/npm/v/postcss-loader.svg\n[npm-url]: https://npmjs.com/package/postcss-loader\n[node]: https://img.shields.io/node/v/postcss-loader.svg\n[node-url]: https://nodejs.org\n[tests]: https://github.com/webpack/postcss-loader/workflows/postcss-loader/badge.svg\n[tests-url]: https://github.com/webpack/postcss-loader/actions\n[cover]: https://codecov.io/gh/webpack/postcss-loader/branch/main/graph/badge.svg\n[cover-url]: https://codecov.io/gh/webpack/postcss-loader\n[discussion]: https://img.shields.io/github/discussions/webpack/webpack\n[discussion-url]: https://github.com/webpack/webpack/discussions\n[chat-postcss]: https://badges.gitter.im/postcss/postcss.svg\n[chat-postcss-url]: https://gitter.im/postcss/postcss\n[size]: https://packagephobia.now.sh/badge?p=postcss-loader\n[size-url]: https://packagephobia.now.sh/result?p=postcss-loader\n","publish_time":1771184678246,"_cnpm_publish_time":1771184678246,"dist-tags":{"latest":"8.2.1","version-4":"4.3.0"}}