From b39df0cfc64bce54ad4708abc9c6df2e4c75aa3d Mon Sep 17 00:00:00 2001 From: cap-Bernardito Date: Mon, 17 Aug 2020 19:11:41 +0300 Subject: [PATCH] docs: addDependency --- README.md | 94 +++++++++++++++++++ test/fixtures/config/context/plugin.js | 11 +-- .../options/__snapshots__/config.test.js.snap | 2 + test/options/config.test.js | 3 +- 4 files changed, 102 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 41485568..39f8494d 100644 --- a/README.md +++ b/README.md @@ -861,6 +861,100 @@ module.exports = { }; ``` +### `Add dependencies` + +There are two way to add dependencies: + +1. (Recommended). Postcss plugin should emit message in `result.messages`. + +The message should contain the following fields: + +- `type` = `dependency` - Message type (require, should be equal `dependency`) +- `file` - absolute file path (require) + +**`webpack.config.js`** + +```js +const path = require('path'); + +const customPlugin = () => (css, result) => { + result.messages.push({ + type: 'dependency', + file: path.resolve(__dirname, 'path', 'to', 'file'), + }); +}; + +const postcssPlugin = postcss.plugin('postcss-assets', customPlugin); + +module.exports = { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + 'style-loader', + 'css-loader', + { + loader: 'postcss-loader', + options: { + plugins: [postcssPlugin()], + }, + }, + ], + }, + ], + }, +}; +``` + +2. Pass `loaderContext` in plugin. + +**`webpack.config.js`** + +```js +module.exports = { + module: { + rules: [ + { + test: /\.css$/i, + use: [ + 'style-loader', + 'css-loader', + { + loader: 'postcss-loader', + options: { + config: 'path/to/postcss.config.js', + }, + }, + ], + }, + ], + }, +}; +``` + +**`postcss.config.js`** + +```js +module.exports = (loaderContext) => ({ + plugins: [require('path/to/customPlugin')(loaderContext)], +}); +``` + +**`customPlugin.js`** + +```js +const path = require('path'); + +const customPlugin = (loaderContext) => (css, result) => { + loaderContext.webpack.addDependency( + path.resolve(__dirname, 'path', 'to', 'file') + ); +}; + +module.exports = postcss.plugin('postcss-assets', customPlugin); +``` +

Maintainers

diff --git a/test/fixtures/config/context/plugin.js b/test/fixtures/config/context/plugin.js index 17164458..57609db3 100644 --- a/test/fixtures/config/context/plugin.js +++ b/test/fixtures/config/context/plugin.js @@ -1,9 +1,6 @@ -'use strict' +const postcss = require('postcss'); -const postcss = require('postcss') - -// This plugin creates asset file in webpack compilation -module.exports = postcss.plugin('plugin', (ctx) => { +const customPlugin = (ctx) => (css, result) => { ctx.webpack._compilation.assets['asset.txt'] = { source () { return '123' @@ -12,4 +9,6 @@ module.exports = postcss.plugin('plugin', (ctx) => { return 0 } } -}) +}; + +module.exports = postcss.plugin('plugin', customPlugin); diff --git a/test/options/__snapshots__/config.test.js.snap b/test/options/__snapshots__/config.test.js.snap index df207b96..84e5c4fc 100644 --- a/test/options/__snapshots__/config.test.js.snap +++ b/test/options/__snapshots__/config.test.js.snap @@ -99,6 +99,8 @@ exports[`Config Options should work Config - true: errors 1`] = `Array []`; exports[`Config Options should work Config - true: warnings 1`] = `Array []`; +exports[`Config Options should work Config – Context – Loader {Object}: errors 1`] = `Array []`; + exports[`Config Options should work Config – Context – Loader {Object}: warnings 1`] = `Array []`; exports[`Config Options should work package.json - {Object} - Process CSS: css 1`] = ` diff --git a/test/options/config.test.js b/test/options/config.test.js index ff0f43a5..df3900df 100644 --- a/test/options/config.test.js +++ b/test/options/config.test.js @@ -145,8 +145,7 @@ describe('Config Options', () => { expect(asset in assets).toBeTruthy(); expect(getWarnings(stats)).toMatchSnapshot('warnings'); - // Todo fixed error in testplugin - // expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getErrors(stats)).toMatchSnapshot('errors'); }); it('should work postcss.config.js - {Object} - Process CSS', async () => {