Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 2 additions & 29 deletions lib/internal/modules/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const {
ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING,
ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX,
} = require('internal/errors').codes;
const { getOptionValue } = require('internal/options');
const assert = require('internal/assert');
const { Buffer } = require('buffer');
const {
getCompileCacheEntry,
saveCompileCacheEntry,
Expand Down Expand Up @@ -117,26 +115,18 @@ function stripTypeScriptTypes(code, options = kEmptyObject) {
}

/**
* @typedef {'strip-only' | 'transform'} TypeScriptMode
* @typedef {object} TypeScriptOptions
* @property {TypeScriptMode} mode Mode.
* @property {boolean} sourceMap Whether to generate source maps.
* @property {string|undefined} filename Filename.
*/

/**
* Processes TypeScript code by stripping types or transforming.
* Handles source maps if needed.
* Processes TypeScript code by stripping types.
* @param {string} code TypeScript code to process.
* @param {TypeScriptOptions} options The configuration object.
* @returns {string} The processed code.
*/
function processTypeScriptCode(code, options) {
const { code: transformedCode, map } = parseTypeScript(code, options);

if (map) {
return addSourceMap(transformedCode, map);
}
const { code: transformedCode } = parseTypeScript(code, options);

if (options.filename) {
return `${transformedCode}\n\n//# sourceURL=${options.filename}`;
Expand All @@ -163,8 +153,6 @@ function stripTypeScriptModuleTypes(source, filename) {
if (isUnderNodeModules(filename)) {
throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename);
}
const sourceMap = getOptionValue('--enable-source-maps');

// Get a compile cache entry into the native compile cache store,
// keyed by the filename. If the cache can already be loaded on disk,
// cached.transpiled contains the cached string. Otherwise we should do
Expand All @@ -177,7 +165,6 @@ function stripTypeScriptModuleTypes(source, filename) {

const options = {
mode: 'strip-only',
sourceMap,
filename,
};

Expand All @@ -193,20 +180,6 @@ function stripTypeScriptModuleTypes(source, filename) {
return transpiled;
}

/**
*
* @param {string} code The compiled code.
* @param {string} sourceMap The source map.
* @returns {string} The code with the source map attached.
*/
function addSourceMap(code, sourceMap) {
// The base64 encoding should be https://datatracker.ietf.org/doc/html/rfc4648#section-4,
// not base64url https://datatracker.ietf.org/doc/html/rfc4648#section-5. See data url
// spec https://tools.ietf.org/html/rfc2397#section-2.
const base64SourceMap = Buffer.from(sourceMap).toString('base64');
return `${code}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`;
}

module.exports = {
stripTypeScriptModuleTypes,
stripTypeScriptTypes,
Expand Down
Loading