context-DY6ggBKV.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { parse } from "./context-D49cMElb.js";
  2. import { resolve } from "node:path";
  3. import { Buffer } from "node:buffer";
  4. //#region src/rspack/context.ts
  5. function createBuildContext(compiler, compilation, loaderContext) {
  6. return {
  7. getNativeBuildContext() {
  8. return {
  9. framework: "rspack",
  10. compiler,
  11. compilation,
  12. loaderContext
  13. };
  14. },
  15. addWatchFile(file) {
  16. const cwd = process.cwd();
  17. compilation.fileDependencies.add(resolve(cwd, file));
  18. },
  19. getWatchFiles() {
  20. return Array.from(compilation.fileDependencies);
  21. },
  22. parse,
  23. emitFile(emittedFile) {
  24. const outFileName = emittedFile.fileName || emittedFile.name;
  25. if (emittedFile.source && outFileName) {
  26. const { sources } = compilation.compiler.webpack;
  27. compilation.emitAsset(outFileName, new sources.RawSource(typeof emittedFile.source === "string" ? emittedFile.source : Buffer.from(emittedFile.source)));
  28. }
  29. }
  30. };
  31. }
  32. function createContext(loader) {
  33. return {
  34. error: (error) => loader.emitError(normalizeMessage(error)),
  35. warn: (message) => loader.emitWarning(normalizeMessage(message))
  36. };
  37. }
  38. function normalizeMessage(error) {
  39. const err = new Error(typeof error === "string" ? error : error.message);
  40. if (typeof error === "object") {
  41. err.stack = error.stack;
  42. err.cause = error.meta;
  43. }
  44. return err;
  45. }
  46. //#endregion
  47. export { createBuildContext as createBuildContext$1, createContext as createContext$1, normalizeMessage as normalizeMessage$1 };