-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allow to properly include CSS in library builds by importing the…
… CSS files This way we do not require any DOM environment to be present. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
- Loading branch information
Showing
7 changed files
with
100 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2023 Ferdinand Thiessen <opensource@fthiessen.de> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import type { Plugin } from 'vite' | ||
import MagicString from 'magic-string' | ||
import { dirname, relative } from 'node:path' | ||
|
||
/** | ||
* Plugin that imports splitted CSS assets used by current entry point | ||
* | ||
* Basically this automatically imports all splitted CSS so that the users build system can decide what to do with the styles. | ||
* Because injecting is only possible with a DOM environement (not on SSR). | ||
*/ | ||
export const ImportCSSPlugin: () => Plugin = () => { | ||
return { | ||
name: 'vite-import-css-libmode', | ||
enforce: 'post', | ||
renderChunk(code, chunk) { | ||
if (!chunk.viteMetadata) return | ||
const { importedCss } = chunk.viteMetadata | ||
if (!importedCss.size) return | ||
|
||
/** | ||
* Inject the referenced style files at the top of the chunk. | ||
*/ | ||
const ms = new MagicString(code) | ||
for (const cssFileName of importedCss) { | ||
let cssFilePath = relative(dirname(chunk.fileName), cssFileName) | ||
cssFilePath = cssFilePath.startsWith('.') ? cssFilePath : `./${cssFilePath}` | ||
ms.prepend(`import '${cssFilePath}';\n`) | ||
} | ||
return { | ||
code: ms.toString(), | ||
map: ms.generateMap(), | ||
} | ||
}, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters