Skip to content

Commit eb57178

Browse files
[#8] Add a configure param to choose execution priority (#9)
* Add param and logic * Update doc * 1.1.0
1 parent 4e2bc63 commit eb57178

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
A Vite plugin that takes the CSS and adds it to the page through the JS. For those who want a single JS file.
33

44
## How does it work
5-
Essentially what it does is take all the CSS generated by the build process and add it through javascript.
5+
Essentially what it does is take all the CSS generated by the build process and add it via JavaScript.
66
The CSS file is therefore not generated and the declaration in the generated HTML file is also removed.
7+
You can also configure when the CSS injection will be executed (before or after your app code).
78

89
## Installation
910
```
@@ -16,7 +17,23 @@ import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
1617

1718
export default {
1819
plugins: [
19-
cssInJsPlugin(),
20+
cssInjectedByJsPlugin(),
21+
]
22+
}
23+
```
24+
### Configurations
25+
When you add the plugin, you can provide a configuration object.
26+
For now, you can configure only when the injection of CSS is done at execution time ```topExecutionPriority```.
27+
#### topExecutionPriority
28+
The default behavior adds the injection of CSS before your bundle code.
29+
If you provide ```topExecutionPriority``` equal to: ```false``` the code of injection will be added after the bundle code.
30+
This is an example:
31+
```ts
32+
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
33+
34+
export default {
35+
plugins: [
36+
cssInjectedByJsPlugin({topExecutionPriority: false}),
2037
]
2138
}
2239
```

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-plugin-css-injected-by-js",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "A Vite plugin that takes the CSS and adds it to the page through the JS. For those who want a single JS file.",
55
"main": "dist/plugin.js",
66
"scripts": {

src/plugin.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IndexHtmlTransformContext, IndexHtmlTransformResult, Plugin } from 'vit
55
*
66
* @return {Plugin}
77
*/
8-
export default function cssInjectedByJsPlugin(): Plugin {
8+
export default function cssInjectedByJsPlugin({topExecutionPriority} = { topExecutionPriority: true }): Plugin {
99

1010
return {
1111
apply: 'build',
@@ -40,8 +40,15 @@ export default function cssInjectedByJsPlugin(): Plugin {
4040

4141
if (chunk.type === 'chunk' && chunk.fileName.includes('.js')) {
4242

43-
//IIFE http://benalman.com/news/2010/11/immediately-invoked-function-expression/
44-
chunk.code += `(function(){ try {var elementStyle = document.createElement('style'); elementStyle.innerText = \`${styleCode}\`; document.head.appendChild(elementStyle);} catch(e) {console.error(e, 'vite-plugin-css-injected-by-js: can\\'t add the style.');} })()`;
43+
let topCode: string = '';
44+
let bottomCode: string = '';
45+
if (topExecutionPriority) {
46+
bottomCode = chunk.code;
47+
} else {
48+
topCode = chunk.code;
49+
}
50+
51+
chunk.code = `${topCode}(function(){ try {var elementStyle = document.createElement('style'); elementStyle.innerText = \`${styleCode}\`; document.head.appendChild(elementStyle);} catch(e) {console.error(e, 'vite-plugin-css-injected-by-js: error when trying to add the style.');} })();${bottomCode}`;
4552

4653
break;
4754

0 commit comments

Comments
 (0)