load.mjs 3.3 KB

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