load.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // src/rspack/context.ts
  2. import { resolve } from "path";
  3. import { Buffer } from "buffer";
  4. import { Parser } from "acorn";
  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. compilation.fileDependencies.add(resolve(process.cwd(), file));
  17. },
  18. getWatchFiles() {
  19. return Array.from(compilation.fileDependencies);
  20. },
  21. parse(code, opts = {}) {
  22. return Parser.parse(code, {
  23. sourceType: "module",
  24. ecmaVersion: "latest",
  25. locations: true,
  26. ...opts
  27. });
  28. },
  29. emitFile(emittedFile) {
  30. const outFileName = emittedFile.fileName || emittedFile.name;
  31. if (emittedFile.source && outFileName) {
  32. const { sources } = compilation.compiler.webpack;
  33. compilation.emitAsset(
  34. outFileName,
  35. new sources.RawSource(
  36. typeof emittedFile.source === "string" ? emittedFile.source : Buffer.from(emittedFile.source)
  37. )
  38. );
  39. }
  40. }
  41. };
  42. }
  43. function createContext(loader) {
  44. return {
  45. error: (error) => loader.emitError(normalizeMessage(error)),
  46. warn: (message) => loader.emitWarning(normalizeMessage(message))
  47. };
  48. }
  49. function normalizeMessage(error) {
  50. const err = new Error(typeof error === "string" ? error : error.message);
  51. if (typeof error === "object") {
  52. err.stack = error.stack;
  53. err.cause = error.meta;
  54. }
  55. return err;
  56. }
  57. // src/utils.ts
  58. import { isAbsolute, normalize } from "path";
  59. function normalizeAbsolutePath(path) {
  60. if (isAbsolute(path))
  61. return normalize(path);
  62. else
  63. return path;
  64. }
  65. // src/rspack/utils.ts
  66. import { basename, dirname, resolve as resolve2 } from "path";
  67. function decodeVirtualModuleId(encoded, _plugin) {
  68. return decodeURIComponent(basename(encoded));
  69. }
  70. function isVirtualModuleId(encoded, plugin) {
  71. return dirname(encoded) === plugin.__virtualModulePrefix;
  72. }
  73. // src/rspack/loaders/load.ts
  74. async function load(source, map) {
  75. var _a;
  76. const callback = this.async();
  77. const { unpluginName } = this.query;
  78. const plugin = (_a = this._compiler) == null ? void 0 : _a.$unpluginContext[unpluginName];
  79. let id = this.resource;
  80. if (!(plugin == null ? void 0 : plugin.load) || !id)
  81. return callback(null, source, map);
  82. if (isVirtualModuleId(id, plugin))
  83. id = decodeVirtualModuleId(id, plugin);
  84. const context = createContext(this);
  85. const res = await plugin.load.call(
  86. Object.assign(
  87. {},
  88. this._compilation && createBuildContext(this._compiler, this._compilation, this),
  89. context
  90. ),
  91. normalizeAbsolutePath(id)
  92. );
  93. if (res == null)
  94. callback(null, source, map);
  95. else if (typeof res !== "string")
  96. callback(null, res.code, res.map ?? map);
  97. else
  98. callback(null, res, map);
  99. }
  100. export {
  101. load as default
  102. };