rolldown-jmAeXo_f.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { n as __toESM, t as require_binding } from "./binding-B4m_2rFW.mjs";
  2. import { f as __decorate, h as PlainObjectLike, m as lazyProp, u as transformToRollupOutput } from "./bindingify-input-options-DCTviPFW.mjs";
  3. import { l as PluginDriver, s as validateOption, t as createBundlerOptions } from "./create-bundler-option-DLv8UpsD.mjs";
  4. import { i as unwrapBindingResult } from "./error-Bj1xBdEY.mjs";
  5. //#region src/types/rolldown-output-impl.ts
  6. var import_binding = /* @__PURE__ */ __toESM(require_binding(), 1);
  7. var RolldownOutputImpl = class extends PlainObjectLike {
  8. bindingOutputs;
  9. constructor(bindingOutputs) {
  10. super();
  11. this.bindingOutputs = bindingOutputs;
  12. if (bindingOutputs.mangleCache !== void 0) this.mangleCache = bindingOutputs.mangleCache;
  13. }
  14. get output() {
  15. return transformToRollupOutput(this.bindingOutputs).output;
  16. }
  17. __rolldown_external_memory_handle__(keepDataAlive) {
  18. const results = this.output.map((item) => item.__rolldown_external_memory_handle__(keepDataAlive));
  19. if (!results.every((r) => r.freed)) {
  20. const reasons = results.filter((r) => !r.freed).map((r) => r.reason).filter(Boolean);
  21. return {
  22. freed: false,
  23. reason: `Failed to free ${reasons.length} item(s): ${reasons.join("; ")}`
  24. };
  25. }
  26. return { freed: true };
  27. }
  28. };
  29. __decorate([lazyProp], RolldownOutputImpl.prototype, "output", null);
  30. //#endregion
  31. //#region src/api/rolldown/rolldown-build.ts
  32. Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose");
  33. /**
  34. * The bundle object returned by {@linkcode rolldown} function.
  35. *
  36. * @category Programmatic APIs
  37. */
  38. var RolldownBuild = class {
  39. #inputOptions;
  40. #bundler;
  41. #stopWorkers;
  42. #asyncRuntimeReleased = false;
  43. /** @hidden should not be used directly */
  44. constructor(inputOptions) {
  45. this.#inputOptions = inputOptions;
  46. this.#bundler = new import_binding.BindingBundler();
  47. (0, import_binding.startAsyncRuntime)();
  48. }
  49. /**
  50. * Whether the bundle has been closed.
  51. *
  52. * If the bundle is closed, calling other methods will throw an error.
  53. */
  54. get closed() {
  55. return this.#bundler.closed;
  56. }
  57. /**
  58. * Generate bundles in-memory.
  59. *
  60. * If you directly want to write bundles to disk, use the {@linkcode write} method instead.
  61. *
  62. * @param outputOptions The output options.
  63. * @returns The generated bundle.
  64. * @throws {@linkcode BundleError} When an error occurs during the build.
  65. */
  66. async generate(outputOptions = {}) {
  67. return this.#build(false, outputOptions);
  68. }
  69. /**
  70. * Generate and write bundles to disk.
  71. *
  72. * If you want to generate bundles in-memory, use the {@linkcode generate} method instead.
  73. *
  74. * @param outputOptions The output options.
  75. * @returns The generated bundle.
  76. * @throws {@linkcode BundleError} When an error occurs during the build.
  77. */
  78. async write(outputOptions = {}) {
  79. return this.#build(true, outputOptions);
  80. }
  81. /**
  82. * Close the bundle and free resources.
  83. *
  84. * This method should be called even if the {@linkcode generate} method
  85. * or the {@linkcode write} method threw an error. It should be called
  86. * even if neither of the methods are called.
  87. *
  88. * This method is called automatically when using `using` syntax.
  89. *
  90. * @example
  91. * ```js
  92. * import { rolldown } from 'rolldown';
  93. *
  94. * {
  95. * using bundle = await rolldown({ input: 'src/main.js' });
  96. * const output = await bundle.generate({ format: 'esm' });
  97. * console.log(output);
  98. * // bundle.close() is called automatically here
  99. * }
  100. * ```
  101. */
  102. async close() {
  103. const shouldRelease = !this.#asyncRuntimeReleased;
  104. this.#asyncRuntimeReleased = true;
  105. try {
  106. await this.#stopWorkers?.();
  107. await this.#bundler.close();
  108. this.#stopWorkers = void 0;
  109. } finally {
  110. if (shouldRelease) (0, import_binding.shutdownAsyncRuntime)();
  111. }
  112. }
  113. /** @hidden documented in close method */
  114. async [Symbol.asyncDispose]() {
  115. await this.close();
  116. }
  117. /**
  118. * @experimental
  119. * @hidden not ready for public usage yet
  120. */
  121. get watchFiles() {
  122. return Promise.resolve(this.#bundler.getWatchFiles());
  123. }
  124. async #build(isWrite, outputOptions) {
  125. validateOption("output", outputOptions);
  126. await this.#stopWorkers?.();
  127. const option = await createBundlerOptions(this.#inputOptions, outputOptions, false, true);
  128. try {
  129. this.#stopWorkers = option.stopWorkers;
  130. let output;
  131. if (isWrite) output = await this.#bundler.write(option.bundlerOptions);
  132. else output = await this.#bundler.generate(option.bundlerOptions);
  133. return new RolldownOutputImpl(unwrapBindingResult(output));
  134. } catch (e) {
  135. await option.stopWorkers?.();
  136. throw e;
  137. }
  138. }
  139. };
  140. //#endregion
  141. //#region src/api/rolldown/index.ts
  142. /**
  143. * The API compatible with Rollup's `rollup` function.
  144. *
  145. * Unlike Rollup, the module graph is not built until the methods of the bundle object are called.
  146. *
  147. * @param input The input options object.
  148. * @returns A Promise that resolves to a bundle object.
  149. *
  150. * @example
  151. * ```js
  152. * import { rolldown } from 'rolldown';
  153. *
  154. * let bundle, failed = false;
  155. * try {
  156. * bundle = await rolldown({
  157. * input: 'src/main.js',
  158. * });
  159. * await bundle.write({
  160. * format: 'esm',
  161. * });
  162. * } catch (e) {
  163. * console.error(e);
  164. * failed = true;
  165. * }
  166. * if (bundle) {
  167. * await bundle.close();
  168. * }
  169. * process.exitCode = failed ? 1 : 0;
  170. * ```
  171. *
  172. * @category Programmatic APIs
  173. */
  174. const rolldown = async (input) => {
  175. validateOption("input", input);
  176. return new RolldownBuild(await PluginDriver.callOptionsHook(input));
  177. };
  178. //#endregion
  179. export { rolldown as t };