Skip to content

Commit

Permalink
Add missing sourcesContent in source maps when they are available i…
Browse files Browse the repository at this point in the history
…n the filesystem (#9)
  • Loading branch information
filipsobol authored Oct 12, 2024
1 parent b7831e3 commit 30ddb2b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/cool-roses-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"sonda": patch
"unplugin-sourcemaps": patch
---

Add missing `sourcesContent` in source maps when they are available in the filesystem
22 changes: 22 additions & 0 deletions packages/load-source-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export function loadCodeAndMap( codePath: string ): MaybeCodeMap {
const { map, mapPath } = maybeMap;

map.sources = normalizeSourcesPaths( map, mapPath );
map.sourcesContent = loadMissingSourcesContent( map );

delete map.sourceRoot;

return {
Expand Down Expand Up @@ -105,6 +107,9 @@ function parseDataUrl( url: string ): string {
}
}

/**
* Normalize the paths of the sources in the source map to be absolute paths.
*/
function normalizeSourcesPaths( map: SourceMapV3, mapPath: string ): SourceMapV3[ 'sources' ] {
const mapDir = dirname( mapPath );

Expand All @@ -118,3 +123,20 @@ function normalizeSourcesPaths( map: SourceMapV3, mapPath: string ): SourceMapV3
: resolve( mapDir, map.sourceRoot ?? '.', source );
} );
}

/**
* Loop through the sources and try to load missing `sourcesContent` from the file system.
*/
function loadMissingSourcesContent( map: SourceMapV3 ): Array<string | null> {
return map.sources.map( ( source, index ) => {
if ( map.sourcesContent?.[ index ] ) {
return map.sourcesContent[ index ];
}

if ( source && existsSync( source ) ) {
return readFileSync( source, 'utf-8' );
}

return null;
} );
}

0 comments on commit 30ddb2b

Please sign in to comment.