Skip to content

Commit 5c61343

Browse files
[DOCS] Update readme 3.4.0 (#135)
1 parent b03f1fa commit 5c61343

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

README.md

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ export default {
3131

3232
### Configurations
3333

34-
When you add the plugin, you can provide a configuration object.
35-
Below you can find all configuration parameters available.
34+
When you add the plugin, you can provide a configuration object. Below you can find all configuration parameters
35+
available.
3636

3737
#### cssAssetsFilterFunction (function)
3838

39-
The `cssAssetsFilterFunction` parameter allows you to specify a filter function that will enable you to exclude some output css assets.
39+
The `cssAssetsFilterFunction` parameter allows you to specify a filter function that will enable you to exclude some
40+
output css assets.
4041

4142
**This option is not applied to `relativeCSSInjection` logic.**
4243

@@ -59,10 +60,16 @@ export default {
5960
#### dev (object)
6061

6162
**EXPERIMENTAL**
63+
Why experimental? Because it uses a non-conventional solution.
64+
65+
Previously, the plugin strictly applied logic solely during the build phase. Now, we have the capability to experiment
66+
with it in the development environment.
67+
68+
To activate the plugin in the development environment as well, you need to configure a dev object and set the enableDev
69+
parameter to true.
70+
71+
Here's an example:
6272

63-
Before this param this plugin applied the logic only in build phase.
64-
Now we can experiment it in the dev environment.
65-
To enable the plugin to work also in the dev environment you should configure a `dev` object and set the `enableDev` param to `true`, an example:
6673
```ts
6774
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
6875

@@ -79,22 +86,30 @@ export default {
7986
]
8087
}
8188
```
82-
This approach should be sufficient unless you are using custom injection code to insert styles where needed.
83-
Since the dev environment involve the concept of "update" the style in the DOM this plugin need at least the code that remove from the DOM the injected style.
84-
Because of all of this, **if you are using custom injection code (by using `injectCode` or `injectCodeFunction`)**, this plugin can't know how to delete the style you injected.
85-
So all you need to do is configure one of `removeStyleCode`, `removeStyleCodeFunction` inside the dev object showed above.
8689

87-
**NOTE:** The `injectCode` and `injectCodeFunction` parameters now also include the `attributes`, and in dev mode, the attributes object encompasses the `data-vite-dev-id` as well. Refer to the `injectCodeFunction` example below for further details.
90+
This approach should serve its purpose effectively unless you're employing custom injection code to insert styles where
91+
necessary. Since the development environment involves the concept of "updating" styles in the Document Object Model (
92+
DOM), this plugin requires code to remove the injected style from the DOM.
93+
94+
Due to these factors, if you're utilizing custom injection code (via `injectCode` or `injectCodeFunction`), the plugin
95+
cannot automatically discern how to delete the injected style. Therefore, all you need to do is configure
96+
either `removeStyleCode` or `removeStyleCodeFunction` within the `dev` object as demonstrated above.
97+
98+
**NOTE:** The `injectCode` and `injectCodeFunction` parameters now also include the `attributes`, and in `dev` mode,
99+
the `attributes` object encompasses the `data-vite-dev-id` as well. Refer to the `injectCodeFunction` example below for
100+
further details.
88101

89102
#### injectCode (function)
90103

91-
You can provide also a function for `injectCode` param to customize the injection code used.
92-
The `injectCode` callback must return a `string` (with valid JS code) and it's called with two arguments:
104+
You can provide also a function for `injectCode` param to customize the injection code used. The `injectCode` callback
105+
must return a `string` (with valid JS code) and it's called with two arguments:
93106

94107
- cssCode (the `string` that contains all the css code that need to be injected via JavaScript)
95-
- options (an object with `styleId`, `useStrictCSP` and `attributes` the last is an object that represent the attributes of the style element that should have)
108+
- options (an object with `styleId`, `useStrictCSP` and `attributes` the last is an object that represent the attributes
109+
of the style element that should have)
96110

97111
This is an example:
112+
98113
```ts
99114
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
100115

@@ -116,7 +131,8 @@ If you prefer to specify the injectCode as a plain function you can use the `inj
116131
The `injectCodeFunction` function is a void function that will be called at runtime application with two arguments:
117132

118133
- cssCode (the `string` that contains all the css code that need to be injected via JavaScript)
119-
- options (an object with `styleId`, `useStrictCSP` and `attributes` the last is an object that represent the attributes of the style element that should have)
134+
- options (an object with `styleId`, `useStrictCSP` and `attributes` the last is an object that represent the attributes
135+
of the style element that should have)
120136

121137
This is an example:
122138

@@ -135,7 +151,7 @@ export default {
135151
for (const attribute in options.attributes) {
136152
elementStyle.setAttribute(attribute, options.attributes[attribute]);
137153
}
138-
154+
139155
elementStyle.appendChild(document.createTextNode(${cssCode}));
140156
document.head.appendChild(elementStyle);
141157
}
@@ -171,8 +187,8 @@ export default {
171187
}
172188
```
173189

174-
In this example, the CSS injection code will only be added to the `index.js` file. If you wish to add the code to multiple
175-
files, you can specify them in the function:
190+
In this example, the CSS injection code will only be added to the `index.js` file. If you wish to add the code to
191+
multiple files, you can specify them in the function:
176192

177193
```javascript
178194
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
@@ -214,18 +230,21 @@ export default {
214230

215231
#### relativeCSSInjection (boolean)
216232

217-
_This feature is based on information provided by Vite. Since we can't control how Vite handles this information this means that there may be problems that may not be possible to fix them in this plugin._
233+
_This feature is based on information provided by Vite. Since we can't control how Vite handles this information this
234+
means that there may be problems that may not be possible to fix them in this plugin._
218235

219236
The default behavior of this plugin takes all the CSS code of your application directly to the entrypoint generated.
220-
The `relativeCSSInjection` if configured to `true` will inject the CSS code of every entrypoint to the relative importer.
237+
The `relativeCSSInjection` if configured to `true` will inject the CSS code of every entrypoint to the relative
238+
importer.
221239

222240
**Set this option to `true` if you are using the multiple entry point option of Rollup.**
223241
**For this feature to work, `build.cssCodeSplit` must be set to `true`**
224242

225-
_Future release can have an advanced behavior where this options will be configured to true automatically by sniffing user configurations._
243+
_Future release can have an advanced behavior where this options will be configured to true automatically by sniffing
244+
user configurations._
226245

227-
If a CSS chunk is generated that's not imported by any JS chunk, a warning will be shown.
228-
To disable this warning set `suppressUnusedCssWarning` to `true`.
246+
If a CSS chunk is generated that's not imported by any JS chunk, a warning will be shown. To disable this warning
247+
set `suppressUnusedCssWarning` to `true`.
229248

230249
#### styleId (string | function)
231250

@@ -251,7 +270,8 @@ The output injected into the DOM will look like this example:
251270
</head>
252271
```
253272

254-
If you provide a `function` for `styleId` param, it will run that function and return a string. It's especially useful if you use `relativeCSSInjection` and want unique styleIds for each file.
273+
If you provide a `function` for `styleId` param, it will run that function and return a string. It's especially useful
274+
if you use `relativeCSSInjection` and want unique styleIds for each file.
255275

256276
```ts
257277
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
@@ -264,6 +284,7 @@ export default {
264284
```
265285

266286
```html
287+
267288
<head>
268289
<style id="foo-1234">/* Generated CSS rules */</style>
269290
<style id="foo-4321">/* Generated CSS rules */</style>
@@ -309,7 +330,7 @@ will be injected by our default injection code.
309330

310331
## Contributing
311332

312-
When you make changes to plugin locally, you may want to build the js from the typescript file of the plugin.
333+
When you make changes to plugin locally, you may want to build the js from the typescript file of the plugin.
313334

314335
Here the guidelines:
315336

@@ -333,7 +354,9 @@ npm run build
333354

334355
See [CONTRIBUTING.md](CONTRIBUTING.md) for more information.
335356

336-
337357
### A note for plugin-legacy users
338-
At first the plugin supported generating the CSS injection code also in the legacy files generated by the [plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy). Since the plugin-legacy injects the CSS code for [different reasons](https://github.com/vitejs/vite/issues/2062), this plugin no longer has the plugin-legacy support code.
339-
If the code of the plugin-legacy changes an update to this plugin may occur.
358+
359+
At first the plugin supported generating the CSS injection code also in the legacy files generated by
360+
the [plugin-legacy](https://github.com/vitejs/vite/tree/main/packages/plugin-legacy). Since the plugin-legacy injects
361+
the CSS code for [different reasons](https://github.com/vitejs/vite/issues/2062), this plugin no longer has the
362+
plugin-legacy support code. If the code of the plugin-legacy changes an update to this plugin may occur.

0 commit comments

Comments
 (0)