@@ -934,16 +934,19 @@ The message should contain the following fields:
934934** webpack.config.js**
935935
936936``` js
937- const customPlugin = () => (css , result ) => {
938- result .messages .push ({
939- type: " asset" ,
940- file: " sprite.svg" ,
941- content: " <svg>...</svg>" ,
942- });
937+ const postcssCustomPlugin = (opts = {}) => {
938+ return {
939+ postcssPlugin : ' postcss-custom-plugin' ,
940+ Once : (root , {result}) => {
941+ result .messages .push ({
942+ type: " asset" ,
943+ file: " sprite.svg" ,
944+ content: " <svg>...</svg>" ,
945+ });
946+ }
947+ }
943948};
944949
945- const postcssPlugin = postcss .plugin (" postcss-assets" , customPlugin);
946-
947950module .exports = {
948951 module: {
949952 rules: [
@@ -956,7 +959,7 @@ module.exports = {
956959 loader: " postcss-loader" ,
957960 options: {
958961 postcssOptions: {
959- plugins: [postcssPlugin ()],
962+ plugins: [postcssCustomPlugin ()],
960963 },
961964 },
962965 },
@@ -985,15 +988,18 @@ The message should contain the following fields:
985988``` js
986989const path = require (" path" );
987990
988- const customPlugin = () => (css , result ) => {
989- result .messages .push ({
990- type: " dependency" ,
991- file: path .resolve (__dirname , " path" , " to" , " file" ),
992- });
991+ const postcssCustomPlugin = (opts = {}) => {
992+ return {
993+ postcssPlugin : ' postcss-custom-plugin' ,
994+ Once : (root , {result}) => {
995+ result .messages .push ({
996+ type: " dependency" ,
997+ file: path .resolve (__dirname , " path" , " to" , " file" ),
998+ });
999+ }
1000+ }
9931001};
9941002
995- const postcssPlugin = postcss .plugin (" postcss-assets" , customPlugin);
996-
9971003module .exports = {
9981004 module: {
9991005 rules: [
@@ -1006,7 +1012,7 @@ module.exports = {
10061012 loader: " postcss-loader" ,
10071013 options: {
10081014 postcssOptions: {
1009- plugins: [postcssPlugin ()],
1015+ plugins: [postcssCustomPlugin ()],
10101016 },
10111017 },
10121018 },
@@ -1017,6 +1023,8 @@ module.exports = {
10171023};
10181024```
10191025
1026+ Or you can use ready-made plugin [ postcss-add-dependencies] ( https://www.npmjs.com/package/postcss-add-dependencies ) .
1027+
102010282 . Pass ` loaderContext ` in plugin.
10211029
10221030** webpack.config.js**
@@ -1052,25 +1060,31 @@ module.exports = {
10521060``` js
10531061module .exports = (api ) => ({
10541062 plugins: [
1055- require (" path/to/customPlugin " )({
1063+ require (" path/to/postcssCustomPlugin.js " )({
10561064 loaderContext: api .webpackLoaderContext ,
10571065 }),
10581066 ],
10591067});
10601068```
10611069
1062- ** customPlugin .js**
1070+ ** postcssCustomPlugin .js**
10631071
10641072``` js
10651073const path = require (" path" );
10661074
1067- const customPlugin = (loaderContext ) => (css , result ) => {
1068- loaderContext .webpack .addDependency (
1069- path .resolve (__dirname , " path" , " to" , " file" )
1070- );
1075+ const postcssCustomPlugin = (opts = {}) => {
1076+ return {
1077+ postcssPlugin : ' postcss-custom-plugin' ,
1078+ Once : (root , {result}) => {
1079+ opts .loaderContext .addDependency (
1080+ path .resolve (__dirname , " path" , " to" , " file" )
1081+ );
1082+ }
1083+ }
10711084};
10721085
1073- module .exports = postcss .plugin (" postcss-assets" , customPlugin);
1086+ postcssCustomPlugin .postcss = true ;
1087+ module .exports = postcssCustomPlugin;
10741088```
10751089
10761090## Contributing
0 commit comments