{"version":3,"file":"rollup.mjs","names":["defu"],"sources":["../src/rollup.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  UnpluginOptions,\n  UnresolvedContext\n} from \"@powerlines/core\";\nimport alias from \"@rollup/plugin-alias\";\nimport inject from \"@rollup/plugin-inject\";\nimport resolve from \"@rollup/plugin-node-resolve\";\nimport replace from \"@rollup/plugin-replace\";\nimport { toArray } from \"@stryke/convert/to-array\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport { defu } from \"defu\";\nimport { globSync } from \"node:fs\";\nimport type { InputOptions, RollupOptions } from \"rollup\";\nimport { Plugin } from \"rollup\";\nimport typescriptPlugin from \"rollup-plugin-typescript2\";\nimport { createRollupPlugin } from \"unplugin\";\nimport {\n  createUnpluginFactory,\n  UnpluginFactoryDecorator,\n  UnpluginFactoryOptions\n} from \"./unplugin\";\n\n/**\n * A Rollup plugin to bundle TypeScript declaration files (.d.ts) alongside the JavaScript output files.\n *\n * @remarks\n * This plugin generates .d.ts files for each entry point in the bundle, ensuring that type definitions are available for consumers of the library.\n */\nexport const dtsBundlePlugin: Plugin = {\n  name: \"powerlines:dts-bundle\",\n  async generateBundle(_opts, bundle) {\n    for (const [, file] of Object.entries(bundle)) {\n      if (\n        file.type === \"asset\" ||\n        !file.isEntry ||\n        file.facadeModuleId == null\n      ) {\n        continue;\n      }\n\n      // Replace various JavaScript file extensions (e.g., .js, .cjs, .mjs, .cjs.js, .mjs.js) with .d.ts for generating type definition file names.\n      const dtsFileName = file.fileName.replace(\n        /(?:\\.cjs|\\.mjs|\\.esm\\.js|\\.cjs\\.js|\\.mjs\\.js|\\.js)$/,\n        \".d.ts\"\n      );\n\n      const relativeSourceDtsName = JSON.stringify(\n        `./${file.facadeModuleId.replace(/\\.[cm]?[jt]sx?$/, \"\")}`\n      );\n\n      this.emitFile({\n        type: \"asset\",\n        fileName: dtsFileName,\n        source: file.exports.includes(\"default\")\n          ? `export * from ${relativeSourceDtsName};\\nexport { default } from ${relativeSourceDtsName};\\n`\n          : `export * from ${relativeSourceDtsName};\\n`\n      });\n    }\n  }\n};\n\n/**\n * Resolves the options for [rollup](https://rollupjs.org).\n *\n * @param context - The build context.\n * @returns The resolved options.\n */\nexport function resolveOptions<TContext extends UnresolvedContext>(\n  context: TContext\n): RollupOptions {\n  return {\n    input: globSync(\n      toArray(context.entry).map(entry =>\n        isString(entry) ? entry : entry.file\n      )\n    ).flat(),\n    external: (source: string) => {\n      if (\n        context.config.resolve.external &&\n        toArray(context.config.resolve.external).includes(source)\n      ) {\n        return true;\n      }\n\n      if (\n        context.config.resolve.noExternal &&\n        toArray(context.config.resolve.noExternal).includes(source)\n      ) {\n        return false;\n      }\n\n      if (context.builtins.includes(source)) {\n        return context.config.projectType !== \"application\";\n      }\n\n      return !context.config.resolve.skipNodeModulesBundle;\n    },\n    plugins: [\n      typescriptPlugin({\n        check: false,\n        tsconfig: context.tsconfig.tsconfigFilePath\n      }),\n      context.config.define &&\n        Object.keys(context.config.define).length > 0 &&\n        replace({\n          sourceMap: context.config.mode === \"development\",\n          preventAssignment: true,\n          ...(context.config.define ?? {})\n        }),\n      context.config.inject &&\n        Object.keys(context.config.inject).length > 0 &&\n        inject({\n          sourceMap: context.config.mode === \"development\",\n          ...context.config.inject\n        }),\n      alias({\n        entries: 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      }),\n      resolve({\n        moduleDirectories: [\"node_modules\"],\n        preferBuiltins: true\n      }),\n      dtsBundlePlugin\n    ].filter(Boolean) as Plugin[],\n    cache: !context.config.skipCache ? undefined : false,\n    logLevel:\n      context.config.logLevel.general === \"trace\"\n        ? \"debug\"\n        : context.config.logLevel.general === \"debug\"\n          ? \"warn\"\n          : \"silent\",\n    output: [\n      {\n        dir: context.config.output.path,\n        format: \"es\",\n        entryFileNames: \"[name].js\",\n        preserveModules: true,\n        sourcemap: context.config.output.sourceMap\n      },\n      {\n        dir: context.config.output.path,\n        format: \"cjs\",\n        entryFileNames: \"[name].cjs\",\n        preserveModules: true,\n        sourcemap: context.config.output.sourceMap\n      }\n    ]\n  };\n}\n\n/**\n * Creates a Rollup plugin factory that generates a plugin instance.\n *\n * @see https://rollupjs.org/guide/en/#plugins-overview\n *\n * @example\n * ```ts\n * // rollup.config.ts\n * import { createRollupFactory } from \"@powerlines/unplugin/rollup\";\n *\n * const powerlinesPlugin = createRollupFactory({ 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 Rollup plugin instance when called. The generated plugin will invoke the Powerlines API hooks during the build process, allowing you to integrate Powerlines into your Rollup build.\n */\nexport function createRollupFactory<TContext extends ExecutionContext>(\n  options: Omit<UnpluginFactoryOptions, \"variant\"> = {},\n  decorate: UnpluginFactoryDecorator<TContext> = options => options\n) {\n  return createUnpluginFactory({ ...options, variant: \"rollup\" }, unplugin =>\n    decorate({\n      ...(unplugin as UnpluginOptions<TContext>),\n      rollup: {\n        async options(opts: InputOptions) {\n          const environment = await unplugin.context.getEnvironment();\n\n          return defu(\n            resolveOptions(environment),\n            opts,\n            unplugin.context.callHook(\n              \"rollup:options\",\n              { environment },\n              opts\n            ) ?? {}\n          );\n        }\n      }\n    })\n  );\n}\n\n/**\n * A Rollup plugin that will invoke the Powerlines API hooks during the build process.\n *\n * @see https://rollupjs.org/guide/en/#plugins-overview\n *\n * @example\n * ```ts\n * // rollup.config.ts\n *\n * import powerlines from \"@powerlines/unplugin/rollup\";\n *\n * export default defineConfig({\n *   plugins: [powerlines({ name: \"example-app\", ... })],\n * })\n * ```\n */\nexport const plugin = createRollupPlugin(createRollupFactory());\n\nexport { plugin as default };\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA+CA,MAAa,kBAA0B;CACrC,MAAM;CACN,MAAM,eAAe,OAAO,QAAQ;EAClC,KAAK,MAAM,GAAG,SAAS,OAAO,QAAQ,MAAM,GAAG;GAC7C,IACE,KAAK,SAAS,WACd,CAAC,KAAK,WACN,KAAK,kBAAkB,MAEvB;GAIF,MAAM,cAAc,KAAK,SAAS,QAChC,uDACA,OACF;GAEA,MAAM,wBAAwB,KAAK,UACjC,KAAK,KAAK,eAAe,QAAQ,mBAAmB,EAAE,GACxD;GAEA,KAAK,SAAS;IACZ,MAAM;IACN,UAAU;IACV,QAAQ,KAAK,QAAQ,SAAS,SAAS,IACnC,iBAAiB,sBAAsB,6BAA6B,sBAAsB,OAC1F,iBAAiB,sBAAsB;GAC7C,CAAC;EACH;CACF;AACF;;;;;;;AAQA,SAAgB,eACd,SACe;CACf,OAAO;EACL,OAAO,SACL,QAAQ,QAAQ,KAAK,CAAC,CAAC,KAAI,UACzB,SAAS,KAAK,IAAI,QAAQ,MAAM,IAClC,CACF,CAAC,CAAC,KAAK;EACP,WAAW,WAAmB;GAC5B,IACE,QAAQ,OAAO,QAAQ,YACvB,QAAQ,QAAQ,OAAO,QAAQ,QAAQ,CAAC,CAAC,SAAS,MAAM,GAExD,OAAO;GAGT,IACE,QA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