transform.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/webpack/loaders/transform.ts
  30. var transform_exports = {};
  31. __export(transform_exports, {
  32. default: () => transform
  33. });
  34. module.exports = __toCommonJS(transform_exports);
  35. // src/webpack/context.ts
  36. var import_path = require("path");
  37. var import_buffer = require("buffer");
  38. var import_process = __toESM(require("process"));
  39. var import_webpack_sources = __toESM(require("webpack-sources"));
  40. var import_acorn = require("acorn");
  41. function createBuildContext(options, compilation) {
  42. return {
  43. parse(code, opts = {}) {
  44. return import_acorn.Parser.parse(code, {
  45. sourceType: "module",
  46. ecmaVersion: "latest",
  47. locations: true,
  48. ...opts
  49. });
  50. },
  51. addWatchFile(id) {
  52. options.addWatchFile((0, import_path.resolve)(import_process.default.cwd(), id));
  53. },
  54. emitFile(emittedFile) {
  55. const outFileName = emittedFile.fileName || emittedFile.name;
  56. if (emittedFile.source && outFileName) {
  57. if (!compilation)
  58. throw new Error("unplugin/webpack: emitFile outside supported hooks (buildStart, buildEnd, load, transform, watchChange)");
  59. compilation.emitAsset(
  60. outFileName,
  61. import_webpack_sources.default ? new import_webpack_sources.default.RawSource(
  62. // @ts-expect-error types mismatch
  63. typeof emittedFile.source === "string" ? emittedFile.source : import_buffer.Buffer.from(emittedFile.source)
  64. ) : {
  65. source: () => emittedFile.source,
  66. size: () => emittedFile.source.length
  67. }
  68. );
  69. }
  70. },
  71. getWatchFiles() {
  72. return options.getWatchFiles();
  73. }
  74. };
  75. }
  76. function createContext(loader) {
  77. return {
  78. error: (error) => loader.emitError(normalizeMessage(error)),
  79. warn: (message) => loader.emitWarning(normalizeMessage(message))
  80. };
  81. }
  82. function normalizeMessage(error) {
  83. const err = new Error(typeof error === "string" ? error : error.message);
  84. if (typeof error === "object") {
  85. err.stack = error.stack;
  86. err.cause = error.meta;
  87. }
  88. return err;
  89. }
  90. // src/webpack/loaders/transform.ts
  91. async function transform(source, map) {
  92. const callback = this.async();
  93. let unpluginName;
  94. if (typeof this.query === "string") {
  95. const query = new URLSearchParams(this.query);
  96. unpluginName = query.get("unpluginName");
  97. } else {
  98. unpluginName = this.query.unpluginName;
  99. }
  100. const plugin = this._compiler?.$unpluginContext[unpluginName];
  101. if (!plugin?.transform)
  102. return callback(null, source, map);
  103. const context = createContext(this);
  104. const res = await plugin.transform.call(
  105. { ...createBuildContext({
  106. addWatchFile: (file) => {
  107. this.addDependency(file);
  108. },
  109. getWatchFiles: () => {
  110. return this.getDependencies();
  111. }
  112. }, this._compilation), ...context },
  113. source,
  114. this.resource
  115. );
  116. if (res == null)
  117. callback(null, source, map);
  118. else if (typeof res !== "string")
  119. callback(null, res.code, map == null ? map : res.map || map);
  120. else
  121. callback(null, res, map);
  122. }