Skip to content

Commit

Permalink
fixup! src,lib: retrieve parsed source map url from v8
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Oct 5, 2022
1 parent 86de757 commit 9c6839a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 38 deletions.
10 changes: 8 additions & 2 deletions lib/internal/modules/cjs/loader.js
Expand Up @@ -1084,7 +1084,10 @@ function wrapSafe(filename, content, cjsModuleInstance) {
},
});

maybeCacheSourceMap(filename, content, this, false, undefined, script.sourceMapURL);
// Cache the source map for the module if present.
if (script.sourceMapURL) {
maybeCacheSourceMap(filename, content, this, false, undefined, script.sourceMapURL);
}

return script.runInThisContext({
displayErrors: true,
Expand All @@ -1107,7 +1110,10 @@ function wrapSafe(filename, content, cjsModuleInstance) {
},
});

maybeCacheSourceMap(filename, content, this, false, undefined, result.sourceMapURL);
// Cache the source map for the module if present.
if (result.sourceMapURL) {
maybeCacheSourceMap(filename, content, this, false, undefined, result.sourceMapURL);
}

return result.function;
} catch (err) {
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/process/pre_execution.js
Expand Up @@ -579,9 +579,11 @@ function initializeESMLoader() {
}

function initializeSourceMapsHandlers() {
const { setSourceMapsEnabled } =
const { setSourceMapsEnabled, getSourceMapsEnabled } =
require('internal/source_map/source_map_cache');
process.setSourceMapsEnabled = setSourceMapsEnabled;
// Initialize the environment flag of source maps.
getSourceMapsEnabled();
}

function initializeFrozenIntrinsics() {
Expand Down
73 changes: 38 additions & 35 deletions lib/internal/source_map/source_map_cache.js
Expand Up @@ -126,45 +126,48 @@ function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSo
sourceMapURL = extractSourceMapURLMagicComment(content);
}

// Bail out when there is no source map url.
if (typeof sourceMapURL !== 'string') {
return;
}

if (sourceURL === undefined) {
sourceURL = extractSourceURLMagicComment(content);
}

if (sourceMapURL) {
const data = dataFromUrl(filename, sourceMapURL);
const url = data ? null : sourceMapURL;
if (cjsModuleInstance) {
cjsSourceMapCache.set(cjsModuleInstance, {
filename,
lineLengths: lineLengths(content),
data,
url,
sourceURL,
});
} else if (isGeneratedSource) {
const entry = {
lineLengths: lineLengths(content),
data,
url,
sourceURL
};
generatedSourceMapCache.set(filename, entry);
if (sourceURL) {
generatedSourceMapCache.set(sourceURL, entry);
}
} else {
// If there is no cjsModuleInstance and is not generated source assume we are in a
// "modules/esm" context.
const entry = {
lineLengths: lineLengths(content),
data,
url,
sourceURL,
};
esmSourceMapCache.set(filename, entry);
if (sourceURL) {
esmSourceMapCache.set(sourceURL, entry);
}
const data = dataFromUrl(filename, sourceMapURL);
const url = data ? null : sourceMapURL;
if (cjsModuleInstance) {
cjsSourceMapCache.set(cjsModuleInstance, {
filename,
lineLengths: lineLengths(content),
data,
url,
sourceURL,
});
} else if (isGeneratedSource) {
const entry = {
lineLengths: lineLengths(content),
data,
url,
sourceURL
};
generatedSourceMapCache.set(filename, entry);
if (sourceURL) {
generatedSourceMapCache.set(sourceURL, entry);
}
} else {
// If there is no cjsModuleInstance and is not generated source assume we are in a
// "modules/esm" context.
const entry = {
lineLengths: lineLengths(content),
data,
url,
sourceURL,
};
esmSourceMapCache.set(filename, entry);
if (sourceURL) {
esmSourceMapCache.set(sourceURL, entry);
}
}
}
Expand Down

0 comments on commit 9c6839a

Please sign in to comment.