Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds to the readme how add dependency #448

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```

<h2 align="center">Maintainers</h2>

<table>
Expand Down
11 changes: 5 additions & 6 deletions test/fixtures/config/context/plugin.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -12,4 +9,6 @@ module.exports = postcss.plugin('plugin', (ctx) => {
return 0
}
}
})
};

module.exports = postcss.plugin('plugin', customPlugin);
2 changes: 2 additions & 0 deletions test/options/__snapshots__/config.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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`] = `
Expand Down
3 changes: 1 addition & 2 deletions test/options/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down