版博士V2.0程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

73 lignes
1.9 KiB

  1. // src/rspack/context.ts
  2. import sources from "webpack-sources";
  3. import { Parser } from "acorn";
  4. function createRspackContext(compilation) {
  5. return {
  6. parse(code, opts = {}) {
  7. return Parser.parse(code, {
  8. sourceType: "module",
  9. ecmaVersion: "latest",
  10. locations: true,
  11. ...opts
  12. });
  13. },
  14. addWatchFile() {
  15. },
  16. emitFile(emittedFile) {
  17. const outFileName = emittedFile.fileName || emittedFile.name;
  18. if (emittedFile.source && outFileName) {
  19. compilation.emitAsset(
  20. outFileName,
  21. new sources.RawSource(
  22. // @ts-expect-error types mismatch
  23. typeof emittedFile.source === "string" ? emittedFile.source : Buffer.from(emittedFile.source)
  24. )
  25. );
  26. }
  27. },
  28. getWatchFiles() {
  29. return [];
  30. }
  31. };
  32. }
  33. // src/utils.ts
  34. import { isAbsolute, normalize } from "path";
  35. function normalizeAbsolutePath(path) {
  36. if (isAbsolute(path))
  37. return normalize(path);
  38. else
  39. return path;
  40. }
  41. // src/rspack/loaders/load.ts
  42. async function load(source, map) {
  43. const callback = this.async();
  44. const id = this.resource;
  45. const { plugin } = this.getOptions();
  46. if (!plugin?.load || !id)
  47. return callback(null, source, map);
  48. if (plugin.loadInclude && !plugin.loadInclude(id))
  49. return callback(null, source, map);
  50. const context = {
  51. error: (error) => this.emitError(typeof error === "string" ? new Error(error) : error),
  52. warn: (error) => this.emitWarning(typeof error === "string" ? new Error(error) : error)
  53. };
  54. const res = await plugin.load.call(
  55. Object.assign(
  56. this._compilation && createRspackContext(this._compilation),
  57. context
  58. ),
  59. normalizeAbsolutePath(id)
  60. );
  61. if (res == null)
  62. callback(null, source, map);
  63. else if (typeof res !== "string")
  64. callback(null, res.code, res.map ?? map);
  65. else
  66. callback(null, res, map);
  67. }
  68. export {
  69. load as default
  70. };