UNPKG

one

Version:

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

79 lines 2.68 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var environmentGuardPlugin_exports = {}; __export(environmentGuardPlugin_exports, { environmentGuardPlugin: () => environmentGuardPlugin, loadEnvironmentGuard: () => loadEnvironmentGuard, resolveEnvironmentGuard: () => resolveEnvironmentGuard }); module.exports = __toCommonJS(environmentGuardPlugin_exports); const VIRTUAL_PREFIX = "\0one-env-guard:"; const GUARD_SPECIFIERS = ["server-only", "client-only", "native-only", "web-only"]; const ALLOWED_ENVIRONMENTS = { "server-only": ["ssr"], "client-only": ["client"], "native-only": ["ios", "android"], "web-only": ["client", "ssr"] }; function resolveEnvironmentGuard(specifier, envName, options) { if (!GUARD_SPECIFIERS.includes(specifier)) { return null; } if (options?.disabled || options?.disableGuards?.includes(specifier)) { return `${VIRTUAL_PREFIX}${specifier}:disabled`; } return `${VIRTUAL_PREFIX}${specifier}:${envName}`; } function loadEnvironmentGuard(id) { if (!id.startsWith(VIRTUAL_PREFIX)) { return null; } const rest = id.slice(VIRTUAL_PREFIX.length); const lastColon = rest.lastIndexOf(":"); if (lastColon === -1) return null; const specifier = rest.slice(0, lastColon); const envName = rest.slice(lastColon + 1); if (envName === "disabled") { return "export {}"; } const allowed = ALLOWED_ENVIRONMENTS[specifier]; if (!allowed) return null; if (allowed.includes(envName)) { return "export {}"; } return `throw new Error("${specifier} cannot be imported in the \\"${envName}\\" environment")`; } function environmentGuardPlugin(options) { return { name: "one:environment-guard", enforce: "pre", resolveId(source) { const envName = this.environment?.name; if (!envName) return null; return resolveEnvironmentGuard(source, envName, options); }, load(id) { return loadEnvironmentGuard(id); } }; }