* Performs type-stripping to TypeScript source code internally. * It is used by internal loaders. * @param {string} source TypeScript code to parse. * @param {string} filename The filename of the source code. * @param {string} [sourceURL] The source URL of the source code. If not specified, use
(source, filename, sourceURL)
| 150 | * @returns {TransformOutput} The stripped TypeScript code. |
| 151 | */ |
| 152 | function stripTypeScriptModuleTypes(source, filename, sourceURL) { |
| 153 | assert(typeof source === 'string'); |
| 154 | if (isUnderNodeModules(filename)) { |
| 155 | throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename); |
| 156 | } |
| 157 | // Get a compile cache entry into the native compile cache store, |
| 158 | // keyed by the filename. If the cache can already be loaded on disk, |
| 159 | // cached.transpiled contains the cached string. Otherwise we should do |
| 160 | // the transpilation and save it in the native store later using |
| 161 | // saveCompileCacheEntry(). |
| 162 | const cached = (filename ? getCompileCacheEntry(source, filename, kStrippedTypeScript) : undefined); |
| 163 | if (cached?.transpiled) { // TODO(joyeecheung): return Buffer here. |
| 164 | return cached.transpiled; |
| 165 | } |
| 166 | |
| 167 | const options = { |
| 168 | mode: 'strip-only', |
| 169 | filename: sourceURL ?? filename, |
| 170 | }; |
| 171 | |
| 172 | const transpiled = processTypeScriptCode(source, options); |
| 173 | if (cached) { |
| 174 | // cached.external contains a pointer to the native cache entry. |
| 175 | // The cached object would be unreachable once it's out of scope, |
| 176 | // but the pointer inside cached.external would stay around for reuse until |
| 177 | // environment shutdown or when the cache is manually flushed |
| 178 | // to disk. Unwrap it in JS before passing into C++ since it's faster. |
| 179 | saveCompileCacheEntry(cached.external, transpiled); |
| 180 | } |
| 181 | return transpiled; |
| 182 | } |
| 183 | |
| 184 | module.exports = { |
| 185 | stripTypeScriptModuleTypes, |
no test coverage detected
searching dependent graphs…