load.js 1.1 KB

12345678910111213141516171819202122232425262728
  1. import { normalizeObjectHook } from "../../context-D49cMElb.js";
  2. import { normalizeAbsolutePath } from "../../webpack-like-CSbnjTNU.js";
  3. import { createBuildContext, createContext } from "../../context-BZKy5Nsn.js";
  4. //#region src/webpack/loaders/load.ts
  5. async function load(source, map) {
  6. const callback = this.async();
  7. const { plugin } = this.query;
  8. let id = this.resource;
  9. if (!plugin?.load || !id) return callback(null, source, map);
  10. if (id.startsWith(plugin.__virtualModulePrefix)) id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
  11. const context = createContext(this);
  12. const { handler } = normalizeObjectHook("load", plugin.load);
  13. const res = await handler.call(Object.assign({}, createBuildContext({
  14. addWatchFile: (file) => {
  15. this.addDependency(file);
  16. },
  17. getWatchFiles: () => {
  18. return this.getDependencies();
  19. }
  20. }, this._compiler, this._compilation, this), context), normalizeAbsolutePath(id));
  21. if (res == null) callback(null, source, map);
  22. else if (typeof res !== "string") callback(null, res.code, res.map ?? map);
  23. else callback(null, res, map);
  24. }
  25. //#endregion
  26. export { load as default };