one
Version:
One is a new React Framework that makes Vite serve both native and web.
68 lines (66 loc) • 2.89 kB
JavaScript
;
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: !0
});
},
__copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__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: !0
}), mod);
var environmentGuardPlugin_exports = {};
__export(environmentGuardPlugin_exports, {
environmentGuardPlugin: () => environmentGuardPlugin,
loadEnvironmentGuard: () => loadEnvironmentGuard,
resolveEnvironmentGuard: () => resolveEnvironmentGuard
});
module.exports = __toCommonJS(environmentGuardPlugin_exports);
var 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) {
var _options_disableGuards;
return GUARD_SPECIFIERS.includes(specifier) ? options?.disabled || !(options == null || (_options_disableGuards = options.disableGuards) === null || _options_disableGuards === void 0) && _options_disableGuards.includes(specifier) ? `${VIRTUAL_PREFIX}${specifier}:disabled` : `${VIRTUAL_PREFIX}${specifier}:${envName}` : null;
}
function loadEnvironmentGuard(id) {
if (!id.startsWith(VIRTUAL_PREFIX)) return null;
var rest = id.slice(VIRTUAL_PREFIX.length),
lastColon = rest.lastIndexOf(":");
if (lastColon === -1) return null;
var specifier = rest.slice(0, lastColon),
envName = rest.slice(lastColon + 1);
if (envName === "disabled") return "export {}";
var 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) {
var _this_environment,
envName = (_this_environment = this.environment) === null || _this_environment === void 0 ? void 0 : _this_environment.name;
return envName ? resolveEnvironmentGuard(source, envName, options) : null;
},
load(id) {
return loadEnvironmentGuard(id);
}
};
}
//# sourceMappingURL=environmentGuardPlugin.native.js.map