541686f17835488d53229021f80bfc66adff645948b7ba8a3a069046087ee8c4a5279eba516388ffb740cf0e6b261f825cd1940342a88f03fbffbf0444adf1 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // src/rspack/context.ts
  2. import { Buffer } from "buffer";
  3. import { resolve } from "path";
  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/rspack/loaders/transform.ts
  58. async function transform(source, map) {
  59. var _a;
  60. const callback = this.async();
  61. let unpluginName;
  62. if (typeof this.query === "string") {
  63. const query = new URLSearchParams(this.query);
  64. unpluginName = query.get("unpluginName");
  65. } else {
  66. unpluginName = this.query.unpluginName;
  67. }
  68. const id = this.resource;
  69. const plugin = (_a = this._compiler) == null ? void 0 : _a.$unpluginContext[unpluginName];
  70. if (!(plugin == null ? void 0 : plugin.transform))
  71. return callback(null, source, map);
  72. const context = createContext(this);
  73. const res = await plugin.transform.call(
  74. Object.assign(
  75. {},
  76. this._compilation && createBuildContext(this._compiler, this._compilation, this),
  77. context
  78. ),
  79. source,
  80. id
  81. );
  82. if (res == null)
  83. callback(null, source, map);
  84. else if (typeof res !== "string")
  85. callback(null, res.code, map == null ? map : res.map || map);
  86. else callback(null, res, map);
  87. }
  88. export {
  89. transform as default
  90. };