bdf7fbf2f419f1e9d71196469417dec0bedd61b61c090935aecee8a7228dcdc1a12b079e4adb27d34eaf241a2bb0d0114c0dd670308104a96dc2271bd9816b 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. // src/rspack/loaders/load.ts
  20. var load_exports = {};
  21. __export(load_exports, {
  22. default: () => load
  23. });
  24. module.exports = __toCommonJS(load_exports);
  25. // src/utils.ts
  26. var import_path = require("path");
  27. function normalizeAbsolutePath(path) {
  28. if ((0, import_path.isAbsolute)(path))
  29. return (0, import_path.normalize)(path);
  30. else
  31. return path;
  32. }
  33. // src/rspack/context.ts
  34. var import_buffer = require("buffer");
  35. var import_path2 = require("path");
  36. var import_acorn = require("acorn");
  37. function createBuildContext(compiler, compilation, loaderContext) {
  38. return {
  39. getNativeBuildContext() {
  40. return {
  41. framework: "rspack",
  42. compiler,
  43. compilation,
  44. loaderContext
  45. };
  46. },
  47. addWatchFile(file) {
  48. compilation.fileDependencies.add((0, import_path2.resolve)(process.cwd(), file));
  49. },
  50. getWatchFiles() {
  51. return Array.from(compilation.fileDependencies);
  52. },
  53. parse(code, opts = {}) {
  54. return import_acorn.Parser.parse(code, {
  55. sourceType: "module",
  56. ecmaVersion: "latest",
  57. locations: true,
  58. ...opts
  59. });
  60. },
  61. emitFile(emittedFile) {
  62. const outFileName = emittedFile.fileName || emittedFile.name;
  63. if (emittedFile.source && outFileName) {
  64. const { sources } = compilation.compiler.webpack;
  65. compilation.emitAsset(
  66. outFileName,
  67. new sources.RawSource(
  68. typeof emittedFile.source === "string" ? emittedFile.source : import_buffer.Buffer.from(emittedFile.source)
  69. )
  70. );
  71. }
  72. }
  73. };
  74. }
  75. function createContext(loader) {
  76. return {
  77. error: (error) => loader.emitError(normalizeMessage(error)),
  78. warn: (message) => loader.emitWarning(normalizeMessage(message))
  79. };
  80. }
  81. function normalizeMessage(error) {
  82. const err = new Error(typeof error === "string" ? error : error.message);
  83. if (typeof error === "object") {
  84. err.stack = error.stack;
  85. err.cause = error.meta;
  86. }
  87. return err;
  88. }
  89. // src/rspack/utils.ts
  90. var import_path3 = require("path");
  91. function decodeVirtualModuleId(encoded, _plugin) {
  92. return decodeURIComponent((0, import_path3.basename)(encoded));
  93. }
  94. function isVirtualModuleId(encoded, plugin) {
  95. return (0, import_path3.dirname)(encoded) === plugin.__virtualModulePrefix;
  96. }
  97. // src/rspack/loaders/load.ts
  98. async function load(source, map) {
  99. var _a;
  100. const callback = this.async();
  101. const { unpluginName } = this.query;
  102. const plugin = (_a = this._compiler) == null ? void 0 : _a.$unpluginContext[unpluginName];
  103. let id = this.resource;
  104. if (!(plugin == null ? void 0 : plugin.load) || !id)
  105. return callback(null, source, map);
  106. if (isVirtualModuleId(id, plugin))
  107. id = decodeVirtualModuleId(id, plugin);
  108. const context = createContext(this);
  109. const res = await plugin.load.call(
  110. Object.assign(
  111. {},
  112. this._compilation && createBuildContext(this._compiler, this._compilation, this),
  113. context
  114. ),
  115. normalizeAbsolutePath(id)
  116. );
  117. if (res == null)
  118. callback(null, source, map);
  119. else if (typeof res !== "string")
  120. callback(null, res.code, res.map ?? map);
  121. else
  122. callback(null, res, map);
  123. }