Skip to content

Commit f20d094

Browse files
[#35] Build CSS code outside html cleaning block (#36)
* Build CSS code outside html cleaning block * 2.0.2
1 parent 633bbfd commit f20d094

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
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.1",
3+
"version": "2.0.2",
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: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,26 @@ export default function cssInjectedByJsPlugin(
3030
!bundle[i].fileName.includes('polyfill')
3131
);
3232

33+
const allCssCode = cssAssets.reduce(function extractCssCodeAndDeleteFromBundle(previousValue, cssName) {
34+
const cssAsset = bundle[cssName] as OutputAsset;
35+
const result = previousValue + cssAsset.source;
36+
delete bundle[cssName];
37+
38+
return result;
39+
}, '');
40+
41+
if (allCssCode.length > 0) {
42+
cssToInject = allCssCode;
43+
}
44+
3345
for (const name of htmlFiles) {
3446
const htmlChunk = bundle[name] as OutputAsset;
3547
let replacedHtml = htmlChunk.source as string;
3648

37-
const allCssCode = cssAssets.reduce(function extractCssCodeAndDeleteFromBundle(previousValue, cssName) {
38-
const cssAsset = bundle[cssName] as OutputAsset;
39-
const result = previousValue + cssAsset.source;
40-
delete bundle[cssName];
49+
cssAssets.forEach((cssName) => {
4150
replacedHtml = removeLinkStyleSheets(replacedHtml, cssName);
4251
htmlChunk.source = replacedHtml;
43-
return result;
44-
}, '');
45-
46-
if (allCssCode.length > 0) {
47-
cssToInject = allCssCode;
48-
}
52+
});
4953
}
5054

5155
const jsAsset = bundle[jsAssets[0]] as OutputChunk;

0 commit comments

Comments
 (0)