@@ -29,6 +29,7 @@ const { IterableWeakMap } = require('internal/util/iterable_weak_map');
2929const {
3030 normalizeReferrerURL,
3131} = require ( 'internal/modules/cjs/helpers' ) ;
32+ const { validateBoolean } = require ( 'internal/validators' ) ;
3233// Since the CJS module cache is mutable, which leads to memory leaks when
3334// modules are deleted, we use a WeakMap so that the source map cache will
3435// be purged automatically:
@@ -41,22 +42,35 @@ let SourceMap;
4142let sourceMapsEnabled ;
4243function getSourceMapsEnabled ( ) {
4344 if ( sourceMapsEnabled === undefined ) {
44- sourceMapsEnabled = getOptionValue ( '--enable-source-maps' ) ;
45- if ( sourceMapsEnabled ) {
46- const {
47- enableSourceMaps,
48- setPrepareStackTraceCallback
49- } = internalBinding ( 'errors' ) ;
50- const {
51- prepareStackTrace
52- } = require ( 'internal/source_map/prepare_stack_trace' ) ;
53- setPrepareStackTraceCallback ( prepareStackTrace ) ;
54- enableSourceMaps ( ) ;
55- }
45+ setSourceMapsEnabled ( getOptionValue ( '--enable-source-maps' ) ) ;
5646 }
5747 return sourceMapsEnabled ;
5848}
5949
50+ function setSourceMapsEnabled ( val ) {
51+ validateBoolean ( val , 'val' ) ;
52+
53+ const {
54+ setSourceMapsEnabled,
55+ setPrepareStackTraceCallback
56+ } = internalBinding ( 'errors' ) ;
57+ setSourceMapsEnabled ( val ) ;
58+ if ( val ) {
59+ const {
60+ prepareStackTrace
61+ } = require ( 'internal/source_map/prepare_stack_trace' ) ;
62+ setPrepareStackTraceCallback ( prepareStackTrace ) ;
63+ } else if ( sourceMapsEnabled !== undefined ) {
64+ // Reset prepare stack trace callback only when disabling source maps.
65+ const {
66+ prepareStackTrace,
67+ } = require ( 'internal/errors' ) ;
68+ setPrepareStackTraceCallback ( prepareStackTrace ) ;
69+ }
70+
71+ sourceMapsEnabled = val ;
72+ }
73+
6074function maybeCacheSourceMap ( filename , content , cjsModuleInstance ) {
6175 const sourceMapsEnabled = getSourceMapsEnabled ( ) ;
6276 if ( ! ( process . env . NODE_V8_COVERAGE || sourceMapsEnabled ) ) return ;
@@ -231,6 +245,7 @@ function findSourceMap(sourceURL) {
231245module . exports = {
232246 findSourceMap,
233247 getSourceMapsEnabled,
248+ setSourceMapsEnabled,
234249 maybeCacheSourceMap,
235250 sourceMapCacheToObject,
236251} ;
0 commit comments