load.mjs 3.6 KB

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