{"version":3,"file":"webPluginVite.mjs","names":[],"sources":["../../../src/builders/web/webPluginVite.ts"],"sourcesContent":["import fs from 'node:fs';\nimport { createRequire } from 'node:module';\nimport path from 'node:path';\n\nimport { readSyncRegistration } from '@agimon-ai/doompi-core/sync-registration';\nimport type { Alias, Plugin } from 'vite';\n\nimport { devPluginRoots, PLUGIN_ROOTS_ENV, PLUGIN_ROOTS_FILE } from './webDevRoots';\nimport type { SyncGeneratedModules } from './webPluginGenerate';\n\nconst DOOMPI_ROOT_ENV = 'DOOMPI_ROOT';\n\n/**\n * The modules the cockpit supplies page-wide, rather than each plugin\n * carrying its own copy.\n *\n * Build-time metadata, and it stays here rather than in a browser-facing\n * barrel on purpose: exporting it from doompi-core/web put the literal list\n * into the shipped cockpit bundle, where a plugin's own specifier strings\n * would then appear as data.\n */\nexport const WEB_PLUGIN_RUNTIME_SPECIFIERS = [\n  'react',\n  'react/jsx-runtime',\n  'react/jsx-dev-runtime',\n  'react-dom',\n  'react-dom/client',\n  '@tanstack/store',\n  '@tanstack/react-store',\n  '@agimon-ai/doompi-core/web',\n  '@agimon-ai/doompi-web-components',\n  '@agimon-ai/doompi-web-security/browser',\n  '@codemirror/state',\n  '@codemirror/view',\n] as const;\n\nconst WEB_PLUGIN_RUNTIME_PROPERTIES: Record<(typeof WEB_PLUGIN_RUNTIME_SPECIFIERS)[number], string> = {\n  react: 'react',\n  'react/jsx-runtime': 'reactJsxRuntime',\n  'react/jsx-dev-runtime': 'reactJsxDevRuntime',\n  'react-dom': 'reactDom',\n  'react-dom/client': 'reactDomClient',\n  '@tanstack/store': 'tanstackStore',\n  '@tanstack/react-store': 'tanstackReactStore',\n  '@agimon-ai/doompi-core/web': 'webContracts',\n  '@agimon-ai/doompi-web-components': 'webComponents',\n  '@agimon-ai/doompi-web-security/browser': 'webSecurityBrowser',\n  '@codemirror/state': 'codemirrorState',\n  '@codemirror/view': 'codemirrorView',\n};\n\n/** Rollup expression for a shared module supplied by the session shell. */\nexport function webPluginRuntimeGlobal(specifier: (typeof WEB_PLUGIN_RUNTIME_SPECIFIERS)[number]): string {\n  return `globalThis.DoomPiWebPluginRuntime.${WEB_PLUGIN_RUNTIME_PROPERTIES[specifier]}`;\n}\n\n/** Resolves generated JSX imports from the host instead of a plugin's optional React peer tree. */\nexport function webPluginRuntimeAliases(clientRoot: string): Alias[] {\n  const runtimeRequire = createRequire(path.join(clientRoot, 'index.html'));\n  return WEB_PLUGIN_RUNTIME_SPECIFIERS.filter((specifier) => specifier.startsWith('react/jsx-')).map((specifier) => ({\n    find: new RegExp(`^${specifier.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}$`, 'u'),\n    replacement: runtimeRequire.resolve(specifier),\n  }));\n}\n\n/**\n * Swaps the committed builtin registry modules for generated ones. A\n * resolveId hook rather than resolve.alias because aliases match raw import\n * specifiers while these two are known by their resolved paths. Both the\n * sync-time build and the dev server use it, so a plugin edit in dev is the\n * same module the bundle would carry.\n */\nexport function webPluginOverridePlugin(clientRoot: string, generated: SyncGeneratedModules): Plugin {\n  const overrides = new Map([\n    [path.join(clientRoot, 'app', 'webPlugins.generated.ts'), generated.clientModulePath],\n    [path.join(clientRoot, 'styles', 'webPluginSources.generated.css'), generated.cssModulePath],\n  ]);\n  return {\n    name: 'doompi-web-plugin-overrides',\n    enforce: 'pre',\n    async resolveId(source, importer, options) {\n      const resolved = await this.resolve(source, importer, { ...options, skipSelf: true });\n      if (!resolved) return null;\n      return overrides.get(resolved.id) ?? null;\n    },\n  };\n}\n\n/**\n * The CSS sources module cannot ride the resolveId hook: Vite and Tailwind\n * resolve CSS @import through Vite's own id resolver, which consults aliases\n * but no plugin hooks. Without this the builtin (empty) @source list ships\n * and every plugin-only utility class is missing.\n */\nexport function webPluginCssAlias(generated: SyncGeneratedModules): Alias {\n  return { find: /^\\.\\/webPluginSources\\.generated\\.css$/, replacement: generated.cssModulePath };\n}\n\n/** The plugin package roots the dev server should serve, from an explicit override or DOOMPI_ROOT. */\nexport function readDevPluginRoots(environment: NodeJS.ProcessEnv, homeDirectory: string): string[] {\n  const explicit = environment[PLUGIN_ROOTS_ENV];\n  if (explicit !== undefined && explicit !== '') {\n    return devPluginRoots({ envValue: explicit, rootsFileText: undefined, delimiter: path.delimiter });\n  }\n  const repoRoot = environment[DOOMPI_ROOT_ENV];\n  if (repoRoot === undefined || repoRoot === '') return [];\n  try {\n    const webDirectory = readSyncRegistration(repoRoot, homeDirectory)?.webDirectory;\n    const rootsFile =\n      webDirectory === null || webDirectory === undefined\n        ? undefined\n        : path.join(path.dirname(webDirectory), PLUGIN_ROOTS_FILE);\n    return devPluginRoots({\n      envValue: undefined,\n      rootsFileText:\n        rootsFile !== undefined && fs.existsSync(rootsFile) ? fs.readFileSync(rootsFile, 'utf8') : undefined,\n      delimiter: path.delimiter,\n    });\n  } catch {\n    return [];\n  }\n}\n"],"mappings":";;;;;;AAUA,MAAM,kBAAkB;;;;;;;;;;AAWxB,MAAa,gCAAgC;CAC3C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAM,gCAAgG;CACpG,OAAO;CACP,qBAAqB;CACrB,yBAAyB;CACzB,aAAa;CACb,oBAAoB;CACpB,mBAAmB;CACnB,yBAAyB;CACzB,8BAA8B;CAC9B,oCAAoC;CACpC,0CAA0C;CAC1C,qBAAqB;CACrB,oBAAoB;AACtB;;AAGA,SAAgB,uBAAuB,WAAmE;CACxG,OAAO,qCAAqC,8BAA8B;AAC5E;;AAGA,SAAgB,wBAAwB,YAA6B;CACnE,MAAM,iBAAiB,cAAc,KAAK,KAAK,YAAY,YAAY,CAAC;CACxE,OAAO,8BAA8B,QAAQ,cAAc,UAAU,WAAW,YAAY,CAAC,CAAC,CAAC,KAAK,eAAe;EACjH,MAAM,IAAI,OAAO,IAAI,UAAU,QAAQ,uBAAuB,MAAM,EAAE,IAAI,GAAG;EAC7E,aAAa,eAAe,QAAQ,SAAS;CAC/C,EAAE;AACJ;;;;;;;;AASA,S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