Skip to content

Commit

Permalink
fix: 🐛 fix config handling and default file loader on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
DerZade committed Nov 9, 2024
1 parent c6abe2a commit 15a7786
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@ Overwrite the default file loader.
This can be useful if you want to modify or process the file before generating srcsets.
For example, you could change the fill color in an SVG from black to white:
```ts
import { stripSrcsetQuery } from 'vite-plugin-srcset';
import { normalizePath } from 'vite';
async function loadFile(id: string) {
const fsPath = new URL(id, import.meta.url).pathname; // id includes all search params (including srcset), which we don't care about here
// id includes all search params (including srcset), which we don't care about here
// in this example we'll strip the `srcset` as well as the `dark` query
const fsPath = normalizePath(stripSrcsetQuery(stripSrcsetQuery(id, 'dark')));
let text = await readFile(fsPath, 'utf-8');
text = text.replaceAll('fill="#000000"', 'fill="#FFFFFF"');
return { contents: Buffer.from(text, 'utf-8') };
Expand Down
21 changes: 17 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createFilter, type Plugin } from 'vite';
import { createFilter, normalizePath, type Plugin } from 'vite';
import { ESLiteral, toESString } from './utils/toESString';
import { readFile } from 'node:fs/promises';
import { parse } from 'node:path';
Expand All @@ -12,10 +12,21 @@ export interface ModuleExport {
// all: Record<ImageType, Record<number, string>>;
}

export function stripSrcsetQuery(id: string, query = 'srcset'): string {
const url = new URL(id, import.meta.url);
const oldSearch = url.search;
url.searchParams.delete(query);
const newSearch = url.search;

return id.replace(oldSearch, newSearch);
}

const DEFAULT_WIDTHS = [64, 128, 256, 512, 1024];
const DEFAULT_FORMATS = { png: true, webp: true };
const DEFAULT_FILE_LOADER = (id: string): Promise<{ contents: Uint8Array }> =>
readFile(new URL(id, import.meta.url).pathname).then((contents) => ({ contents }));
const DEFAULT_FILE_LOADER = (id: string): Promise<{ contents: Uint8Array }> => {
const path = normalizePath(stripSrcsetQuery(id));
return readFile(path).then((contents) => ({ contents }));
};

export async function renderImg(
original: Uint8Array,
Expand Down Expand Up @@ -121,7 +132,9 @@ export default function srcsetPlugin(...options: SrcsetPluginConfig): Plugin {
url.searchParams.delete('srcset');
const idWithoutParams = url.pathname;

const config = findConfig(url.pathname + url.search);
const normalizedId = normalizePath(stripSrcsetQuery(id));

const config = findConfig(normalizedId);

const { contents: original } = await config.loadFile.call(this, id);

Expand Down

0 comments on commit 15a7786

Please sign in to comment.