transform.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/transform.ts
  30. var transform_exports = {};
  31. __export(transform_exports, {
  32. default: () => transform
  33. });
  34. module.exports = __toCommonJS(transform_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/rspack/loaders/transform.ts
  83. async function transform(source, map) {
  84. const callback = this.async();
  85. let unpluginName;
  86. if (typeof this.query === "string") {
  87. const query = new URLSearchParams(this.query);
  88. unpluginName = query.get("unpluginName");
  89. } else {
  90. unpluginName = this.query.unpluginName;
  91. }
  92. const id = this.resource;
  93. const plugin = this._compiler?.$unpluginContext[unpluginName];
  94. if (!plugin?.transform)
  95. return callback(null, source, map);
  96. const context = createContext(this);
  97. const res = await plugin.transform.call(
  98. Object.assign(
  99. this._compilation && createBuildContext(this._compilation),
  100. context
  101. ),
  102. source,
  103. id
  104. );
  105. if (res == null)
  106. callback(null, source, map);
  107. else if (typeof res !== "string")
  108. callback(null, res.code, map == null ? map : res.map || map);
  109. else
  110. callback(null, res, map);
  111. }