load.mjs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // src/webpack/context.ts
  2. import { resolve } from "path";
  3. import { Buffer } from "buffer";
  4. import process from "process";
  5. import sources from "webpack-sources";
  6. import { Parser } from "acorn";
  7. function createBuildContext(options, compilation) {
  8. return {
  9. parse(code, opts = {}) {
  10. return Parser.parse(code, {
  11. sourceType: "module",
  12. ecmaVersion: "latest",
  13. locations: true,
  14. ...opts
  15. });
  16. },
  17. addWatchFile(id) {
  18. options.addWatchFile(resolve(process.cwd(), id));
  19. },
  20. emitFile(emittedFile) {
  21. const outFileName = emittedFile.fileName || emittedFile.name;
  22. if (emittedFile.source && outFileName) {
  23. if (!compilation)
  24. throw new Error("unplugin/webpack: emitFile outside supported hooks (buildStart, buildEnd, load, transform, watchChange)");
  25. compilation.emitAsset(
  26. outFileName,
  27. sources ? new sources.RawSource(
  28. // @ts-expect-error types mismatch
  29. typeof emittedFile.source === "string" ? emittedFile.source : Buffer.from(emittedFile.source)
  30. ) : {
  31. source: () => emittedFile.source,
  32. size: () => emittedFile.source.length
  33. }
  34. );
  35. }
  36. },
  37. getWatchFiles() {
  38. return options.getWatchFiles();
  39. }
  40. };
  41. }
  42. function createContext(loader) {
  43. return {
  44. error: (error) => loader.emitError(normalizeMessage(error)),
  45. warn: (message) => loader.emitWarning(normalizeMessage(message))
  46. };
  47. }
  48. function normalizeMessage(error) {
  49. const err = new Error(typeof error === "string" ? error : error.message);
  50. if (typeof error === "object") {
  51. err.stack = error.stack;
  52. err.cause = error.meta;
  53. }
  54. return err;
  55. }
  56. // src/utils.ts
  57. import { isAbsolute, normalize } from "path";
  58. function normalizeAbsolutePath(path) {
  59. if (isAbsolute(path))
  60. return normalize(path);
  61. else
  62. return path;
  63. }
  64. // src/webpack/loaders/load.ts
  65. async function load(source, map) {
  66. const callback = this.async();
  67. const { unpluginName } = this.query;
  68. const plugin = this._compiler?.$unpluginContext[unpluginName];
  69. let id = this.resource;
  70. if (!plugin?.load || !id)
  71. return callback(null, source, map);
  72. if (id.startsWith(plugin.__virtualModulePrefix))
  73. id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
  74. const context = createContext(this);
  75. const res = await plugin.load.call(
  76. { ...createBuildContext({
  77. addWatchFile: (file) => {
  78. this.addDependency(file);
  79. },
  80. getWatchFiles: () => {
  81. return this.getDependencies();
  82. }
  83. }, this._compilation), ...context },
  84. normalizeAbsolutePath(id)
  85. );
  86. if (res == null)
  87. callback(null, source, map);
  88. else if (typeof res !== "string")
  89. callback(null, res.code, res.map ?? map);
  90. else
  91. callback(null, res, map);
  92. }
  93. export {
  94. load as default
  95. };