transform.mjs 3.0 KB

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