{"version":3,"file":"resolve-options.mjs","names":["resolveRollupOptions"],"sources":["../../src/helpers/resolve-options.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 { getString } from \"@powerlines/core/lib/utilities/source-file\";\nimport { resolveOptions as resolveRollupOptions } from \"@powerlines/unplugin/rollup\";\nimport type {\n  UnbuildOptions as ExternalUnbuildOptions,\n  Loader,\n  LoaderResult\n} from \"@storm-software/unbuild/types\";\nimport { appendPath, relativePath } from \"@stryke/path\";\nimport { joinPaths } from \"@stryke/path/join-paths\";\nimport defu from \"defu\";\nimport { transform } from \"esbuild\";\nimport type { Context, PluginContext } from \"powerlines\";\nimport { TransformResult } from \"unplugin\";\nimport { UnbuildPluginResolvedConfig } from \"../types\";\n\nexport const DEFAULT_UNBUILD_CONFIG = {\n  dts: true,\n  clean: false,\n  includeSrc: false,\n  treeShaking: true,\n  splitting: true,\n  stub: false,\n  watchOptions: {},\n  outputPath: \"dist\",\n  generatePackageJson: true,\n  banner: \" \",\n  rollup: {\n    dts: {},\n    emitCJS: true,\n    replace: {},\n    resolve: {},\n    json: {},\n    esbuild: { target: \"es2020\" },\n    commonjs: {},\n    alias: {}\n  }\n} as Partial<ExternalUnbuildOptions>;\n\nexport const unbuildLoader = (context: PluginContext): Loader => {\n  return async (input, { options }) => {\n    if (\n      !/\\.(?:c|m)?[jt]sx?$/.test(input.path) ||\n      /\\.d\\.[cm]?ts$/.test(input.path)\n    ) {\n      return;\n    }\n\n    const output: LoaderResult = [];\n\n    let contents = await input.getContents();\n\n    // declaration\n    if (options.declaration && !input.srcPath?.match(/\\.d\\.[cm]?ts$/)) {\n      const cm = input.srcPath?.match(/(?<=\\.)(?:c|m)(?=[jt]s$)/)?.[0] || \"\";\n      const extension = `.d.${cm}ts`;\n      output.push({\n        contents,\n        srcPath: input.srcPath,\n        path: input.path,\n        extension,\n        declaration: true\n      });\n    }\n\n    let transformed: TransformResult | string = contents;\n\n    let result = await context.callHook(\n      \"transform\",\n      {\n        sequential: true,\n        order: \"pre\"\n      },\n      transformed,\n      input.path\n    );\n    if (result) {\n      transformed = result;\n    }\n\n    result = await context.callHook(\n      \"transform\",\n      {\n        sequential: true,\n        order: \"normal\"\n      },\n      getString(transformed),\n      input.path\n    );\n    if (result) {\n      transformed = result;\n    }\n\n    result = await context.callHook(\n      \"transform\",\n      {\n        sequential: true,\n        order: \"post\"\n      },\n      getString(transformed),\n      input.path\n    );\n    if (result) {\n      transformed = result;\n    }\n\n    // typescript => js\n    if ([\".ts\", \".mts\", \".cts\"].includes(input.extension)) {\n      contents = await transform(getString(transformed), {\n        ...Object.fromEntries(\n          Object.entries(options.esbuild ?? {}).filter(\n            ([key]) => key !== \"absPaths\"\n          )\n        ),\n        loader: \"ts\"\n      }).then(r => r.code);\n    } else if ([\".tsx\", \".jsx\"].includes(input.extension)) {\n      contents = await transform(getString(transformed), {\n        loader: input.extension === \".tsx\" ? \"tsx\" : \"jsx\",\n        ...Object.fromEntries(\n          Object.entries(options.esbuild ?? {}).filter(\n            ([key]) => key !== \"absPaths\"\n          )\n        )\n      }).then(r => r.code);\n    }\n\n    // esm => cjs\n    const isCjs = options.format === \"cjs\";\n    if (isCjs) {\n      contents = context.resolver\n        .transform({\n          source: contents,\n          retainLines: false\n        })\n        .replace(/^exports.default = /gm, \"module.exports = \")\n        .replace(/^var _default = exports.default = /gm, \"module.exports = \")\n        .replace(\"module.exports = void 0;\", \"\");\n    }\n\n    let extension = isCjs ? \".js\" : \".mjs\"; // TODO: Default to .cjs in next major version\n    if (options.ext) {\n      extension = options.ext.startsWith(\".\") ? options.ext : `.${options.ext}`;\n    }\n\n    output.push({\n      contents,\n      path: input.path,\n      extension\n    });\n\n    return output;\n  };\n};\n\n/**\n * Extracts the unbuild configuration from the context.\n *\n * @param context - The build context.\n * @returns The resolved unbuild configuration.\n */\nexport function resolveOptions(context: Context): ExternalUnbuildOptions {\n  return defu(\n    {\n      alias: context.alias\n    },\n    (context.config as UnbuildPluginResolvedConfig).unbuild\n      ? (context.config as UnbuildPluginResolvedConfig).unbuild\n      : {},\n    {\n      projectName: context.config.name,\n      name: context.config.name,\n      orgName: context.config.organization,\n      sourceRoot: joinPaths(context.config.root, \"src\"),\n      projectRoot: context.config.root,\n      outputPath: relativePath(\n        appendPath(context.config.root, context.config.cwd),\n        context.config.output.path\n      ),\n      platform: context.config.platform,\n      external: context.builtins.reduce((ret, id) => {\n        if (!ret.includes(id)) {\n          ret.push(id);\n        }\n\n        return ret;\n      }, context.config.resolve.external ?? []),\n      loaders: [unbuildLoader(context as PluginContext)],\n      jiti: {\n        interopDefault: true,\n        fsCache: joinPaths(context.envPaths.cache, \"jiti\"),\n        moduleCache: true\n      },\n      rollup: resolveRollupOptions(context) as any,\n      debug: context.config.mode === \"development\",\n      minify: context.config.output.minify,\n      sourcemap: context.config.output.sourceMap\n    },\n    DEFAULT_UNBUILD_CONFIG\n  ) as ExternalUnbuildOptions;\n}\n"],"mappings":";;;;;;;;AAiCA,MAAa,yBAAyB;CACpC,KAAK;CACL,OAAO;CACP,YAAY;CACZ,aAAa;CACb,WAAW;CACX,MAAM;CACN,cAAc,CAAC;CACf,YAAY;CACZ,qBAAqB;CACrB,QAAQ;CACR,QAAQ;EACN,KAAK,CAAC;EACN,SAAS;EACT,SAAS,CAAC;EACV,SAAS,CAAC;EACV,MAAM,CAAC;EACP,SAAS,EAAE,QAAQ,SAAS;EAC5B,UAAU,CAAC;EACX,OAAO,CAAC;CACV;AACF;AAEA,MAAa,iBAAiB,YAAmC;CAC/D,O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