{"version":3,"file":"vite.mjs","names":["resolveEsbuildOptions","resolveRolldownOptions"],"sources":["../src/vite.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                   🗲 Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/powerlines\n Documentation:            https://docs.stormsoftware.com/projects/powerlines\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n  ExecutionContext,\n  UnpluginOptions,\n  UnresolvedContext\n} from \"@powerlines/core\";\nimport { isDevelopmentMode, isTestMode } from \"@stryke/env/environment-checks\";\nimport { appendPath } from \"@stryke/path/append\";\nimport { relativePath } from \"@stryke/path/file-path-fns\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport defu from \"defu\";\nimport { createVitePlugin } from \"unplugin\";\nimport { ESBuildOptions, UserConfig } from \"vite\";\nimport { resolveOptions as resolveEsbuildOptions } from \"./esbuild\";\nimport { resolveOptions as resolveRolldownOptions } from \"./rolldown\";\nimport {\n  createUnpluginFactory,\n  UnpluginFactoryDecorator,\n  UnpluginFactoryOptions\n} from \"./unplugin\";\n\nexport const DEFAULT_OPTIONS: Partial<UserConfig> = {\n  resolve: {\n    extensions: [\".mjs\", \".js\", \".mts\", \".ts\", \".jsx\", \".tsx\", \".json\"]\n  },\n  json: {\n    stringify: true\n  },\n  logLevel: \"silent\",\n  clearScreen: true\n};\n\n/**\n * Resolves the options for [vite](https://vitejs.dev/).\n *\n * @param context - The build context.\n * @returns The resolved options.\n */\nexport function resolveOptions<TContext extends UnresolvedContext>(\n  context: TContext\n): UserConfig {\n  return defu(\n    {\n      define: context.config.define,\n      root: appendPath(context.config.root, context.config.cwd),\n      platform: context.config.platform,\n      mode:\n        context.config.mode === \"development\" ? \"development\" : \"production\",\n      cacheDir: joinPaths(context.cachePath, \"vite\"),\n      build: {\n        minify: context.config.output.minify,\n        metafile: context.config.mode === \"development\",\n        sourcemap: context.config.output.sourceMap,\n        outDir: relativePath(\n          appendPath(context.config.root, context.config.cwd),\n          context.config.output.path\n        ),\n        tsconfig: appendPath(\n          context.tsconfig.tsconfigFilePath,\n          context.config.cwd\n        ),\n        tsconfigRaw: context.tsconfig.tsconfigJson\n      },\n      resolve: {\n        alias: Object.entries(context.alias).reduce(\n          (ret, [id, path]) => {\n            if (!ret.find(e => e.find === id)) {\n              ret.push({\n                find: id,\n                replacement: path\n              });\n            } else {\n              context.warn(\n                `Duplicate alias entry for '${id}' detected. The first entry will be used.`\n              );\n            }\n\n            return ret;\n          },\n          [] as { find: string; replacement: string }[]\n        ),\n        dedupe: context.config.resolve.dedupe,\n        mainFields: context.config.resolve.mainFields,\n        conditions: context.config.resolve.conditions,\n        extensions: context.config.resolve.extensions\n      },\n      esbuild: resolveEsbuildOptions(context) as ESBuildOptions,\n      optimizeDeps: {\n        extensions: context.config.resolve.extensions,\n        rolldownOptions: resolveRolldownOptions(context),\n        esbuildOptions: resolveEsbuildOptions(context)\n      },\n      logLevel:\n        context.config.logLevel.general === \"trace\"\n          ? \"info\"\n          : context.config.logLevel.general === \"debug\"\n            ? \"warn\"\n            : \"error\",\n      clearScreen: true,\n      envDir: context.config.root\n    } as UserConfig,\n    DEFAULT_OPTIONS\n  );\n}\n\n/**\n * Creates a Vite plugin factory that generates a plugin instance.\n *\n * @see https://vitejs.dev/guide/api-plugin.html#plugin-api\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import { createViteFactory } from \"@powerlines/unplugin/vite\";\n *\n * const powerlinesPlugin = createViteFactory({ name: \"example-app\", ... });\n *\n * export default defineConfig({\n *   plugins: [powerlinesPlugin()],\n * });\n *\n * ```\n *\n * @param options - The options to create the plugin factory with.\n * @param decorate - A function to decorate the plugin options with additional properties or hooks. This can be used to add custom behavior to the plugin instance, such as additional hooks or configuration options. The function receives the generated plugin options and should return an object containing any additional properties or hooks to be merged into the final plugin options.\n * @returns A function that generates a Vite plugin instance when called. The generated plugin will invoke the Powerlines API hooks during the build process, allowing you to integrate Powerlines into your Vite build.\n */\nexport function createViteFactory<TContext extends ExecutionContext>(\n  options: Omit<UnpluginFactoryOptions, \"variant\"> = {},\n  decorate: UnpluginFactoryDecorator<TContext> = options => options\n) {\n  return createUnpluginFactory({ ...options, variant: \"vite\" }, unplugin =>\n    decorate({\n      ...(unplugin as UnpluginOptions<TContext>),\n      vite: {\n        sharedDuringBuild: true,\n\n        async hotUpdate(opts) {\n          const environment = await unplugin.context.getEnvironment();\n\n          return unplugin.context.callHook(\n            \"vite:hotUpdate\",\n            { environment },\n            opts\n          );\n        },\n        async config(config, env) {\n          unplugin.context.config.mode = isDevelopmentMode(env.mode)\n            ? \"development\"\n            : isTestMode(env.mode)\n              ? \"test\"\n              : \"production\";\n\n          const environment = await unplugin.context.getEnvironment();\n          const result = await unplugin.context.callHook(\n            \"vite:config\",\n            { environment },\n            config,\n            env\n          );\n\n          return defu(\n            resolveOptions(unplugin.context),\n            result?.build ?? {},\n            config\n          ) as Omit<UserConfig, \"plugins\">;\n        },\n        async configResolved(_config) {\n          const environment = await unplugin.context.getEnvironment();\n\n          await unplugin.context.callHook(\"configResolved\", { environment });\n        },\n        async configureServer(server) {\n          const environment = await unplugin.context.getEnvironment();\n\n          return unplugin.context.callHook(\n            \"vite:configureServer\",\n            { environment },\n            server\n          );\n        },\n        async configurePreviewServer(server) {\n          const environment = await unplugin.context.getEnvironment();\n\n          return unplugin.context.callHook(\n            \"vite:configurePreviewServer\",\n            { environment },\n            server\n          );\n        },\n        async transformIndexHtml(html, ctx) {\n          const environment = await unplugin.context.getEnvironment();\n\n          return unplugin.context.callHook(\n            \"vite:transformIndexHtml\",\n            { environment },\n            html,\n            ctx\n          );\n        },\n        async handleHotUpdate(ctx) {\n          const environment = await unplugin.context.getEnvironment();\n\n          return unplugin.context.callHook(\n            \"vite:handleHotUpdate\",\n            { environment },\n            ctx\n          );\n        }\n      }\n    })\n  );\n}\n\n/**\n * A Vite plugin that will invoke the Powerlines API hooks during the build process.\n *\n * @see https://vitejs.dev/guide/api-plugin.html#plugin-api\n *\n * @example\n * ```ts\n * // vite.config.ts\n * import powerlines from \"@powerlines/unplugin/vite\";\n *\n * export default defineConfig({\n *   plugins: [powerlines({ name: \"example-app\", ... })],\n * });\n *\n * ```\n */\nexport const plugin = createVitePlugin(createViteFactory());\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;AAsCA,MAAa,kBAAuC;CAClD,SAAS,EACP,YAAY;EAAC;EAAQ;EAAO;EAAQ;EAAO;EAAQ;EAAQ;CAAO,EACpE;CACA,MAAM,EACJ,WAAW,KACb;CACA,UAAU;CACV,aAAa;AACf;;;;;;;AAQA,SAAgB,eACd,SACY;CACZ,OAAO,KACL;EACE,QAAQ,QAAQ,OAAO;EACvB,MAAM,WAAW,QAAQ,OAAO,MAAM,QAAQ,OAAO,GAAG;EACxD,UAAU,QAAQ,OAAO;EACzB,MACE,QAAQ,OAAO,SAAS,gBAAgB,gBAAgB;EAC1D,UAAU,UAAU,QAAQ,WAAW,MAAM;EAC7C,OAAO;GACL,QAAQ,QAAQ,OAAO,OAAO;GAC9B,UAAU,QAAQ,OAAO,SAAS;GAClC,WAAW,QAAQ,OAAO,OAAO;GACjC,QAAQ,aACN,WAAW,QAAQ,OAAO,MAAM,QAAQ,OAAO,GAAG,GAClD,QAAQ,OAAO,OAAO,IACxB;GACA,UAAU,WACR,QAAQ,SAAS,kBACjB,QAAQ,OAAO,GACjB;GACA,aAAa,QAAQ,SAAS;EAChC;EACA,SAAS;GACP,OAAO,OAAO,QAAQ,QAAQ,KAAK,CAAC,CAAC,QAClC,KAAK,CAAC,IAAI,UAAU;IACnB,IAAI,CAAC,IAAI,MAAK,MAAK,EAAE,SAAS,EAAE,GAC9B,IAAI,KAAK;KACP,MAAM;KACN,aAAa;IACf,CAA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