transform.js 5.0 KB

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