Repository navigation
Replies: 5 comments 12 replies
|
The A couple of ways to get the file verbatim: 1. Use a
|
|
There's no per-file "don't touch this CSS" flag on the 1. If you control the file: put it in 2. If the import { readFileSync } from 'node:fs';
import type { Plugin } from 'vite';
const KEEP_RAW = /[\\/]monaco-vscode-api[\\/].*\.css$/;
export function keepCssRaw(): Plugin {
return {
name: 'keep-css-raw',
enforce: 'pre',
async resolveId(id, importer) {
const r = await this.resolve(id, importer, { skipSelf: true });
if (r && KEEP_RAW.test(r.id)) {
// ?url makes Vite treat it as a plain asset URL…
return r.id + '?raw-asset';
}
},
load(id) {
if (id.endsWith('?raw-asset')) {
const file = id.replace('?raw-asset', '');
const ref = this.emitFile({
type: 'asset',
name: file.split(/[\\/]/).pop(),
source: readFileSync(file), // byte-for-byte, url() untouched
});
return `export default import.meta.ROLLUP_FILE_URL_${ref};`;
}
},
};
}
One thing that does not work, to save you the experiment: |
|
The 1. import cssText from './myfile.css?raw'No PostCSS, no 2. import cssUrl from './myfile.css?url'Vite treats it as an asset reference and copies it as is rather than processing the CSS. 3. Drop the file in For your VSCode webview case where the service worker needs the original Hope that helps. If it solves it, feel free to select it as the answer. |
|
Yes — set The cleanest approach for your Monaco/VSCode use case: Option 1: // Vite treats this as a string, no CSS processing
const cssText = await import('./myfile.css?raw');
// use cssText.defaultVite skips all CSS transformation when you use Option 2: const cssUrl = new URL('./myfile.css?url', import.meta.url);
// gets the unresolved file URL, no `url()` rewritingUnlike Option 3: Configure If your CSS files are under whatever threshold you set, Vite inlines them, which also means build: {
assetsInlineLimit: 0,
}Option 4: If you need the {
name: 'skip-css-transform',
load(id) {
if (id.endsWith('.css')) return '';
},
}For the VSCode webview assets case specifically, |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
TL;DR: When using the
new URL('./myfile.css', import.meta.url), is there a way to prevent vite from trying to resolveurl(...)in the css file?My use-case
Via monaco-vscode-api, VSCode webview are registered by extension, whose assets files are registered via the
new URL('./myfile.css', import.meta.url)syntax.Some VSCode extensions are injecting css files in the webviews, and that css NEEDS to be kept as-is, because a service-worker is used to retrieve the actual assets, and it fails if the path is rewritten.
Actual css:
Css after vite transformation:
For some reasons, it seems to be happening only in serve mode
It can be a vite configuration or a query parameter (line
inline,raw,worker...), I've tried them all, and nothing helps (exceptcommonjs-proxyfor some reasons, but it would be VERY hacky)All reactions