sourcemap.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getSourcemapModules = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const source_map_1 = require("source-map");
  9. const getBytesPerFileUsingSourceMap = (bundleId, code, map, dir) => {
  10. const modules = {};
  11. let line = 1;
  12. let column = 0;
  13. for (let i = 0; i < code.length; i++, column++) {
  14. const { source } = map.originalPositionFor({
  15. line,
  16. column,
  17. });
  18. if (source != null) {
  19. const id = path_1.default.resolve(path_1.default.dirname(path_1.default.join(dir, bundleId)), source);
  20. modules[id] = modules[id] || { id, renderedLength: 0 };
  21. modules[id].renderedLength += 1;
  22. }
  23. if (code[i] === "\n") {
  24. line += 1;
  25. column = -1;
  26. }
  27. }
  28. return modules;
  29. };
  30. const getSourcemapModules = (id, outputChunk, dir) => {
  31. if (!outputChunk.map) {
  32. return Promise.resolve({});
  33. }
  34. return source_map_1.SourceMapConsumer.with(outputChunk.map, null, (map) => {
  35. return getBytesPerFileUsingSourceMap(id, outputChunk.code, map, dir);
  36. });
  37. };
  38. exports.getSourcemapModules = getSourcemapModules;