getPaths.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. /** @typedef {import("webpack").Compiler} Compiler */
  3. /** @typedef {import("webpack").Stats} Stats */
  4. /** @typedef {import("webpack").MultiStats} MultiStats */
  5. /** @typedef {import("webpack").Asset} Asset */
  6. /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
  7. /** @typedef {import("../index.js").ServerResponse} ServerResponse */
  8. /**
  9. * @template {IncomingMessage} Request
  10. * @template {ServerResponse} Response
  11. * @param {import("../index.js").FilledContext<Request, Response>} context context
  12. * @returns {{ outputPath: string, publicPath: string, assetsInfo: Asset["info"] }[]} paths
  13. */
  14. function getPaths(context) {
  15. const {
  16. stats,
  17. options
  18. } = context;
  19. /* eslint-disable unicorn/prefer-logical-operator-over-ternary */
  20. /** @type {Stats[]} */
  21. const childStats = /** @type {MultiStats} */
  22. stats.stats ? /** @type {MultiStats} */stats.stats : [(/** @type {Stats} */stats)];
  23. /** @type {{ outputPath: string, publicPath: string, assetsInfo: Asset["info"] }[]} */
  24. const publicPaths = [];
  25. for (const {
  26. compilation
  27. } of childStats) {
  28. if (compilation.options.devServer === false) {
  29. continue;
  30. }
  31. // The `output.path` is always present and always absolute
  32. const outputPath = compilation.getPath(compilation.outputOptions.path || "");
  33. const publicPath = options.publicPath ? compilation.getPath(options.publicPath) : compilation.outputOptions.publicPath ? compilation.getPath(compilation.outputOptions.publicPath) : "";
  34. publicPaths.push({
  35. outputPath,
  36. publicPath,
  37. assetsInfo: compilation.assetsInfo
  38. });
  39. }
  40. return publicPaths;
  41. }
  42. module.exports = getPaths;