transform.mjs 2.7 KB

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