load.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/load.ts
  26. var load_exports = {};
  27. __export(load_exports, {
  28. default: () => load
  29. });
  30. module.exports = __toCommonJS(load_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/utils.ts
  72. var import_path2 = require("path");
  73. function normalizeAbsolutePath(path) {
  74. if ((0, import_path2.isAbsolute)(path))
  75. return (0, import_path2.normalize)(path);
  76. else
  77. return path;
  78. }
  79. // src/webpack/loaders/load.ts
  80. async function load(source, map) {
  81. const callback = this.async();
  82. const { unpluginName } = this.query;
  83. const plugin = this._compiler?.$unpluginContext[unpluginName];
  84. let id = this.resource;
  85. if (!plugin?.load || !id)
  86. return callback(null, source, map);
  87. const context = {
  88. error: (error) => this.emitError(typeof error === "string" ? new Error(error) : error),
  89. warn: (error) => this.emitWarning(typeof error === "string" ? new Error(error) : error)
  90. };
  91. if (id.startsWith(plugin.__virtualModulePrefix))
  92. id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
  93. const res = await plugin.load.call(
  94. Object.assign(this._compilation && createContext(this._compilation), context),
  95. normalizeAbsolutePath(id)
  96. );
  97. if (res == null)
  98. callback(null, source, map);
  99. else if (typeof res !== "string")
  100. callback(null, res.code, res.map ?? map);
  101. else
  102. callback(null, res, map);
  103. }
  104. // Annotate the CommonJS export names for ESM import in node:
  105. 0 && (module.exports = {});