mirror of
https://github.com/hanzoai/postcss.git
synced 2026-09-24 17:01:56 +00:00
docs: show ESM and TypeScript plugin declaration (#2118)
* docs: show ESM and TypeScript plugin declaration Document the maintainer-recommended pattern of setting postcss = true on the creator function and default-exporting it, so ES modules and TypeScript do not need module.exports. Fixes #1771 * docs: make ESM the default plugin example
This commit is contained in:
@@ -108,7 +108,7 @@ It is better even not to import `postcss`.
|
||||
Plugin name will be used in error messages and warnings.
|
||||
|
||||
```js
|
||||
module.exports = opts => {
|
||||
const plugin = opts => {
|
||||
return {
|
||||
postcssPlugin: 'postcss-name',
|
||||
Once(root) {
|
||||
@@ -116,7 +116,9 @@ module.exports = opts => {
|
||||
}
|
||||
}
|
||||
}
|
||||
module.exports.postcss = true
|
||||
plugin.postcss = true
|
||||
|
||||
export default plugin
|
||||
```
|
||||
|
||||
## 2. Processing
|
||||
|
||||
@@ -91,14 +91,32 @@ For public plugins:
|
||||
3. Publish your code there.
|
||||
|
||||
```js
|
||||
module.exports = (opts = {}) => {
|
||||
const plugin = (opts = {}) => {
|
||||
// Plugin creator to check options or prepare shared state
|
||||
return {
|
||||
postcssPlugin: 'PLUGIN NAME'
|
||||
// Plugin listeners
|
||||
}
|
||||
}
|
||||
module.exports.postcss = true
|
||||
plugin.postcss = true
|
||||
|
||||
export default plugin
|
||||
```
|
||||
|
||||
In TypeScript, type the creator as `PluginCreator` so `postcss = true` is valid:
|
||||
|
||||
```ts
|
||||
import type { PluginCreator } from 'postcss'
|
||||
|
||||
const plugin: PluginCreator<object> = (opts = {}) => {
|
||||
return {
|
||||
postcssPlugin: 'PLUGIN NAME'
|
||||
// Plugin listeners
|
||||
}
|
||||
}
|
||||
plugin.postcss = true
|
||||
|
||||
export default plugin
|
||||
```
|
||||
|
||||
[PostCSS plugin boilerplate]: https://github.com/postcss/postcss-plugin-boilerplate/
|
||||
|
||||
Reference in New Issue
Block a user