UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

38 lines (37 loc) 1.59 kB
const VIRTUAL_PREFIX = "\0one-env-guard:", GUARD_SPECIFIERS = ["server-only", "client-only", "native-only", "web-only"], ALLOWED_ENVIRONMENTS = { "server-only": ["ssr"], "client-only": ["client"], "native-only": ["ios", "android"], "web-only": ["client", "ssr"] }; function resolveEnvironmentGuard(specifier, envName, options) { return GUARD_SPECIFIERS.includes(specifier) ? options?.disabled || options?.disableGuards?.includes(specifier) ? `${VIRTUAL_PREFIX}${specifier}:disabled` : `${VIRTUAL_PREFIX}${specifier}:${envName}` : null; } function loadEnvironmentGuard(id) { if (!id.startsWith(VIRTUAL_PREFIX)) return null; const rest = id.slice(VIRTUAL_PREFIX.length), lastColon = rest.lastIndexOf(":"); if (lastColon === -1) return null; const specifier = rest.slice(0, lastColon), envName = rest.slice(lastColon + 1); if (envName === "disabled") return "export {}"; const allowed = ALLOWED_ENVIRONMENTS[specifier]; return allowed ? allowed.includes(envName) ? "export {}" : `throw new Error("${specifier} cannot be imported in the \\"${envName}\\" environment")` : null; } function environmentGuardPlugin(options) { return { name: "one:environment-guard", enforce: "pre", resolveId(source) { const envName = this.environment?.name; return envName ? resolveEnvironmentGuard(source, envName, options) : null; }, load(id) { return loadEnvironmentGuard(id); } }; } export { environmentGuardPlugin, loadEnvironmentGuard, resolveEnvironmentGuard }; //# sourceMappingURL=environmentGuardPlugin.mjs.map