transform.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. const callback = this.async();
  74. let unpluginName;
  75. if (typeof this.query === "string") {
  76. const query = new URLSearchParams(this.query);
  77. unpluginName = query.get("unpluginName");
  78. } else {
  79. unpluginName = this.query.unpluginName;
  80. }
  81. const plugin = this._compiler?.$unpluginContext[unpluginName];
  82. if (!plugin?.transform)
  83. return callback(null, source, map);
  84. const context = {
  85. error: (error) => this.emitError(typeof error === "string" ? new Error(error) : error),
  86. warn: (error) => this.emitWarning(typeof error === "string" ? new Error(error) : error)
  87. };
  88. const res = await plugin.transform.call(Object.assign(this._compilation && createContext(this._compilation), context), source, this.resource);
  89. if (res == null)
  90. callback(null, source, map);
  91. else if (typeof res !== "string")
  92. callback(null, res.code, map == null ? map : res.map || map);
  93. else
  94. callback(null, res, map);
  95. }
  96. // Annotate the CommonJS export names for ESM import in node:
  97. 0 && (module.exports = {});