Skip to content

Commit 60e4f12

Browse files
docs: addDependency (#448)
1 parent 68699f6 commit 60e4f12

File tree

4 files changed

+102
-8
lines changed

4 files changed

+102
-8
lines changed

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,100 @@ module.exports = {
861861
};
862862
```
863863

864+
### `Add dependencies`
865+
866+
There are two way to add dependencies:
867+
868+
1. (Recommended). Postcss plugin should emit message in `result.messages`.
869+
870+
The message should contain the following fields:
871+
872+
- `type` = `dependency` - Message type (require, should be equal `dependency`)
873+
- `file` - absolute file path (require)
874+
875+
**`webpack.config.js`**
876+
877+
```js
878+
const path = require('path');
879+
880+
const customPlugin = () => (css, result) => {
881+
result.messages.push({
882+
type: 'dependency',
883+
file: path.resolve(__dirname, 'path', 'to', 'file'),
884+
});
885+
};
886+
887+
const postcssPlugin = postcss.plugin('postcss-assets', customPlugin);
888+
889+
module.exports = {
890+
module: {
891+
rules: [
892+
{
893+
test: /\.css$/i,
894+
use: [
895+
'style-loader',
896+
'css-loader',
897+
{
898+
loader: 'postcss-loader',
899+
options: {
900+
plugins: [postcssPlugin()],
901+
},
902+
},
903+
],
904+
},
905+
],
906+
},
907+
};
908+
```
909+
910+
2. Pass `loaderContext` in plugin.
911+
912+
**`webpack.config.js`**
913+
914+
```js
915+
module.exports = {
916+
module: {
917+
rules: [
918+
{
919+
test: /\.css$/i,
920+
use: [
921+
'style-loader',
922+
'css-loader',
923+
{
924+
loader: 'postcss-loader',
925+
options: {
926+
config: 'path/to/postcss.config.js',
927+
},
928+
},
929+
],
930+
},
931+
],
932+
},
933+
};
934+
```
935+
936+
**`postcss.config.js`**
937+
938+
```js
939+
module.exports = (loaderContext) => ({
940+
plugins: [require('path/to/customPlugin')(loaderContext)],
941+
});
942+
```
943+
944+
**`customPlugin.js`**
945+
946+
```js
947+
const path = require('path');
948+
949+
const customPlugin = (loaderContext) => (css, result) => {
950+
loaderContext.webpack.addDependency(
951+
path.resolve(__dirname, 'path', 'to', 'file')
952+
);
953+
};
954+
955+
module.exports = postcss.plugin('postcss-assets', customPlugin);
956+
```
957+
864958
<h2 align="center">Maintainers</h2>
865959

866960
<table>
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
'use strict'
1+
const postcss = require('postcss');
22

3-
const postcss = require('postcss')
4-
5-
// This plugin creates asset file in webpack compilation
6-
module.exports = postcss.plugin('plugin', (ctx) => {
3+
const customPlugin = (ctx) => (css, result) => {
74
ctx.webpack._compilation.assets['asset.txt'] = {
85
source () {
96
return '123'
@@ -12,4 +9,6 @@ module.exports = postcss.plugin('plugin', (ctx) => {
129
return 0
1310
}
1411
}
15-
})
12+
};
13+
14+
module.exports = postcss.plugin('plugin', customPlugin);

test/options/__snapshots__/config.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ exports[`Config Options should work Config - true: errors 1`] = `Array []`;
9999

100100
exports[`Config Options should work Config - true: warnings 1`] = `Array []`;
101101

102+
exports[`Config Options should work Config – Context – Loader {Object}: errors 1`] = `Array []`;
103+
102104
exports[`Config Options should work Config – Context – Loader {Object}: warnings 1`] = `Array []`;
103105

104106
exports[`Config Options should work package.json - {Object} - Process CSS: css 1`] = `

test/options/config.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ describe('Config Options', () => {
145145

146146
expect(asset in assets).toBeTruthy();
147147
expect(getWarnings(stats)).toMatchSnapshot('warnings');
148-
// Todo fixed error in testplugin
149-
// expect(getErrors(stats)).toMatchSnapshot('errors');
148+
expect(getErrors(stats)).toMatchSnapshot('errors');
150149
});
151150

152151
it('should work postcss.config.js - {Object} - Process CSS', async () => {

0 commit comments

Comments
 (0)