UNPKG

one

Version:

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

65 lines (64 loc) 2.89 kB
var VIRTUAL_PREFIX = "\0one-env-guard:"; var GUARD_SPECIFIERS = ["server-only", "client-only", "native-only", "web-only"]; var ALLOWED_BY_NAME = { "native-only": ["ios", "android"], "web-only": ["client", "ssr"] }; var ALLOWED_BY_CONSUMER = { "server-only": "server", "client-only": "client" }; function resolveEnvironmentGuard(specifier, envName, consumer, options) { var _options_disableGuards; if (!GUARD_SPECIFIERS.includes(specifier)) { return null; } if ((options === null || options === void 0 ? void 0 : options.disabled) || (options === null || options === void 0 ? void 0 : (_options_disableGuards = options.disableGuards) === null || _options_disableGuards === void 0 ? void 0 : _options_disableGuards.includes(specifier))) { return `${VIRTUAL_PREFIX}${specifier}:disabled`; } return `${VIRTUAL_PREFIX}${specifier}:${envName}:${consumer !== null && consumer !== void 0 ? consumer : "unknown"}`; } function loadEnvironmentGuard(id) { if (!id.startsWith(VIRTUAL_PREFIX)) { return null; } var rest = id.slice(VIRTUAL_PREFIX.length); var parts = rest.split(":"); if (parts.length === 2 && parts[1] === "disabled") { return "export {}"; } var specifier = parts[0]; var envName = parts[1]; var consumer = parts[2]; var requiredConsumer = ALLOWED_BY_CONSUMER[specifier]; if (requiredConsumer) { if (consumer === requiredConsumer) return "export {}"; if (!consumer || consumer === "unknown") { var legacyAllowed = requiredConsumer === "server" ? ["ssr"] : ["client"]; if (legacyAllowed.includes(envName)) return "export {}"; } return `throw new Error("${specifier} cannot be imported in the \\"${envName}\\" environment")`; } var allowedNames = ALLOWED_BY_NAME[specifier]; if (!allowedNames) return null; if (allowedNames.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) { var _this_environment, _this_environment_config, _this_environment1; var envName = (_this_environment = this.environment) === null || _this_environment === void 0 ? void 0 : _this_environment.name; if (!envName) return null; var consumer = (_this_environment1 = this.environment) === null || _this_environment1 === void 0 ? void 0 : (_this_environment_config = _this_environment1.config) === null || _this_environment_config === void 0 ? void 0 : _this_environment_config.consumer; return resolveEnvironmentGuard(source, envName, consumer, options); }, load(id) { return loadEnvironmentGuard(id); } }; } export { environmentGuardPlugin, loadEnvironmentGuard, resolveEnvironmentGuard }; //# sourceMappingURL=environmentGuardPlugin.native.js.map