{"version":3,"file":"tsdown.mjs","names":["_getDependencyConfig","rolldown"],"sources":["../src/tsdown.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  CopyConfig,\n  ResolveConfig,\n  UnresolvedContext\n} from \"@powerlines/core\";\nimport type { GetDependencyConfigResult } from \"@powerlines/core/plugin-utils\";\nimport { getDependencyConfig as _getDependencyConfig } from \"@powerlines/core/plugin-utils\";\nimport type { Format } from \"@storm-software/build-tools/types\";\nimport { toArray } from \"@stryke/convert/to-array\";\nimport { appendPath } from \"@stryke/path/append\";\nimport { relativePath } from \"@stryke/path/file-path-fns\";\nimport { globToRegex } from \"@stryke/path/glob-to-regex\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport { replaceExtension, replacePath } from \"@stryke/path/replace\";\nimport { isFunction } from \"@stryke/type-checks/is-function\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\nimport defu from \"defu\";\nimport { ModuleFormat } from \"rolldown\";\nimport { UserConfig as BuildOptions, Format as TsdownFormat } from \"tsdown\";\nimport type { UserConfig } from \"tsdown/config\";\nimport rolldown from \"./rolldown\";\nimport { UnpluginExecutionOptions } from \"./types\";\n\n/**\n * Get the {@link ResolveConfig.external | external} and {@link ResolveConfig.noExternal | noExternal} dependencies for the build configuration.\n *\n * @param context - The build context.\n * @returns The dependency configuration.\n */\nexport function getDependencyConfig<TContext extends UnresolvedContext>(\n  context: TContext\n): GetDependencyConfigResult {\n  const { external, noExternal } = _getDependencyConfig(context);\n\n  return {\n    external:\n      !external || external.length === 0\n        ? undefined\n        : external.map(ext =>\n            isSetString(ext) && ext.includes(\"*\") ? globToRegex(ext) : ext\n          ),\n    noExternal:\n      !noExternal || noExternal.length === 0\n        ? undefined\n        : noExternal.map(noExt =>\n            isSetString(noExt) && noExt.includes(\"*\")\n              ? globToRegex(noExt)\n              : noExt\n          )\n  };\n}\n\nexport const DEFAULT_OPTIONS: Partial<BuildOptions> = {\n  platform: \"neutral\",\n  target: \"esnext\",\n  fixedExtension: true,\n  clean: false\n} as const;\n\n/**\n * Resolves the entry options for [tsdown](https://github.com/rolldown/tsdown).\n *\n * @param formats - The formats to resolve.\n * @returns The resolved entry options.\n */\nexport function resolveFormat(formats?: Format | Format[]): TsdownFormat[] {\n  return toArray(formats).map(format => {\n    switch (format) {\n      case \"cjs\":\n        return \"cjs\";\n      case \"iife\":\n        return \"iife\";\n      case \"esm\":\n      default:\n        return \"esm\";\n    }\n  });\n}\n\n/**\n * Resolves the entry options for [tsdown](https://github.com/rolldown/tsdown).\n *\n * @param formats - The formats to resolve.\n * @returns The resolved entry options.\n */\nexport function resolveFromFormat(\n  formats?: TsdownFormat | TsdownFormat[]\n): Format[] {\n  return toArray(formats).map(format => {\n    switch (format) {\n      case \"cjs\":\n      case \"commonjs\":\n        return \"cjs\";\n      case \"iife\":\n        return \"iife\";\n      case \"esm\":\n      case \"es\":\n      case \"module\":\n      case \"umd\":\n      default:\n        return \"esm\";\n    }\n  });\n}\n\nconst formatMessage = <TContext extends UnresolvedContext>(\n  context: TContext,\n  ...msgs: any[]\n) =>\n  msgs\n    .filter(Boolean)\n    .join(\" \")\n    .trim()\n    .replace(new RegExp(`\\\\[${context.config.name}\\\\]`, \"g\"), \"\")\n    .replaceAll(/^\\s+/g, \"\")\n    .replaceAll(/\\s+$/g, \"\")\n    .trim();\n\n/**\n * Resolves the options for [tsdown](https://github.com/rolldown/tsdown).\n *\n * @param context - The build context.\n * @returns The resolved options.\n */\nexport function resolveOptions<TContext extends UnresolvedContext>(\n  context: TContext\n): BuildOptions {\n  const { external, noExternal } = getDependencyConfig(context);\n\n  return defu(\n    {\n      name: context.config.name,\n      cwd: appendPath(context.config.root, context.config.cwd),\n      entry:\n        context.entry.filter(entry => entry?.file).length > 0\n          ? Object.fromEntries(\n              context.entry\n                .filter(entry => entry?.file)\n                .map(entry => [\n                  entry.output ||\n                    replaceExtension(\n                      replacePath(\n                        replacePath(\n                          entry.file,\n                          joinPaths(context.config.root, \"src\")\n                        ),\n                        context.entryPath\n                      )\n                    ),\n                  entry.file\n                ])\n            )\n          : [\n              joinPaths(\n                context.config.cwd,\n                context.config.root,\n                \"src\",\n                \"**/*.ts\"\n              ),\n              joinPaths(\n                context.config.cwd,\n                context.config.root,\n                \"src\",\n                \"**/*.tsx\"\n              )\n            ],\n      exports: {\n        customExports: (exports: Record<string, any>) => {\n          const result = Object.fromEntries(\n            Object.entries(exports).map(([key, value]) => {\n              if (isSetString(value)) {\n                return [key, value];\n              }\n\n              const currentExport = {} as Record<string, any>;\n              if (isSetString(value.require)) {\n                currentExport.require = {\n                  types: replaceExtension(value.require, \".d.cts\", {\n                    fullExtension: true\n                  }),\n                  default: value.require\n                };\n              }\n\n              if (isSetString(value.import)) {\n                currentExport.import = {\n                  types: replaceExtension(value.import, \".d.mts\", {\n                    fullExtension: true\n                  }),\n                  default: value.import\n                };\n              }\n\n              if (!isSetObject(value.default)) {\n                if (isSetObject(currentExport.import)) {\n                  currentExport.default = currentExport.import;\n                } else if (isSetObject(currentExport.require)) {\n                  currentExport.default = currentExport.require;\n                }\n              }\n\n              return [key, currentExport];\n            })\n          );\n\n          return Object.keys(result)\n            .sort()\n            .reduce(\n              (ret, key) => {\n                ret[key] = result[key];\n                return ret;\n              },\n              {} as Record<string, any>\n            );\n        }\n      },\n      define: context.config.define,\n      inputOptions: {\n        transform: {\n          define: context.config.define,\n          inject: context.config.inject,\n          typescript: {\n            target: context.tsconfig.tsconfigJson?.compilerOptions?.target\n          }\n        }\n      },\n      deps: {\n        neverBundle: external,\n        alwaysBundle: context.config.resolve.skipNodeModulesBundle\n          ? undefined\n          : noExternal,\n        onlyBundle: context.config.resolve.skipNodeModulesBundle\n          ? noExternal\n          : undefined,\n        skipNodeModulesBundle: context.config.resolve.skipNodeModulesBundle\n      },\n      alias: context.alias,\n      resolve: {\n        alias: context.alias\n      },\n      platform: context.config.platform,\n      dts: context.config.output.dts,\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      format: resolveFormat(context.config.output.format).filter(Boolean),\n      mode: context.config.mode,\n      minify: context.config.output.minify,\n      metafile: context.config.mode === \"development\",\n      sourcemap: context.config.output.sourceMap,\n      nodeProtocol: context.config.output.nodeProtocol,\n      debug: context.config.mode === \"development\",\n      silent:\n        context.config.logLevel.general === \"silent\" ||\n        context.config.mode === \"production\",\n      logLevel: context.config.logLevel.general === \"trace\" ? \"debug\" : \"error\",\n      customLogger: {\n        level: context.config.logLevel.general === \"trace\" ? \"debug\" : \"error\",\n        info: (...msgs: any[]) =>\n          isSetString(formatMessage(context, ...msgs).replace(/\\s+/g, \"\")) &&\n          context.trace(formatMessage(context, ...msgs)),\n        warn: (...msgs: any[]) =>\n          isSetString(formatMessage(context, ...msgs).replace(/\\s+/g, \"\")) &&\n          context.warn(formatMessage(context, ...msgs)),\n        warnOnce: (...msgs: any[]) =>\n          isSetString(formatMessage(context, ...msgs).replace(/\\s+/g, \"\")) &&\n          context.warn(formatMessage(context, ...msgs)),\n        error: (...msgs: any[]) =>\n          isSetString(formatMessage(context, ...msgs).replace(/\\s+/g, \"\")) &&\n          context.error(formatMessage(context, ...msgs)),\n        success: (...msgs: any[]) =>\n          isSetString(formatMessage(context, ...msgs).replace(/\\s+/g, \"\")) &&\n          context.trace(formatMessage(context, ...msgs))\n      }\n    },\n    DEFAULT_OPTIONS\n  ) as BuildOptions;\n}\n\n/**\n * A Tsdown configuration function that integrates Powerlines into the build process.\n *\n * @see https://github.com/rolldown/tsdown\n *\n * @example\n * ```ts\n * // tsdown.config.ts\n * import withPowerlines from \"@powerlines/unplugin/tsdown\";\n *\n * export default withPowerlines({\n *  entry: [\"src/index.ts\"],\n *  format: [\"cjs\", \"esm\"],\n *  dts: true,\n *  sourcemap: true,\n *  clean: true,\n * });\n *\n * ```\n *\n * @param options - The Tsdown options to merge with the Powerlines configuration.\n * @returns The merged Tsdown configuration options.\n */\nexport function plugin(options: UserConfig = {}): UserConfig {\n  return {\n    ...options,\n    entry: options.entry,\n    plugins: [\n      rolldown({\n        ...options,\n        output: {\n          path: options.outDir,\n          format: resolveFromFormat(\n            options.format as ModuleFormat | ModuleFormat[]\n          ),\n          copy: {\n            path: options.outDir!,\n            assets: toArray(options.copy)\n              .map(copy => {\n                if (!copy) {\n                  return undefined;\n                }\n                if (isSetString(copy)) {\n                  return copy;\n                }\n                if (isFunction(copy)) {\n                  // eslint-disable-next-line no-console\n                  console.warn(\n                    \"Function-based copy options are not supported in Powerlines.\"\n                  );\n                  return undefined;\n                }\n                return {\n                  input: copy.from,\n                  output: copy.to\n                };\n              })\n              .filter(Boolean) as CopyConfig[\"assets\"]\n          }\n        },\n        resolve: {\n          external: options.deps?.neverBundle\n            ? (toArray(options.deps?.neverBundle)\n                .map(external => {\n                  if (isFunction(external)) {\n                    // eslint-disable-next-line no-console\n                    console.warn(\n                      \"Function-based external options are not supported in Powerlines.\"\n                    );\n                    return undefined;\n                  }\n                  return external;\n                })\n                .filter(Boolean) as ResolveConfig[\"external\"])\n            : undefined,\n          noExternal:\n            (options.deps?.skipNodeModulesBundle && options.deps?.onlyBundle) ||\n            (!options.deps?.skipNodeModulesBundle && options.deps?.alwaysBundle)\n              ? (toArray(\n                  options.deps?.skipNodeModulesBundle\n                    ? options.deps?.onlyBundle\n                    : options.deps?.alwaysBundle\n                )\n                  .map(noExternal => {\n                    if (isFunction(noExternal)) {\n                      // eslint-disable-next-line no-console\n                      console.warn(\n                        \"Function-based noExternal options are not supported in Powerlines.\"\n                      );\n                      return undefined;\n                    }\n                    return noExternal;\n                  })\n                  .filter(Boolean) as ResolveConfig[\"noExternal\"])\n              : undefined\n        },\n        tsconfig: isSetString(options.tsconfig) ? options.tsconfig : undefined\n      } as UnpluginExecutionOptions)\n    ]\n  };\n}\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,oBACd,SAC2B;CAC3B,MAAM,EAAE,UAAU,eAAeA,sBAAqB,OAAO;CAE7D,OAAO;EACL,UACE,CAAC,YAAY,SAAS,WAAW,IAC7B,SACA,SAAS,KAAI,QACX,YAAY,GAAG,KAAK,IAAI,SAAS,GAAG,IAAI,YAAY,GAAG,IAAI,GAC7D;EACN,YACE,CAAC,cAAc,WAAW,WAAW,IACjC,SACA,WAAW,KAAI,UACb,YAAY,KAAK,KAAK,MAAM,SAAS,GAAG,IACpC,YAAY,KAAK,IACjB,KACN;CACR;AACF;AAEA,MAAa,kBAAyC;CACpD,UAAU;CACV,QAAQ;CACR,gBAAgB;CAChB,OAAO;AACT;;;;;;;AAQA,SAAgB,cAAc,SAA6C;CACzE,OAAO,QAAQ,OAAO,CAAC,CAAC,KAAI,WAAU;EACpC,QAAQ,QAAR;GACE,KAAK,OACH,OAAO;GACT,KAAK,QACH,OAAO;GAET,SACE,OAAO;EACX;CACF,CAAC;AACH;;;;;;;AAQA,SAAgB,kBACd,SACU;CACV,OAAO,QAAQ,OAAO,CAAC,CAAC,KAAI,WAAU;EACpC,QAAQ,QAAR;GACE,KAAK;GACL,KAAK,YACH,OAAO;GACT,KAAK,QACH,OAAO;GAKT,SACE,OAAO;EACX;CACF,CAAC;AACH;AAEA,MAAM,iBACJ,SACA,GAAG,SAEH,KACG,OAAO,OAAO,CAAC,CACf,KAAK,GAAG,CAAC,CACT,KAAK,CAAC,CACN,QAAQ,IAAI,OAAO,MAAM,QAA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