4c4279301bd64609da49f0259b069575dce0051f7fd13349a86f98cb11593deb7bde45954b26f74fcd6e8e4125f2c6c168135e7bd02b2b04b6444d1ed4e7cd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // src/utils.ts
  2. import { isAbsolute, normalize } from "path";
  3. function normalizeAbsolutePath(path) {
  4. if (isAbsolute(path))
  5. return normalize(path);
  6. else
  7. return path;
  8. }
  9. // src/rspack/context.ts
  10. import { Buffer } from "buffer";
  11. import { resolve } from "path";
  12. import { Parser } from "acorn";
  13. function createBuildContext(compiler, compilation, loaderContext) {
  14. return {
  15. getNativeBuildContext() {
  16. return {
  17. framework: "rspack",
  18. compiler,
  19. compilation,
  20. loaderContext
  21. };
  22. },
  23. addWatchFile(file) {
  24. compilation.fileDependencies.add(resolve(process.cwd(), file));
  25. },
  26. getWatchFiles() {
  27. return Array.from(compilation.fileDependencies);
  28. },
  29. parse(code, opts = {}) {
  30. return Parser.parse(code, {
  31. sourceType: "module",
  32. ecmaVersion: "latest",
  33. locations: true,
  34. ...opts
  35. });
  36. },
  37. emitFile(emittedFile) {
  38. const outFileName = emittedFile.fileName || emittedFile.name;
  39. if (emittedFile.source && outFileName) {
  40. const { sources } = compilation.compiler.webpack;
  41. compilation.emitAsset(
  42. outFileName,
  43. new sources.RawSource(
  44. typeof emittedFile.source === "string" ? emittedFile.source : Buffer.from(emittedFile.source)
  45. )
  46. );
  47. }
  48. }
  49. };
  50. }
  51. function createContext(loader) {
  52. return {
  53. error: (error) => loader.emitError(normalizeMessage(error)),
  54. warn: (message) => loader.emitWarning(normalizeMessage(message))
  55. };
  56. }
  57. function normalizeMessage(error) {
  58. const err = new Error(typeof error === "string" ? error : error.message);
  59. if (typeof error === "object") {
  60. err.stack = error.stack;
  61. err.cause = error.meta;
  62. }
  63. return err;
  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. };