{"version":3,"file":"esbuild.mjs","names":[],"sources":["../src/esbuild.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 type {\n  ExecutionContext,\n  ResolvedEntryFileReference,\n  UnpluginOptions,\n  UnresolvedContext\n} from \"@powerlines/core\";\nimport { resolveEntryOutput } from \"@powerlines/core/lib/entry\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport { replaceExtension, replacePath } from \"@stryke/path/replace\";\nimport { camelCase } from \"@stryke/string-format/camel-case\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport { isUndefined } from \"@stryke/type-checks/is-undefined\";\nimport { DeepPartial } from \"@stryke/types/base\";\nimport defu from \"defu\";\nimport type { BuildOptions, PluginBuild } from \"esbuild\";\nimport { Format } from \"esbuild\";\nimport { createEsbuildPlugin } from \"unplugin\";\nimport {\n  createUnpluginFactory,\n  UnpluginFactoryDecorator,\n  UnpluginFactoryOptions\n} from \"./unplugin\";\n\nexport const DEFAULT_OPTIONS: Partial<BuildOptions> = {\n  target: \"esnext\",\n  platform: \"neutral\",\n  format: \"esm\",\n  write: true,\n  minify: true,\n  sourcemap: false,\n  bundle: true,\n  treeShaking: true,\n  keepNames: true,\n  splitting: true,\n  logLevel: \"silent\"\n};\n\n/**\n * Resolves the entry options for esbuild.\n *\n * @param context - The build context.\n * @param entryPoints - The entry points to resolve.\n * @returns The resolved entry options.\n */\nexport function resolveEntry<TContext extends UnresolvedContext>(\n  context: TContext,\n  entryPoints: ResolvedEntryFileReference[] | string[] = []\n): BuildOptions[\"entryPoints\"] {\n  return entryPoints.reduce(\n    (ret, entry) => {\n      if (isString(entry)) {\n        ret[replaceExtension(replacePath(entry, context.config.root))] =\n          replacePath(entry, context.config.root);\n      } else {\n        ret[entry.output || resolveEntryOutput(context, entry)] = entry.file;\n      }\n\n      return ret;\n    },\n    {} as Record<string, string>\n  );\n}\n\n/**\n * Resolves the esbuild options.\n *\n * @param context - The build context.\n * @param override - Optional esbuild options to override the resolved options.\n * @returns The resolved esbuild options.\n */\nexport function resolveOptions<TContext extends UnresolvedContext>(\n  context: TContext,\n  override: DeepPartial<BuildOptions> = {}\n): BuildOptions {\n  if (context.config.inject && Object.keys(context.config.inject).length > 0) {\n    context.fs.writeSync(\n      joinPaths(\n        context.config.cwd,\n        context.config.root,\n        context.artifactsPath,\n        \"inject-shim.js\"\n      ),\n      Object.entries(context.config.inject)\n        .map(([key, value]) => {\n          if (value) {\n            if (Array.isArray(value)) {\n              if (camelCase(key) !== key) {\n                if (value.length === 1) {\n                  return `\nimport ${camelCase(key)} from \"${value[0]}\";\nexport { ${camelCase(key)} as \"${key}\" }`;\n                } else if (value.length > 1) {\n                  return `\nimport ${value[1] === \"*\" ? `* as ${camelCase(key)}` : `{ ${value[1]} as ${camelCase(key)} }`} from \"${value[0]}\";\nexport { ${camelCase(key)} as \"${key}\" }`;\n                }\n              } else if (value.length === 1) {\n                return `\nimport ${key} from \"${value[0]}\";\nexport { ${key} };`;\n              } else if (value.length > 1) {\n                return `\nimport ${value[1] === \"*\" ? `* as ${key}` : `{ ${value[1]} as ${key} }`} from \"${value[0]}\";\nexport { ${key} };`;\n              }\n            } else if (camelCase(key) !== key) {\n              return `\nimport ${camelCase(key)} from \"${value[0]}\";\nexport { ${camelCase(key)} as \"${key}\" }`;\n            } else {\n              return `\nimport ${key} from \"${value}\";\nexport { ${key} };`;\n            }\n          }\n\n          return \"\";\n        })\n        .join(\"\\n\")\n    );\n  }\n\n  return defu(\n    {\n      alias: context.alias,\n      inject:\n        context.config.inject && Object.keys(context.config.inject).length > 0\n          ? [\n              joinPaths(\n                context.config.cwd,\n                context.config.root,\n                context.artifactsPath,\n                \"inject-shim.js\"\n              )\n            ]\n          : undefined\n    },\n    override,\n    {\n      mainFields: context.config.resolve.mainFields,\n      conditions: context.config.resolve.conditions,\n      define: context.config.define,\n      resolveExtensions: context.config.resolve.extensions,\n      packages: context.config.resolve.skipNodeModulesBundle\n        ? \"external\"\n        : \"bundle\",\n      format: (Array.isArray(context.config.output.format)\n        ? context.config.output.format[0]\n        : context.config.output.format) as Format,\n      platform: context.config.platform,\n      outdir: context.config.output.path,\n      tsconfig: context.tsconfig.tsconfigFilePath,\n      minify: context.config.output.minify,\n      metafile: context.config.mode === \"development\",\n      sourcemap: context.config.output.sourceMap\n    },\n    DEFAULT_OPTIONS\n  ) as BuildOptions;\n}\n\n/**\n * Creates an ESBuild plugin factory that generates a plugin instance.\n *\n * @see https://esbuild.github.io/plugins/\n *\n * @example\n * ```ts\n * // esbuild.config.ts\n * import { createEsbuildFactory } from \"@powerlines/unplugin/esbuild\";\n *\n * const powerlinesPlugin = createEsbuildFactory({ 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 an ESBuild plugin instance when called. The generated plugin will invoke the Powerlines API hooks during the build process, allowing you to integrate Powerlines into your ESBuild build.\n */\nexport function createEsbuildFactory<TContext extends ExecutionContext>(\n  options: Omit<UnpluginFactoryOptions, \"variant\"> = {},\n  decorate: UnpluginFactoryDecorator<TContext> = options => options\n) {\n  return createUnpluginFactory({ ...options, variant: \"esbuild\" }, unplugin =>\n    decorate({\n      ...(unplugin as UnpluginOptions<TContext>),\n      esbuild: {\n        config: opts => {\n          opts ??= {};\n\n          const result = resolveOptions(unplugin.context);\n          for (const key in result) {\n            if (\n              isUndefined(opts[key as keyof BuildOptions]) &&\n              !isUndefined(result[key as keyof BuildOptions])\n            ) {\n              opts[key as keyof BuildOptions] = result[\n                key as keyof BuildOptions\n              ] as any;\n            }\n          }\n        },\n        setup: async (build: PluginBuild) => {\n          const environment = await unplugin.context.getEnvironment();\n\n          return unplugin.context.callHook(\n            \"esbuild:setup\",\n            { environment },\n            build\n          );\n        }\n      }\n    })\n  );\n}\n\n/**\n * An ESBuild plugin that will invoke the Powerlines API hooks during the build process.\n *\n * @see https://esbuild.github.io/plugins/\n *\n * @example\n * ```js\n * // esbuild.config.js\n * import powerlines from \"@powerlines/unplugin/esbuild\";\n *\n * export default {\n *  plugins: [powerlines({ name: \"example-app\", ... })],\n * };\n *\n * ```\n */\nconst plugin = createEsbuildPlugin(createEsbuildFactory());\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;AAyCA,MAAa,kBAAyC;CACpD,QAAQ;CACR,UAAU;CACV,QAAQ;CACR,OAAO;CACP,QAAQ;CACR,WAAW;CACX,QAAQ;CACR,aAAa;CACb,WAAW;CACX,WAAW;CACX,UAAU;AACZ;;;;;;;;AASA,SAAgB,aACd,SACA,cAAuD,CAAC,GAC3B;CAC7B,OAAO,YAAY,QAChB,KAAK,UAAU;EACd,IAAI,SAAS,KAAK,GAChB,IAAI,iBAAiB,YAAY,OAAO,QAAQ,OAAO,IAAI,CAAC,KAC1D,YAAY,OAAO,QAAQ,OAAO,IAAI;OAExC,IAAI,MAAM,UAAU,mBAAmB,SAAS,KAAK,KAAK,MAAM;EAGlE,OAAO;CACT,GACA,CAAC,CACH;AACF;;;;;;;;AASA,SAAgB,eACd,SACA,WAAsC,CAAC,GACzB;CACd,IAAI,QAAQ,OAAO,UAAU,OAAO,KAAK,QAAQ,OAAO,MAAM,CAAC,CAAC,SAAS,GACvE,QAAQ,GAAG,UACT,UACE,QAAQ,OAAO,KACf,QAAQ,OAAO,M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