@masx200/vite-plugin-virtual-http-resolve
v1.0.1
Published
A Vite plugin that resolves remote HTTP/HTTPS modules to virtual modules with caching support
Downloads
63
Maintainers
Readme
@masx200/vite-plugin-virtual-http-resolve
A Vite plugin that resolves remote HTTP/HTTPS modules to virtual modules with caching support.
Features
- 🚀 Resolves remote HTTP/HTTPS modules as virtual modules
- 📦 Built-in caching (memory and file system)
- 🔧 Customizable fetcher function
- 🎯 Handles relative imports from remote modules
- 🛡️ Variable conflict resolution for node modules
- ⚡ Fast and efficient with pre-processing
Installation
npm install @masx200/vite-plugin-virtual-http-resolve
# or
yarn add @masx200/vite-plugin-virtual-http-resolve
# or
pnpm add @masx200/vite-plugin-virtual-http-resolveUsage
Basic Usage
// vite.config.js
import { defineConfig } from "vite";
import virtualHttpResolve from "@masx200/vite-plugin-virtual-http-resolve";
export default defineConfig({
plugins: [virtualHttpResolve()],
});With Custom Options
// vite.config.js
import { defineConfig } from "vite";
import virtualHttpResolve, {
FileCache,
} from "@masx200/vite-plugin-virtual-http-resolve";
export default defineConfig({
plugins: [
virtualHttpResolve({
// Use file cache instead of memory cache
cache: new FileCache("./custom-cache-folder"),
// Custom fetcher function
fetcher: async (url) => {
const response = await fetch(url);
if (response.ok) return await response.text();
throw Error("failed to fetch:" + url);
},
}),
],
});In Your Code
// Import from remote HTTP/HTTPS URL
import React from "virtual:https://esm.sh/[email protected]";
import ReactDOM from "virtual:https://esm.sh/[email protected]";
// The plugin will automatically handle relative imports
// from these remote modulesAPI
virtualHttpResolve(options?)
Options
cache(CacheType) - Cache implementation for storing fetched modules. Defaults to in-memory cache.fetcher(url: string) => Promise<string>- Custom fetcher function for HTTP requests.
CacheType Interface
interface CacheType {
has(key: string): Promise<boolean> | boolean;
set(key: string, value: string): Promise<any> | any;
get(key: string): Promise<string | undefined> | string | undefined;
}FileCache
A file system-based cache implementation.
const cache = new FileCache(cacheFolder?: string)How It Works
- Virtual Module Resolution: Converts HTTP/HTTPS URLs to virtual modules
(
virtual:https://...) - Relative Import Handling: Resolves relative imports from remote modules
- Caching: Stores fetched modules to avoid repeated requests
- Variable Conflict Resolution: Automatically resolves variable name conflicts in node modules
- Pre-processing: Handles special cases like chunk files and Skypack-style redirects
License
MIT
