{"version":3,"file":"environmentGuardPlugin.js","names":[],"sources":["esm/vite/plugins/environmentGuardPlugin.js"],"sourcesContent":["import { prefixRegex } from \"rolldown/filter\";\nconst VIRTUAL_PREFIX = \"\\0one-env-guard:\";\nconst GUARD_SPECIFIERS = [\n  \"server-only\",\n  \"client-only\",\n  \"native-only\",\n  \"web-only\"\n];\nconst GUARD_SPECIFIERS_RE = new RegExp(`^(?:${GUARD_SPECIFIERS.join(\"|\")})$`);\nconst ALLOWED_BY_NAME = {\n  \"native-only\": [\"ios\", \"android\"],\n  \"web-only\": [\"client\", \"ssr\", \"worker\"]\n};\nconst ALLOWED_BY_CONSUMER = {\n  \"server-only\": \"server\",\n  \"client-only\": \"client\"\n};\nfunction resolveEnvironmentGuard(specifier, envName, consumer, options) {\n  if (!GUARD_SPECIFIERS.includes(specifier)) {\n    return null;\n  }\n  if (options?.disabled || options?.disableGuards?.includes(specifier)) {\n    return `${VIRTUAL_PREFIX}${specifier}:disabled`;\n  }\n  return `${VIRTUAL_PREFIX}${specifier}:${envName}:${consumer ?? \"unknown\"}`;\n}\nfunction loadEnvironmentGuard(id) {\n  if (!id.startsWith(VIRTUAL_PREFIX)) {\n    return null;\n  }\n  const rest = id.slice(VIRTUAL_PREFIX.length);\n  const parts = rest.split(\":\");\n  if (parts.length === 2 && parts[1] === \"disabled\") {\n    return \"export {}\";\n  }\n  const specifier = parts[0];\n  const envName = parts[1];\n  const consumer = parts[2];\n  const requiredConsumer = ALLOWED_BY_CONSUMER[specifier];\n  if (requiredConsumer) {\n    if (consumer === requiredConsumer) return \"export {}\";\n    if (!consumer || consumer === \"unknown\") {\n      const legacyAllowed = requiredConsumer === \"server\" ? [\"ssr\"] : [\"client\"];\n      if (legacyAllowed.includes(envName)) return \"export {}\";\n    }\n    return `throw new Error(\"${specifier} cannot be imported in the \\\\\"${envName}\\\\\" environment\")`;\n  }\n  const allowedNames = ALLOWED_BY_NAME[specifier];\n  if (!allowedNames) return null;\n  if (allowedNames.includes(envName)) return \"export {}\";\n  return `throw new Error(\"${specifier} cannot be imported in the \\\\\"${envName}\\\\\" environment\")`;\n}\nfunction environmentGuardPlugin(options) {\n  return {\n    name: \"one:environment-guard\",\n    enforce: \"pre\",\n    // filters are evaluated in rust, so non-guard ids never cross into js.\n    // without them every import in the graph paid a napi round trip to be told\n    // it wasn't one of these four specifiers.\n    resolveId: {\n      filter: { id: GUARD_SPECIFIERS_RE },\n      handler(source) {\n        const envName = this.environment?.name;\n        if (!envName) return null;\n        const consumer = this.environment?.config?.consumer;\n        return resolveEnvironmentGuard(source, envName, consumer, options);\n      }\n    },\n    load: {\n      filter: { id: prefixRegex(VIRTUAL_PREFIX) },\n      handler(id) {\n        return loadEnvironmentGuard(id);\n      }\n    }\n  };\n}\nexport {\n  environmentGuardPlugin,\n  loadEnvironmentGuard,\n  resolveEnvironmentGuard\n};\n//# sourceMappingURL=environmentGuardPlugin.js.map\n"],"mappings":";;;AACA,MAAM,iBAAiB;AACvB,MAAM,mBAAmB;CACvB;CACA;CACA;CACA;AACF;AACA,MAAM,sBAAsB,IAAI,OAAO,OAAO,iBAAiB,KAAK,GAAG,EAAE,GAAG;AAC5E,MAAM,kBAAkB;CACtB,eAAe,CAAC,OAAO,SAAS;CAChC,YAAY;EAAC;EAAU;EAAO;CAAQ;AACxC;AACA,MAAM,sBAAsB;CAC1B,eAAe;CACf,eAAe;AACjB;AACA,SAAS,wBAAwB,WAAW,SAAS,UAAU,SAAS;CACtE,IAAI,CAAC,iBAAiB,SAAS,SAAS,GAAG;EACzC,OAAO;CACT;CACA,IAAI,SAAS,YAAY,SAAS,eAAe,SAAS,SAAS,GAAG;EACpE,OAAO,GAAG,iBAAiB,UAAU;CACvC;CACA,OAAO,GAAG,iBAAiB,UAAU,GAAG,QAAQ,GAAG,YAAY;AACjE;AACA,SAAS,qBAAqB,IAAI;CAChC,IAAI,CAAC,GAAG,WAAW,cAAc,GAAG;EAClC,OAAO;CACT;CACA,MAAM,OAAO,GAAG,MAAM,eAAe,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