load.js 3.8 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. 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. }
  80. // src/webpack/loaders/load.ts
  81. async function load(source, map) {
  82. var _a;
  83. const callback = this.async();
  84. const { unpluginName } = this.query;
  85. const plugin = (_a = this._compiler) == null ? void 0 : _a.$unpluginContext[unpluginName];
  86. let id = this.resource;
  87. if (!(plugin == null ? void 0 : plugin.load) || !id) {
  88. return callback(null, source, map);
  89. }
  90. const context = {
  91. error: (error) => this.emitError(typeof error === "string" ? new Error(error) : error),
  92. warn: (error) => this.emitWarning(typeof error === "string" ? new Error(error) : error)
  93. };
  94. if (id.startsWith(plugin.__virtualModulePrefix)) {
  95. id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
  96. }
  97. const res = await plugin.load.call(
  98. Object.assign(this._compilation && createContext(this._compilation), context),
  99. normalizeAbsolutePath(id)
  100. );
  101. if (res == null) {
  102. callback(null, source, map);
  103. } else if (typeof res !== "string") {
  104. callback(null, res.code, res.map ?? map);
  105. } else {
  106. callback(null, res, map);
  107. }
  108. }
  109. // Annotate the CommonJS export names for ESM import in node:
  110. 0 && (module.exports = {});