Skip to content

Commit

Permalink
fix: fixed errors thrown by the fileURLToPath method (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
phk422 committed Sep 5, 2023
1 parent 2bcc415 commit 48ec48c
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mime from 'mime';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import sharp from 'sharp';
import type { Plugin, UserConfig } from 'vite';
import type { Plugin } from 'vite';

export interface LQIPPluginOptions {
sharp?: {
Expand All @@ -22,13 +22,9 @@ export interface LQIPPluginOptions {
}

export default function vitePluginLqip(options?: LQIPPluginOptions): Plugin {
let viteConfig: UserConfig = {} as unknown as UserConfig;
return {
name: 'vite-plugin-lqip',
enforce: 'pre',
configResolved(cfg) {
viteConfig = cfg as unknown as UserConfig;
},
async load(id) {
const [base, search] = id.split('?');
if (!search) {
Expand All @@ -39,7 +35,7 @@ export default function vitePluginLqip(options?: LQIPPluginOptions): Plugin {
return null;
}

const img = sharp(fileURLToPath(new URL(base!, `file://${viteConfig.root}/`)));
const img = sharp(fileURLToPath(new URL(`file://${base!}`)));
const metadata = await img.metadata();
const output = await img
.toFormat('webp', {
Expand Down

0 comments on commit 48ec48c

Please sign in to comment.