astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
55 lines (54 loc) • 1.72 kB
JavaScript
import { fileURLToPath } from "node:url";
import { SessionStorageInitError } from "../errors/errors-data.js";
import { AstroError } from "../errors/index.js";
import { normalizeSessionDriverConfig } from "./utils.js";
const VIRTUAL_SESSION_DRIVER_ID = "virtual:astro:session-driver";
const RESOLVED_VIRTUAL_SESSION_DRIVER_ID = "\0" + VIRTUAL_SESSION_DRIVER_ID;
function vitePluginSessionDriver({ settings }) {
return {
name: VIRTUAL_SESSION_DRIVER_ID,
enforce: "pre",
resolveId: {
filter: {
id: new RegExp(`^${VIRTUAL_SESSION_DRIVER_ID}$`)
},
handler() {
return RESOLVED_VIRTUAL_SESSION_DRIVER_ID;
}
},
load: {
filter: {
id: new RegExp(`^${RESOLVED_VIRTUAL_SESSION_DRIVER_ID}$`)
},
async handler() {
if (!settings.config.session?.driver) {
return { code: "export default null;" };
}
const driver = normalizeSessionDriverConfig(
settings.config.session.driver,
settings.config.session.options
);
const importerPath = fileURLToPath(import.meta.url);
const resolved = await this.resolve(driver.entrypoint, importerPath);
if (!resolved) {
throw new AstroError({
...SessionStorageInitError,
message: SessionStorageInitError.message(
`Failed to resolve session driver: ${driver.entrypoint}`,
driver.entrypoint
)
});
}
return {
code: `import { default as _default } from '${resolved.id}';
export * from '${resolved.id}';
export default _default;`
};
}
}
};
}
export {
VIRTUAL_SESSION_DRIVER_ID,
vitePluginSessionDriver
};