Skip to content

Commit

Permalink
[#108] Fix injection for multiple build of same entry point (#109)
Browse files Browse the repository at this point in the history
* fix: inject css into multiple build process of same entry point

* docs: add info about relativeCSSInjection

* 3.2.1

* refactor: remove unreq comment
  • Loading branch information
marco-prontera committed Jul 17, 2023
1 parent 2bf2cb1 commit 57f36d5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ _This feature is based on information provided by Vite. Since we can't control h
The default behavior of this plugin takes all the CSS code of your application directly to the entrypoint generated.
The `relativeCSSInjection` if configured to `true` will inject the CSS code of every entrypoint to the relative importer.

**Set this option to `true` if you are using the multiple entry point option of Rollup.**
_Future release can have an advanced behavior where this options will be configured to true automatically by sniffing user configurations._

If a CSS code it's not injected inside some JS a warning will be showed.
To disable this warning set `suppressUnusedCssWarning` to `true`.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-css-injected-by-js",
"version": "3.2.0",
"version": "3.2.1",
"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.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
35 changes: 34 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function defaultJsAssetsFilter(chunk: OutputChunk): boolean {

// The cache must be global since execution context is different every entry
const cssSourceCache: { [key: string]: string } = {};

export function extractCss(bundle: OutputBundle, cssName: string): string {
const cssAsset = bundle[cssName] as OutputAsset;

Expand Down Expand Up @@ -224,6 +225,9 @@ export async function relativeCssInjection(
}
}

const globalCSSCodeEntryCache = new Map();
let previousFacadeModuleId = '';

export async function globalCssInjection(
bundle: OutputBundle,
cssAssets: string[],
Expand All @@ -241,10 +245,39 @@ export async function globalCssInjection(
process.env.VITE_CSS_INJECTED_BY_JS_DEBUG &&
debugLog(`[vite-plugin-css-injected-by-js] Global CSS Assets: [${cssAssets.join(',')}]`);
const allCssCode = concatCssAndDeleteFromBundle(bundle, cssAssets);
const cssInjectionCode = allCssCode.length > 0 ? (await buildCssCode(allCssCode))?.code : '';
let cssInjectionCode: string = '';

if (allCssCode.length > 0) {
const cssCode = (await buildCssCode(allCssCode))?.code;
if (typeof cssCode == 'string') {
cssInjectionCode = cssCode;
}
}

for (const jsTargetKey of jsTargetBundleKeys) {
const jsAsset = bundle[jsTargetKey] as OutputChunk;

/**
* Since it creates the assets once sequential builds for the same entry point
* (for example when multiple formats of same entry point are built),
* we need to reuse the same CSS created the first time.
*/
if (jsAsset.facadeModuleId != null && jsAsset.isEntry && cssInjectionCode != '') {
if (jsAsset.facadeModuleId != previousFacadeModuleId) {
globalCSSCodeEntryCache.clear();
}
previousFacadeModuleId = jsAsset.facadeModuleId;
globalCSSCodeEntryCache.set(jsAsset.facadeModuleId, cssInjectionCode);
}
if (
cssInjectionCode == '' &&
jsAsset.isEntry &&
jsAsset.facadeModuleId != null &&
typeof globalCSSCodeEntryCache.get(jsAsset.facadeModuleId) == 'string'
) {
cssInjectionCode = globalCSSCodeEntryCache.get(jsAsset.facadeModuleId);
}

process.env.VITE_CSS_INJECTED_BY_JS_DEBUG &&
debugLog(`[vite-plugin-css-injected-by-js] Global CSS inject: ${jsAsset.fileName}`);
jsAsset.code = buildOutputChunkWithCssInjectionCode(
Expand Down

0 comments on commit 57f36d5

Please sign in to comment.