load.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. // src/rspack/loaders/load.ts
  30. var load_exports = {};
  31. __export(load_exports, {
  32. default: () => load
  33. });
  34. module.exports = __toCommonJS(load_exports);
  35. // src/rspack/context.ts
  36. var import_buffer = require("buffer");
  37. var import_webpack_sources = __toESM(require("webpack-sources"));
  38. var import_acorn = require("acorn");
  39. function createBuildContext(compilation) {
  40. return {
  41. parse(code, opts = {}) {
  42. return import_acorn.Parser.parse(code, {
  43. sourceType: "module",
  44. ecmaVersion: "latest",
  45. locations: true,
  46. ...opts
  47. });
  48. },
  49. addWatchFile() {
  50. },
  51. emitFile(emittedFile) {
  52. const outFileName = emittedFile.fileName || emittedFile.name;
  53. if (emittedFile.source && outFileName) {
  54. compilation.emitAsset(
  55. outFileName,
  56. new import_webpack_sources.default.RawSource(
  57. // @ts-expect-error types mismatch
  58. typeof emittedFile.source === "string" ? emittedFile.source : import_buffer.Buffer.from(emittedFile.source)
  59. )
  60. );
  61. }
  62. },
  63. getWatchFiles() {
  64. return [];
  65. }
  66. };
  67. }
  68. function createContext(loader) {
  69. return {
  70. error: (error) => loader.emitError(normalizeMessage(error)),
  71. warn: (message) => loader.emitWarning(normalizeMessage(message))
  72. };
  73. }
  74. function normalizeMessage(error) {
  75. const err = new Error(typeof error === "string" ? error : error.message);
  76. if (typeof error === "object") {
  77. err.stack = error.stack;
  78. err.cause = error.meta;
  79. }
  80. return err;
  81. }
  82. // src/utils.ts
  83. var import_path = require("path");
  84. function normalizeAbsolutePath(path) {
  85. if ((0, import_path.isAbsolute)(path))
  86. return (0, import_path.normalize)(path);
  87. else
  88. return path;
  89. }
  90. // src/rspack/loaders/load.ts
  91. async function load(source, map) {
  92. const callback = this.async();
  93. const { unpluginName } = this.query;
  94. const plugin = this._compiler?.$unpluginContext[unpluginName];
  95. const id = this.resource;
  96. if (!plugin?.load || !id)
  97. return callback(null, source, map);
  98. if (plugin.loadInclude && !plugin.loadInclude(id))
  99. return callback(null, source, map);
  100. const context = createContext(this);
  101. const res = await plugin.load.call(
  102. Object.assign(
  103. this._compilation && createBuildContext(this._compilation),
  104. context
  105. ),
  106. normalizeAbsolutePath(id)
  107. );
  108. if (res == null)
  109. callback(null, source, map);
  110. else if (typeof res !== "string")
  111. callback(null, res.code, res.map ?? map);
  112. else
  113. callback(null, res, map);
  114. }