73cd2b81ba29245a8c44b2d1244311e180c0f018e8c762a8a58b01fd78576f444381503c08bba747a791ac5b0d31d2349b2d7148b279adbf7dc54c72f3dddb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/utils.ts
  36. var import_path = require("path");
  37. function normalizeAbsolutePath(path) {
  38. if ((0, import_path.isAbsolute)(path))
  39. return (0, import_path.normalize)(path);
  40. else
  41. return path;
  42. }
  43. function resolveQuery(query) {
  44. if (typeof query === "string") {
  45. return new URLSearchParams(query).get("unpluginName");
  46. } else {
  47. return query.unpluginName;
  48. }
  49. }
  50. // src/webpack/context.ts
  51. var import_buffer = require("buffer");
  52. var import_module = require("module");
  53. var import_path2 = require("path");
  54. var import_process = __toESM(require("process"));
  55. var import_acorn = require("acorn");
  56. function getSource(fileSource) {
  57. const webpackRequire = (0, import_module.createRequire)(require.resolve("webpack"));
  58. const RawSource = webpackRequire("webpack-sources").RawSource;
  59. return new RawSource(
  60. typeof fileSource === "string" ? fileSource : import_buffer.Buffer.from(fileSource.buffer).toString("utf-8")
  61. );
  62. }
  63. function createBuildContext(options, compiler, compilation, loaderContext) {
  64. return {
  65. parse(code, opts = {}) {
  66. return import_acorn.Parser.parse(code, {
  67. sourceType: "module",
  68. ecmaVersion: "latest",
  69. locations: true,
  70. ...opts
  71. });
  72. },
  73. addWatchFile(id) {
  74. options.addWatchFile((0, import_path2.resolve)(import_process.default.cwd(), id));
  75. },
  76. emitFile(emittedFile) {
  77. const outFileName = emittedFile.fileName || emittedFile.name;
  78. if (emittedFile.source && outFileName) {
  79. if (!compilation)
  80. throw new Error("unplugin/webpack: emitFile outside supported hooks (buildStart, buildEnd, load, transform, watchChange)");
  81. compilation.emitAsset(
  82. outFileName,
  83. getSource(emittedFile.source)
  84. );
  85. }
  86. },
  87. getWatchFiles() {
  88. return options.getWatchFiles();
  89. },
  90. getNativeBuildContext() {
  91. return { framework: "webpack", compiler, compilation, loaderContext };
  92. }
  93. };
  94. }
  95. function createContext(loader) {
  96. return {
  97. error: (error) => loader.emitError(normalizeMessage(error)),
  98. warn: (message) => loader.emitWarning(normalizeMessage(message))
  99. };
  100. }
  101. function normalizeMessage(error) {
  102. const err = new Error(typeof error === "string" ? error : error.message);
  103. if (typeof error === "object") {
  104. err.stack = error.stack;
  105. err.cause = error.meta;
  106. }
  107. return err;
  108. }
  109. // src/webpack/loaders/load.ts
  110. async function load(source, map) {
  111. var _a;
  112. const callback = this.async();
  113. const unpluginName = resolveQuery(this.query);
  114. const plugin = (_a = this._compiler) == null ? void 0 : _a.$unpluginContext[unpluginName];
  115. let id = this.resource;
  116. if (!(plugin == null ? void 0 : plugin.load) || !id)
  117. return callback(null, source, map);
  118. if (id.startsWith(plugin.__virtualModulePrefix))
  119. id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
  120. const context = createContext(this);
  121. const res = await plugin.load.call(
  122. Object.assign({}, createBuildContext({
  123. addWatchFile: (file) => {
  124. this.addDependency(file);
  125. },
  126. getWatchFiles: () => {
  127. return this.getDependencies();
  128. }
  129. }, this._compiler, this._compilation, this), context),
  130. normalizeAbsolutePath(id)
  131. );
  132. if (res == null)
  133. callback(null, source, map);
  134. else if (typeof res !== "string")
  135. callback(null, res.code, res.map ?? map);
  136. else
  137. callback(null, res, map);
  138. }