Skip to content

Commit

Permalink
fix: fixed errors thrown by the fileURLToPath method (#6) (#7)
Browse files Browse the repository at this point in the history
* fix: fixed errors thrown by the fileURLToPath method (#6)

* Update src/index.ts

optimized code

Co-authored-by: Drew Powers <[email protected]>

* update changeset

* style: remove useless code

---------

Co-authored-by: Drew Powers <[email protected]>
  • Loading branch information
phk422 and drwpow authored Sep 6, 2023
1 parent 2bcc415 commit 9f18fd4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-phones-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-lqip': patch
---

fixed errors thrown by the fileURLToPath method (#6).
9 changes: 2 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,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 +21,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 +34,7 @@ export default function vitePluginLqip(options?: LQIPPluginOptions): Plugin {
return null;
}

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

0 comments on commit 9f18fd4

Please sign in to comment.