getIdentitySourceMap.js 796 B

123456789101112131415161718192021222324252627282930
  1. const { SourceMapGenerator } = require('source-map');
  2. /**
  3. * Generates an identity source map from a source file.
  4. * @param {string} source The content of the source file.
  5. * @param {string} resourcePath The name of the source file.
  6. * @returns {import('source-map').RawSourceMap} The identity source map.
  7. */
  8. function getIdentitySourceMap(source, resourcePath) {
  9. const sourceMap = new SourceMapGenerator();
  10. sourceMap.setSourceContent(resourcePath, source);
  11. source.split('\n').forEach((line, index) => {
  12. sourceMap.addMapping({
  13. source: resourcePath,
  14. original: {
  15. line: index + 1,
  16. column: 0,
  17. },
  18. generated: {
  19. line: index + 1,
  20. column: 0,
  21. },
  22. });
  23. });
  24. return sourceMap.toJSON();
  25. }
  26. module.exports = getIdentitySourceMap;