@@ -93,8 +93,9 @@ function stripTypeScriptTypes(code, options = kEmptyObject) {
9393}
9494
9595/**
96+ * @typedef {'strip-only' | 'transform' } TypeScriptMode
9697 * @typedef {object } TypeScriptOptions
97- * @property {'transform'|'strip-only' } mode Mode.
98+ * @property {TypeScriptMode } mode Mode.
9899 * @property {boolean } sourceMap Whether to generate source maps.
99100 * @property {string|undefined } filename Filename.
100101 */
@@ -122,7 +123,7 @@ function processTypeScriptCode(code, options) {
122123
123124/**
124125 * Get the type enum used for compile cache.
125- * @param {'strip-only'|'transform' } mode Mode of transpilation.
126+ * @param {TypeScriptMode } mode Mode of transpilation.
126127 * @param {boolean } sourceMap Whether source maps are enabled.
127128 * @returns {number }
128129 */
@@ -158,6 +159,11 @@ function stripTypeScriptModuleTypes(source, filename, emitWarning = true) {
158159 // as checking process.env equally involves calling into C++ anyway, and
159160 // the compile cache can be enabled dynamically.
160161 const type = getCachedCodeType ( mode , sourceMap ) ;
162+ // Get a compile cache entry into the native compile cache store,
163+ // keyed by the filename. If the cache can already be loaded on disk,
164+ // cached.transpiled contains the cached string. Otherwise we should do
165+ // the transpilation and save it in the native store later using
166+ // saveCompileCacheEntry().
161167 const cached = ( filename ? getCompileCacheEntry ( source , filename , type ) : undefined ) ;
162168 if ( cached ?. transpiled ) { // TODO(joyeecheung): return Buffer here.
163169 return cached . transpiled ;
@@ -171,6 +177,11 @@ function stripTypeScriptModuleTypes(source, filename, emitWarning = true) {
171177
172178 const transpiled = processTypeScriptCode ( source , options ) ;
173179 if ( cached ) {
180+ // cached.external contains a pointer to the native cache entry.
181+ // The cached object would be unreachable once it's out of scope,
182+ // but the pointer inside cached.external would stay around for reuse until
183+ // environment shutdown or when the cache is manually flushed
184+ // to disk. Unwrap it in JS before passing into C++ since it's faster.
174185 saveCompileCacheEntry ( cached . external , transpiled ) ;
175186 }
176187 return transpiled ;
0 commit comments