load.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/load.ts
  30. var load_exports = {};
  31. __export(load_exports, {
  32. default: () => load
  33. });
  34. module.exports = __toCommonJS(load_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/utils.ts
  91. var import_path2 = require("path");
  92. function normalizeAbsolutePath(path) {
  93. if ((0, import_path2.isAbsolute)(path))
  94. return (0, import_path2.normalize)(path);
  95. else
  96. return path;
  97. }
  98. // src/webpack/loaders/load.ts
  99. async function load(source, map) {
  100. const callback = this.async();
  101. const { unpluginName } = this.query;
  102. const plugin = this._compiler?.$unpluginContext[unpluginName];
  103. let id = this.resource;
  104. if (!plugin?.load || !id)
  105. return callback(null, source, map);
  106. if (id.startsWith(plugin.__virtualModulePrefix))
  107. id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
  108. const context = createContext(this);
  109. const res = await plugin.load.call(
  110. { ...createBuildContext({
  111. addWatchFile: (file) => {
  112. this.addDependency(file);
  113. },
  114. getWatchFiles: () => {
  115. return this.getDependencies();
  116. }
  117. }, this._compilation), ...context },
  118. normalizeAbsolutePath(id)
  119. );
  120. if (res == null)
  121. callback(null, source, map);
  122. else if (typeof res !== "string")
  123. callback(null, res.code, res.map ?? map);
  124. else
  125. callback(null, res, map);
  126. }