serverPluginSourceMap.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  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.sourceMapPlugin = exports.mergeSourceMap = void 0;
  7. const merge_source_map_1 = __importDefault(require("merge-source-map"));
  8. function mergeSourceMap(oldMap, newMap) {
  9. if (!oldMap) {
  10. return newMap;
  11. }
  12. // merge-source-map will overwrite original sources if newMap also has
  13. // sourcesContent
  14. newMap.sourcesContent = [];
  15. return merge_source_map_1.default(oldMap, newMap);
  16. }
  17. exports.mergeSourceMap = mergeSourceMap;
  18. function genSourceMapString(map) {
  19. if (typeof map !== 'string') {
  20. map = JSON.stringify(map);
  21. }
  22. return `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(map).toString('base64')}`;
  23. }
  24. exports.sourceMapPlugin = ({ app }) => {
  25. app.use(async (ctx, next) => {
  26. await next();
  27. if (typeof ctx.body === 'string' && ctx.map) {
  28. ctx.body += genSourceMapString(ctx.map);
  29. }
  30. });
  31. };
  32. //# sourceMappingURL=serverPluginSourceMap.js.map