load.js 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/rspack/context.ts
  26. var import_path = require("path");
  27. var import_buffer = require("buffer");
  28. var import_acorn = require("acorn");
  29. function createBuildContext(compiler, compilation, loaderContext) {
  30. return {
  31. getNativeBuildContext() {
  32. return {
  33. framework: "rspack",
  34. compiler,
  35. compilation,
  36. loaderContext
  37. };
  38. },
  39. addWatchFile(file) {
  40. compilation.fileDependencies.add((0, import_path.resolve)(process.cwd(), file));
  41. },
  42. getWatchFiles() {
  43. return Array.from(compilation.fileDependencies);
  44. },
  45. parse(code, opts = {}) {
  46. return import_acorn.Parser.parse(code, {
  47. sourceType: "module",
  48. ecmaVersion: "latest",
  49. locations: true,
  50. ...opts
  51. });
  52. },
  53. emitFile(emittedFile) {
  54. const outFileName = emittedFile.fileName || emittedFile.name;
  55. if (emittedFile.source && outFileName) {
  56. const { sources } = compilation.compiler.webpack;
  57. compilation.emitAsset(
  58. outFileName,
  59. new sources.RawSource(
  60. typeof emittedFile.source === "string" ? emittedFile.source : import_buffer.Buffer.from(emittedFile.source)
  61. )
  62. );
  63. }
  64. }
  65. };
  66. }
  67. function createContext(loader) {
  68. return {
  69. error: (error) => loader.emitError(normalizeMessage(error)),
  70. warn: (message) => loader.emitWarning(normalizeMessage(message))
  71. };
  72. }
  73. function normalizeMessage(error) {
  74. const err = new Error(typeof error === "string" ? error : error.message);
  75. if (typeof error === "object") {
  76. err.stack = error.stack;
  77. err.cause = error.meta;
  78. }
  79. return err;
  80. }
  81. // src/utils.ts
  82. var import_path2 = require("path");
  83. function normalizeAbsolutePath(path) {
  84. if ((0, import_path2.isAbsolute)(path))
  85. return (0, import_path2.normalize)(path);
  86. else
  87. return path;
  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. }