30fa96602f20579639d204a37b22ede2faa7e758cbdd43eb1720b3336b99eb5b48631aaaa2b04452dafecbc50b2cc4bde4824b747122179abcf5f724e99e26 3.3 KB

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