Skip to content

Commit 173f09b

Browse files
[#46] Skip plugin execution when SSR is enabled (#47)
* Add skip logic for SSR usage * 2.1.0 * 2.1.0-beta-0 * 2.1.0-beta-1 * Cosmetics * Cosmetics * 2.1.0
1 parent 825fe24 commit 173f09b

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
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": "2.0.4",
3+
"version": "2.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/cjs/index.js",
66
"module": "dist/esm/index.js",

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from 'vite';
1+
import { Plugin, ResolvedConfig } from 'vite';
22
import { OutputAsset, OutputChunk } from 'rollup';
33
import { buildCSSInjectionCode, removeLinkStyleSheets } from './utils.js';
44

@@ -15,12 +15,20 @@ export default function cssInjectedByJsPlugin(
1515
): Plugin {
1616
//Globally so we can add it to legacy and non-legacy bundle.
1717
let cssToInject: string = '';
18+
let config: ResolvedConfig;
1819

1920
return {
2021
apply: 'build',
2122
enforce: 'post',
2223
name: 'css-in-js-plugin',
24+
configResolved(_config) {
25+
config = _config;
26+
},
2327
async generateBundle(opts, bundle) {
28+
if (config.build.ssr) {
29+
return;
30+
}
31+
2432
const htmlFiles = Object.keys(bundle).filter((i) => i.endsWith('.html'));
2533
const cssAssets = Object.keys(bundle).filter(
2634
(i) => bundle[i].type == 'asset' && bundle[i].fileName.endsWith('.css')

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function injectionCSSCodePlugin(cssToInject: string, styleId?: string): Plugin {
4848
if (id == cssInjectedByJsId) {
4949
const cssCode = JSON.stringify(cssToInject.trim());
5050

51-
return `try{if (typeof document !== 'undefined'){var elementStyle = document.createElement('style');${
51+
return `try{if(typeof document != 'undefined'){var elementStyle = document.createElement('style');${
5252
typeof styleId == 'string' && styleId.length > 0 ? `elementStyle.id = '${styleId}';` : ''
5353
}elementStyle.appendChild(document.createTextNode(${cssCode}));document.head.appendChild(elementStyle);}}catch(e){console.error('vite-plugin-css-injected-by-js', e);}`;
5454
}

0 commit comments

Comments
 (0)