{"version":3,"file":"index.cjs","names":["INTLAYER_NO_EXTERNAL_PATTERN","getConfiguration","prepareIntlayer","viteIntlayerPlugin","getAlias","resolve","emitRewrittenPages"],"sources":["../../src/index.ts"],"sourcesContent":["import { resolve } from 'node:path';\nimport { getConfiguration } from '@intlayer/config/node';\nimport { getAlias } from '@intlayer/config/utils';\nimport { prepareIntlayer } from '@intlayer/engine/build';\nimport type { AstroIntegration } from 'astro';\nimport type { PluginOption } from 'vite';\nimport {\n  INTLAYER_NO_EXTERNAL_PATTERN,\n  intlayer as viteIntlayerPlugin,\n} from 'vite-intlayer';\nimport { emitRewrittenPages } from './emitRewrittenPages';\n\n/**\n * Keeps the intlayer packages out of Node's native module loader in *every*\n * Vite environment Astro renders from.\n *\n * A static dev server renders from `ssr`, but other setups render from Astro's\n * `astro` or `prerender` environments, and a top-level `ssr.noExternal` only\n * seeds the `ssr` one. Declaring it per environment covers the rest.\n */\nconst intlayerNoExternalEnvironments = (): PluginOption => ({\n  name: 'astro-intlayer-no-external',\n  configEnvironment: () => ({\n    resolve: { noExternal: [INTLAYER_NO_EXTERNAL_PATTERN] },\n  }),\n});\n\n/**\n * Astro integration for Intlayer.\n *\n * It handles:\n * 1. Preparing Intlayer resources (dictionaries) at config setup.\n * 2. Injecting Vite plugins for aliases, locale-based routing (middleware), and build optimizations (prune).\n * 3. Configuring Vite aliases for dictionary access.\n * 4. Emitting the prerendered pages at their rewritten (localized) URLs.\n *\n * The dev-time content watcher is not started here: the bundled\n * `vite-intlayer` plugin already starts one from `configureServer`, and\n * `watch()` subscribes anew on every call, so doing both would rebuild each\n * content edit twice.\n *\n * @returns An Astro integration object.\n *\n * @example\n * ```ts\n * // astro.config.mjs\n * import { defineConfig } from 'astro/config';\n * import { intlayer } from 'astro-intlayer';\n *\n * export default defineConfig({\n *   integrations: [intlayer()],\n * });\n * ```\n */\nexport const intlayer = (): AstroIntegration =>\n  ({\n    name: 'astro-intlayer',\n    hooks: {\n      'astro:config:setup': async ({ updateConfig }) => {\n        const configuration = getConfiguration();\n\n        // Prepare once per process start to ensure generated entries exist\n        await prepareIntlayer(configuration);\n\n        updateConfig({\n          vite: {\n            plugins: [\n              viteIntlayerPlugin(),\n              intlayerNoExternalEnvironments(),\n            ] as PluginOption[],\n            resolve: {\n              alias: {\n                ...getAlias({\n                  configuration,\n                  formatter: (value) => resolve(value),\n                }),\n              },\n            },\n            // `astro-intlayer` is tagged with the `astro` keyword, so Astro\n            // treats it as an Astro package and crawls its dependency tree\n            // (`vitefu`), force-externalizing every dependency it finds —\n            // `@intlayer/core`, `@intlayer/config`, … — into\n            // `resolve.external`. Vite checks `external` before `noExternal`,\n            // so the `ssr.noExternal` that `vite-intlayer` returns from its\n            // Vite `config` hook loses that race and the packages are loaded\n            // natively by Node, stranding dictionary edits behind Node's\n            // require cache. Declaring it here instead runs before the crawl,\n            // which drops explicitly no-externalized packages from its result.\n            ssr: {\n              noExternal: [INTLAYER_NO_EXTERNAL_PATTERN],\n            },\n          },\n        });\n      },\n\n      // Astro renders each page from its canonical file-system route, so a\n      // static build has no file for the localized paths declared in\n      // `routing.rewrite`. Mirror them here, otherwise the URLs produced by\n      // `getLocalizedUrl` (links, hreflang, sitemap) 404 once deployed.\n      'astro:build:done': async ({ dir, logger }) => {\n        const configuration = getConfiguration();\n\n        const emittedPages = await emitRewrittenPages(configuration, dir);\n\n        if (emittedPages.length > 0) {\n          logger.info(\n            `Emitted ${emittedPages.length} rewritten page(s): ${emittedPages\n              .map(([from, to]) => `${from} \\u2192 ${to}`)\n              .join(', ')}`\n          );\n        }\n      },\n    },\n  }) satisfies AstroIntegration;\n\n/**\n * Alias of {@link intlayer}, kept so `astro add astro-intlayer` works.\n *\n * Astro's CLI codemod always writes a default import\n * (`import intlayer from 'astro-intlayer'`) without checking what the package\n * exports, so the integration has to be reachable that way too. The named\n * export stays the documented one.\n */\nexport default intlayer;\n"],"mappings":";;;;;;;;;;;;;;;;;AAoBA,MAAM,wCAAsD;CAC1D,MAAM;CACN,0BAA0B,EACxB,SAAS,EAAE,YAAY,CAACA,0CAA4B,EAAE,EACxD;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,kBACV;CACC,MAAM;CACN,OAAO;EACL,sBAAsB,OAAO,EAAE,mBAAmB;GAChD,MAAM,oBAAgBC,wCAAiB;GAGvC,UAAMC,wCAAgB,aAAa;GAEnC,aAAa,EACX,MAAM;IACJ,SAAS,KACPC,wBAAmB,GACnB,+BAA+B,CACjC;IACA,SAAS,EACP,OAAO,EACL,OAAGC,iCAAS;KACV;KACA,YAAY,cAAUC,mBAAQ,KAAK;IACrC,CAAC,EACH,EACF;IAWA,KAAK,EACH,YAAY,CAACL,0CAA4B,EAC3C;GACF,EACF,CAAC;EACH;EAMA,oBAAoB,OAAO,EAAE,KAAK,aAAa;GAC7C,MAAM,oBAAgBC,wCAAiB;GAEvC,MAAM,eAAe,MAAMK,8CAAmB,eAAe,GAAG;GAEhE,IAAI,aAAa,SAAS,GACxB,OAAO,KACL,WAAW,aAAa,OAAO,sBAAsB,aAClD,KAAK,CAAC,MAAM,QAAQ,GAAG,KAAK,UAAU,IAAI,CAAC,CAC3C,KAAK,IAAI,GACd;EAEJ;CACF;AACF"}