bf6bf2fb2639a5f92553e3763673b764478d231b9b8bb464510bc7e2f70fe95b810e29e0169480fd7fa8df22b45a01f406a1853ab72d44fbf7cdcc1ba89a9d 4.5 KB

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