-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79c2a50
commit ca17144
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./logger"; | ||
export * from "./plugin"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
interface PluginAsset { | ||
filename: string; | ||
url: string; | ||
} | ||
|
||
const registeredAssets: PluginAsset[] = []; | ||
const moduleCache = new Map(); | ||
|
||
export async function registerPluginAssets(assets: PluginAsset[]) { | ||
if (!Array.isArray(assets)) { | ||
throw new TypeError(`Expected an array, got ${typeof assets}`); | ||
} | ||
|
||
for (const asset of assets) { | ||
if ( | ||
!registeredAssets.some((existing) => existing.filename === asset.filename) | ||
) { | ||
registeredAssets.push(asset); | ||
} | ||
} | ||
} | ||
|
||
export function resolvePluginAsset(filename: string) { | ||
if (registeredAssets.length === 0) { | ||
throw new Error("Plugin assets not registered"); | ||
} | ||
|
||
const asset = registeredAssets.find((asset) => asset.filename === filename); | ||
|
||
if (!asset) { | ||
throw new Error(`Plugin asset "${filename}" not found`); | ||
} | ||
|
||
return asset; | ||
} | ||
|
||
export async function loadPluginModule(filename: string) { | ||
if (!filename.endsWith(".js")) { | ||
filename += ".js"; | ||
} | ||
|
||
if (moduleCache.has(filename)) { | ||
return moduleCache.get(filename); | ||
} | ||
|
||
const asset = resolvePluginAsset(filename); | ||
const mod = await import(/* @vite-ignore */ asset.url); | ||
moduleCache.set(filename, mod); | ||
return mod; | ||
} |