|
| 1 | +# @pigment-css/plugin |
| 2 | + |
| 3 | +<Subtitle>Package responsible for integrating Pigment CSS with your bundler.</Subtitle> |
| 4 | + |
| 5 | +This is the package responsible for integrating Pigment CSS with your bundler. It hooks into the bundler's build process and transpiles your source code and replaces the `css`, `styled` or other Pigment CSS functions with a runtime equivalent while also extracting the CSS written in the source code to external stylesheets. |
| 6 | + |
| 7 | +## Options |
| 8 | + |
| 9 | +Make sure that you've already configured your bundler to use Pigment CSS. Read the [integrations](/overview/integrations) overview first to configure your bundler. |
| 10 | + |
| 11 | +To customize the plugin behaviour, you can pass in following options in the `pigmentConfig` object configured during the integration. |
| 12 | + |
| 13 | +### include |
| 14 | + |
| 15 | +Type: `String | RegExp | Array[...String|RegExp]` |
| 16 | + |
| 17 | +A valid [picomatch](https://github.com/micromatch/picomatch#globbing-features) pattern, or array of patterns. If `pigmentConfig.include` is omitted or has zero length, filter will return true by default. Otherwise, an ID must match one or more of the picomatch patterns, and must not match any of the `pigmentConfig.exclude` patterns. |
| 18 | + |
| 19 | +This is used to specify the files that should be transformed by the plugin. If a file path does not match the `include` pattern, it won't go through Pigment CSS's tranformation process. This is useful when you are sure that a file in a certain directory or an extension imports the `@pigment-css/react-new` package. By default, all files that go through your bundler's build process are transformed (except anything in the `node_modules` directory). |
| 20 | + |
| 21 | +In the simplest case, you can pass a regex like - `include: /\.tsx?$/` to transform all the Typescript files if its a Typescript project. |
| 22 | + |
| 23 | +### exclude |
| 24 | + |
| 25 | +Type: `String | RegExp | Array[...String|RegExp]` |
| 26 | + |
| 27 | +This is used to specify the files that should not be transformed by the plugin. This is same as the `include` option but it is used to exclude files from being transformed. If you are sure that files in a certain directory or files with a particular extension don't import the `@pigment-css/react-new` package, you can exclude it to improve the build performance. |
| 28 | + |
| 29 | +### displayName |
| 30 | + |
| 31 | +Type: `Boolean` |
| 32 | + |
| 33 | +Default: `false` |
| 34 | + |
| 35 | +Prepends the generated class names with the appropriate variable name (when available) to make the class names more readable. Ideally, this should only be used in development mode. For example, for the example shown below - |
| 36 | + |
| 37 | +```tsx |
| 38 | +const redClass = css({ color: 'red' }); |
| 39 | +``` |
| 40 | + |
| 41 | +The generated class name will be `redClass_pp5a5eu` when `displayName` is `true`. This makes it easier to identify the origin of the styles. |
| 42 | + |
| 43 | +### sourceMap |
| 44 | + |
| 45 | +Type: `Boolean` |
| 46 | + |
| 47 | +Default: `false` |
| 48 | + |
| 49 | +Enables source maps for the generated CSS so that the generated class names are mapped to the original JS/TS source code. This is useful for debugging purposes, especially during development. |
| 50 | + |
| 51 | +### debug |
| 52 | + |
| 53 | +Type: `false | IFileReporterOptions | undefined` |
| 54 | + |
| 55 | +Default: `false` |
| 56 | + |
| 57 | +Enables debug mode for the plugin. This will log the generated CSS to the console. This is useful for debugging purposes, especially during development. |
| 58 | +Pass and object with the following properties to generate debug logs. This will help us diagnose issues with the plugin when you report them. It only includes the file paths and not the contents of the files. So it won't inlcude any sensitive information. |
| 59 | + |
| 60 | +- `dir`: The directory to save the debug logs. |
| 61 | +- `print`: Whether to print the debug logs to the console. |
| 62 | + |
| 63 | +### asyncResolve |
| 64 | + |
| 65 | +Type: `(what: string, importer: string) => Promise<string | null>` |
| 66 | + |
| 67 | +Default: `null` |
| 68 | + |
| 69 | +A custom async resolver for the plugin. This is useful if the plugin is not able to resolve a module specific to your bundler. This is also used internally by the plugin to resolve, for example, the `paths` set in the `tsconfig.json` file in a Typescript Next.js project. |
| 70 | + |
| 71 | +`what` is the module that is being resolved and `importer` is the file that is importing `what`, example: |
| 72 | + |
| 73 | +```ts title="src/components/Link.tsx" |
| 74 | +import Link from 'next/link'; |
| 75 | +``` |
| 76 | + |
| 77 | +In the above case, `what` is `'next/link'` and `importer` is `'src/components/Link.tsx'`. |
| 78 | +In most of the cases, you don't need to provide this option as the plugin will be able to resolve the module by itself using the bundler's resolver. But if you are facing an error like `Cannot find module 'next/link'` or similar, you can provide a custom resolver using this option. |
| 79 | + |
| 80 | +### features |
| 81 | + |
| 82 | +Type: `PigmentFeatures` |
| 83 | + |
| 84 | +Default: `{}` |
| 85 | + |
| 86 | +Feature flags that user can choose to enable/disable to control the output (JS/CSS). Right now, there is only one flag available inthe `PigmentFeatures` object - |
| 87 | + |
| 88 | +1. `useLayer`: Default is `true`. If set to `false`, the output css will not be wrapped in `@layer pigment.base`. Note that the `layer` functionality is essential if you are using the [Variants API](/overview/variants). But if you are not, you can set it to `false`. |
| 89 | + |
| 90 | +### theme |
| 91 | + |
| 92 | +Type: `Theme | ThemeOptions<Theme>` |
| 93 | + |
| 94 | +Default: `undefined` |
| 95 | + |
| 96 | +The theme object that will be passed to the `css` or `styled` functions. Read the [theming](/features/theming) feature to learn more about how to use themes in Pigment CSS. |
| 97 | + |
| 98 | +### runtimeReplacementPath |
| 99 | + |
| 100 | +Type: `(tag: string, source: string) => string | null` |
| 101 | + |
| 102 | +Default: `undefined` |
| 103 | + |
| 104 | +A function that can be used to customize the runtime replacement path. This can be useful if you want to use a custom runtime implementation. |
| 105 | + |
| 106 | +`tag` is the function that is being called and `source` is the import path of the function. For example, in the below code - |
| 107 | + |
| 108 | +```ts |
| 109 | +import { css } from '@pigment-css/core'; |
| 110 | + |
| 111 | +const cls = css({ color: 'red' }); |
| 112 | +``` |
| 113 | + |
| 114 | +`tag`'s value will be `css` and `source` will be `@pigment-css/core`. |
| 115 | + |
| 116 | +To replace the import with a custom runtime implementation, you can return the new path from the function. |
| 117 | + |
| 118 | +```ts title="bundler.config.ts" |
| 119 | +const pigmentConfig: PigmentCSSConfig = { |
| 120 | + runtimeReplacementPath: (tag, source) => { |
| 121 | + if (source === '@pigment-css/core') { |
| 122 | + return `@my-lib/runtime`; |
| 123 | + } |
| 124 | + return null; |
| 125 | + }, |
| 126 | +}; |
| 127 | +``` |
0 commit comments