transform.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  22. mod
  23. ));
  24. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  25. // src/webpack/loaders/transform.ts
  26. var transform_exports = {};
  27. __export(transform_exports, {
  28. default: () => transform
  29. });
  30. module.exports = __toCommonJS(transform_exports);
  31. // src/webpack/context.ts
  32. var import_path = require("path");
  33. var import_webpack_sources = __toESM(require("webpack-sources"));
  34. var import_acorn = require("acorn");
  35. function createContext(compilation) {
  36. return {
  37. parse(code, opts = {}) {
  38. return import_acorn.Parser.parse(code, {
  39. sourceType: "module",
  40. ecmaVersion: "latest",
  41. locations: true,
  42. ...opts
  43. });
  44. },
  45. addWatchFile(id) {
  46. (compilation.fileDependencies ?? compilation.compilationDependencies).add(
  47. (0, import_path.resolve)(process.cwd(), id)
  48. );
  49. },
  50. emitFile(emittedFile) {
  51. const outFileName = emittedFile.fileName || emittedFile.name;
  52. if (emittedFile.source && outFileName) {
  53. compilation.emitAsset(
  54. outFileName,
  55. import_webpack_sources.default ? new import_webpack_sources.default.RawSource(
  56. typeof emittedFile.source === "string" ? emittedFile.source : Buffer.from(emittedFile.source)
  57. ) : {
  58. source: () => emittedFile.source,
  59. size: () => emittedFile.source.length
  60. }
  61. );
  62. }
  63. },
  64. getWatchFiles() {
  65. return Array.from(
  66. compilation.fileDependencies ?? compilation.compilationDependencies
  67. );
  68. }
  69. };
  70. }
  71. // src/webpack/loaders/transform.ts
  72. async function transform(source, map) {
  73. var _a;
  74. const callback = this.async();
  75. let unpluginName;
  76. if (typeof this.query === "string") {
  77. const query = new URLSearchParams(this.query);
  78. unpluginName = query.get("unpluginName");
  79. } else {
  80. unpluginName = this.query.unpluginName;
  81. }
  82. const plugin = (_a = this._compiler) == null ? void 0 : _a.$unpluginContext[unpluginName];
  83. if (!(plugin == null ? void 0 : plugin.transform)) {
  84. return callback(null, source, map);
  85. }
  86. const context = {
  87. error: (error) => this.emitError(typeof error === "string" ? new Error(error) : error),
  88. warn: (error) => this.emitWarning(typeof error === "string" ? new Error(error) : error)
  89. };
  90. const res = await plugin.transform.call(Object.assign(this._compilation && createContext(this._compilation), context), source, this.resource);
  91. if (res == null) {
  92. callback(null, source, map);
  93. } else if (typeof res !== "string") {
  94. callback(null, res.code, map == null ? map : res.map || map);
  95. } else {
  96. callback(null, res, map);
  97. }
  98. }
  99. // Annotate the CommonJS export names for ESM import in node:
  100. 0 && (module.exports = {});