| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0794861 commit 6575b76
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -270,6 +270,105 @@ changes: | |||
| 270 | 270 | Register a module that exports [hooks][] that customize Node.js module | |
| 271 | 271 | resolution and loading behavior. See [Customization hooks][]. | |
| 272 | 272 | ||
| 273 | + ## `module.stripTypeScriptTypes(code[, options])` | ||
| 274 | + | ||
| 275 | + <!-- YAML | ||
| 276 | + added: REPLACEME | ||
| 277 | + --> | ||
| 278 | + | ||
| 279 | + > Stability: 1.0 - Early development | ||
| 280 | + | ||
| 281 | + * `code` {string} The code to strip type annotations from. | ||
| 282 | + * `options` {Object} | ||
| 283 | + * `mode` {string} **Default:** `'strip'`. Possible values are: | ||
| 284 | + * `'strip'` Only strip type annotations without performing the transformation of TypeScript features. | ||
| 285 | + * `'transform'` Strip type annotations and transform TypeScript features to JavaScript. | ||
| 286 | + * `sourceMap` {boolean} **Default:** `false`. Only when `mode` is `'transform'`, if `true`, a source map | ||
| 287 | + will be generated for the transformed code. | ||
| 288 | + * `sourceUrl` {string} Specifies the source url used in the source map. | ||
| 289 | + * Returns: {string} The code with type annotations stripped. | ||
| 290 | + `module.stripTypeScriptTypes()` removes type annotations from TypeScript code. It | ||
| 291 | + can be used to strip type annotations from TypeScript code before running it | ||
| 292 | + with `vm.runInContext()` or `vm.compileFunction()`. | ||
| 293 | + By default, it will throw an error if the code contains TypeScript features | ||
| 294 | + that require transformation such as `Enums`, | ||
| 295 | + see [type-stripping][] for more information. | ||
| 296 | + When mode is `'transform'`, it also transforms TypeScript features to JavaScript, | ||
| 297 | + see [transform TypeScript features][] for more information. | ||
| 298 | + When mode is `'strip'`, source maps are not generated, because locations are preserved. | ||
| 299 | + If `sourceMap` is provided, when mode is `'strip'`, an error will be thrown. | ||
| 300 | + | ||
| 301 | + _WARNING_: The output of this function should not be considered stable across Node.js versions, | ||
| 302 | + due to changes in the TypeScript parser. | ||
| 303 | + | ||
| 304 | + ```mjs | ||
| 305 | + import { stripTypeScriptTypes } from 'node:module'; | ||
| 306 | + const code = 'const a: number = 1;'; | ||
| 307 | + const strippedCode = stripTypeScriptTypes(code); | ||
| 308 | + console.log(strippedCode); | ||
| 309 | + // Prints: const a = 1; | ||
| 310 | + ``` | ||
| 311 | + | ||
| 312 | + ```cjs | ||
| 313 | + const { stripTypeScriptTypes } = require('node:module'); | ||
| 314 | + const code = 'const a: number = 1;'; | ||
| 315 | + const strippedCode = stripTypeScriptTypes(code); | ||
| 316 | + console.log(strippedCode); | ||
| 317 | + // Prints: const a = 1; | ||
| 318 | + ``` | ||
| 319 | + | ||
| 320 | + If `sourceUrl` is provided, it will be used appended as a comment at the end of the output: | ||
| 321 | + | ||
| 322 | + ```mjs | ||
| 323 | + import { stripTypeScriptTypes } from 'node:module'; | ||
| 324 | + const code = 'const a: number = 1;'; | ||
| 325 | + const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' }); | ||
| 326 | + console.log(strippedCode); | ||
| 327 | + // Prints: const a = 1\n\n//# sourceURL=source.ts; | ||
| 328 | + ``` | ||
| 329 | + | ||
| 330 | + ```cjs | ||
| 331 | + const { stripTypeScriptTypes } = require('node:module'); | ||
| 332 | + const code = 'const a: number = 1;'; | ||
| 333 | + const strippedCode = stripTypeScriptTypes(code, { mode: 'strip', sourceUrl: 'source.ts' }); | ||
| 334 | + console.log(strippedCode); | ||
| 335 | + // Prints: const a = 1\n\n//# sourceURL=source.ts; | ||
| 336 | + ``` | ||
| 337 | + | ||
| 338 | + When `mode` is `'transform'`, the code is transformed to JavaScript: | ||
| 339 | + | ||
| 340 | + ```mjs | ||
| 341 | + import { stripTypeScriptTypes } from 'node:module'; | ||
| 342 | + const code = ` | ||
| 343 | + namespace MathUtil { | ||
| 344 | + export const add = (a: number, b: number) => a + b; | ||
| 345 | + }`; | ||
| 346 | + const strippedCode = stripTypeScriptTypes(code, { mode: 'transform', sourceMap: true }); | ||
| 347 | + console.log(strippedCode); | ||
| 348 | + // Prints: | ||
| 349 | + // var MathUtil; | ||
| 350 | + // (function(MathUtil) { | ||
| 351 | + // MathUtil.add = (a, b)=>a + b; | ||
| 352 | + // })(MathUtil || (MathUtil = {})); | ||
| 353 | + // # sourceMappingURL=data:application/json;base64, ... | ||
| 354 | + ``` | ||
| 355 | + | ||
| 356 | + ```cjs | ||
| 357 | + const { stripTypeScriptTypes } = require('node:module'); | ||
| 358 | + const code = ` | ||
| 359 | + namespace MathUtil { | ||
| 360 | + export const add = (a: number, b: number) => a + b; | ||
| 361 | + }`; | ||
| 362 | + const strippedCode = stripTypeScriptTypes(code, { mode: 'transform', sourceMap: true }); | ||
| 363 | + console.log(strippedCode); | ||
| 364 | + // Prints: | ||
| 365 | + // var MathUtil; | ||
| 366 | + // (function(MathUtil) { | ||
| 367 | + // MathUtil.add = (a, b)=>a + b; | ||
| 368 | + // })(MathUtil || (MathUtil = {})); | ||
| 369 | + // # sourceMappingURL=data:application/json;base64, ... | ||
| 370 | + ``` | ||
| 371 | + | ||
| 273 | 372 | ### `module.syncBuiltinESMExports()` | |
| 274 | 373 | ||
| 275 | 374 | <!-- YAML | |
@@ -1251,3 +1350,5 @@ returned object contains the following keys: | |||
| 1251 | 1350 | [realm]: https://tc39.es/ecma262/#realm | |
| 1252 | 1351 | [source map include directives]: https://sourcemaps.info/spec.html#h.lmz475t4mvbx | |
| 1253 | 1352 | [transferrable objects]: worker_threads.md#portpostmessagevalue-transferlist | |
| 1353 | + [transform TypeScript features]: typescript.md#typescript-features | ||
| 1354 | + [type-stripping]: typescript.md#type-stripping | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,8 +14,8 @@ const { | |||
| 14 | 14 | markBootstrapComplete, | |
| 15 | 15 | } = require('internal/process/pre_execution'); | |
| 16 | 16 | const { evalModuleEntryPoint, evalScript } = require('internal/process/execution'); | |
| 17 | - const { addBuiltinLibsToObject, stripTypeScriptTypes } = require('internal/modules/helpers'); | ||
| 18 | - | ||
| 17 | + const { addBuiltinLibsToObject } = require('internal/modules/helpers'); | ||
| 18 | + const { stripTypeScriptModuleTypes } = require('internal/modules/typescript'); | ||
| 19 | 19 | const { getOptionValue } = require('internal/options'); | |
| 20 | 20 | ||
| 21 | 21 | prepareMainThreadExecution(); | |
@@ -24,7 +24,7 @@ markBootstrapComplete(); | |||
| 24 | 24 | ||
| 25 | 25 | const code = getOptionValue('--eval'); | |
| 26 | 26 | const source = getOptionValue('--experimental-strip-types') ? | |
| 27 | - stripTypeScriptTypes(code) : | ||
| 27 | + stripTypeScriptModuleTypes(code) : | ||
| 28 | 28 | code; | |
| 29 | 29 | ||
| 30 | 30 | const print = getOptionValue('--print'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -153,8 +153,8 @@ const { | |||
| 153 | 153 | setHasStartedUserCJSExecution, | |
| 154 | 154 | stripBOM, | |
| 155 | 155 | toRealPath, | |
| 156 | - stripTypeScriptTypes, | ||
| 157 | 156 | } = require('internal/modules/helpers'); | |
| 157 | + const { stripTypeScriptModuleTypes } = require('internal/modules/typescript'); | ||
| 158 | 158 | const packageJsonReader = require('internal/modules/package_json_reader'); | |
| 159 | 159 | const { getOptionValue, getEmbedderOptions } = require('internal/options'); | |
| 160 | 160 | const shouldReportRequiredModules = getLazy(() => process.env.WATCH_REPORT_DEPENDENCIES); | |
@@ -1347,7 +1347,7 @@ let emittedRequireModuleWarning = false; | |||
| 1347 | 1347 | function loadESMFromCJS(mod, filename) { | |
| 1348 | 1348 | let source = getMaybeCachedSource(mod, filename); | |
| 1349 | 1349 | if (getOptionValue('--experimental-strip-types') && path.extname(filename) === '.mts') { | |
| 1350 | - source = stripTypeScriptTypes(source, filename); | ||
| 1350 | + source = stripTypeScriptModuleTypes(source, filename); | ||
| 1351 | 1351 | } | |
| 1352 | 1352 | const cascadedLoader = require('internal/modules/esm/loader').getOrInitializeCascadedLoader(); | |
| 1353 | 1353 | const isMain = mod[kIsMainSymbol]; | |
@@ -1584,7 +1584,7 @@ function getMaybeCachedSource(mod, filename) { | |||
| 1584 | 1584 | ||
| 1585 | 1585 | function loadCTS(module, filename) { | |
| 1586 | 1586 | const source = getMaybeCachedSource(module, filename); | |
| 1587 | - const code = stripTypeScriptTypes(source, filename); | ||
| 1587 | + const code = stripTypeScriptModuleTypes(source, filename); | ||
| 1588 | 1588 | module._compile(code, filename, 'commonjs'); | |
| 1589 | 1589 | } | |
| 1590 | 1590 | ||
@@ -1596,7 +1596,7 @@ function loadCTS(module, filename) { | |||
| 1596 | 1596 | function loadTS(module, filename) { | |
| 1597 | 1597 | // If already analyzed the source, then it will be cached. | |
| 1598 | 1598 | const source = getMaybeCachedSource(module, filename); | |
| 1599 | - const content = stripTypeScriptTypes(source, filename); | ||
| 1599 | + const content = stripTypeScriptModuleTypes(source, filename); | ||
| 1600 | 1600 | let format; | |
| 1601 | 1601 | const pkg = packageJsonReader.getNearestParentPackageJSON(filename); | |
| 1602 | 1602 | // Function require shouldn't be used in ES modules. | |
@@ -1616,7 +1616,7 @@ function loadTS(module, filename) { | |||
| 1616 | 1616 | if (Module._cache[parentPath]) { | |
| 1617 | 1617 | let parentSource; | |
| 1618 | 1618 | try { | |
| 1619 | - parentSource = stripTypeScriptTypes(fs.readFileSync(parentPath, 'utf8'), parentPath); | ||
| 1619 | + parentSource = stripTypeScriptModuleTypes(fs.readFileSync(parentPath, 'utf8'), parentPath); | ||
| 1620 | 1620 | } catch { | |
| 1621 | 1621 | // Continue regardless of error. | |
| 1622 | 1622 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -164,9 +164,10 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE | |||
| 164 | 164 | // Since experimental-strip-types depends on detect-module, we always return null | |
| 165 | 165 | // if source is undefined. | |
| 166 | 166 | if (!source) { return null; } | |
| 167 | - const { stripTypeScriptTypes, stringify } = require('internal/modules/helpers'); | ||
| 167 | + const { stringify } = require('internal/modules/helpers'); | ||
| 168 | + const { stripTypeScriptModuleTypes } = require('internal/modules/typescript'); | ||
| 168 | 169 | const stringifiedSource = stringify(source); | |
| 169 | - const parsedSource = stripTypeScriptTypes(stringifiedSource, fileURLToPath(url)); | ||
| 170 | + const parsedSource = stripTypeScriptModuleTypes(stringifiedSource, fileURLToPath(url)); | ||
| 170 | 171 | const detectedFormat = detectModuleFormat(parsedSource, url); | |
| 171 | 172 | const format = `${detectedFormat}-typescript`; | |
| 172 | 173 | if (format === 'module-typescript' && foundPackageJson) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,10 +30,10 @@ const { | |||
| 30 | 30 | assertBufferSource, | |
| 31 | 31 | loadBuiltinModule, | |
| 32 | 32 | stringify, | |
| 33 | - stripTypeScriptTypes, | ||
| 34 | 33 | stripBOM, | |
| 35 | 34 | urlToFilename, | |
| 36 | 35 | } = require('internal/modules/helpers'); | |
| 36 | + const { stripTypeScriptModuleTypes } = require('internal/modules/typescript'); | ||
| 37 | 37 | const { | |
| 38 | 38 | kIsCachedByESMLoader, | |
| 39 | 39 | Module: CJSModule, | |
@@ -244,7 +244,7 @@ translators.set('require-commonjs', (url, source, isMain) => { | |||
| 244 | 244 | translators.set('require-commonjs-typescript', (url, source, isMain) => { | |
| 245 | 245 | emitExperimentalWarning('Type Stripping'); | |
| 246 | 246 | assert(cjsParse); | |
| 247 | - const code = stripTypeScriptTypes(stringify(source), url); | ||
| 247 | + const code = stripTypeScriptModuleTypes(stringify(source), url); | ||
| 248 | 248 | return createCJSModuleWrap(url, code); | |
| 249 | 249 | }); | |
| 250 | 250 | ||
@@ -459,7 +459,7 @@ translators.set('wasm', async function(url, source) { | |||
| 459 | 459 | translators.set('commonjs-typescript', function(url, source) { | |
| 460 | 460 | emitExperimentalWarning('Type Stripping'); | |
| 461 | 461 | assertBufferSource(source, true, 'load'); | |
| 462 | - const code = stripTypeScriptTypes(stringify(source), url); | ||
| 462 | + const code = stripTypeScriptModuleTypes(stringify(source), url); | ||
| 463 | 463 | debug(`Translating TypeScript ${url}`); | |
| 464 | 464 | return FunctionPrototypeCall(translators.get('commonjs'), this, url, code, false); | |
| 465 | 465 | }); | |
@@ -468,7 +468,7 @@ translators.set('commonjs-typescript', function(url, source) { | |||
| 468 | 468 | translators.set('module-typescript', function(url, source) { | |
| 469 | 469 | emitExperimentalWarning('Type Stripping'); | |
| 470 | 470 | assertBufferSource(source, true, 'load'); | |
| 471 | - const code = stripTypeScriptTypes(stringify(source), url); | ||
| 471 | + const code = stripTypeScriptModuleTypes(stringify(source), url); | ||
| 472 | 472 | debug(`Translating TypeScript ${url}`); | |
| 473 | 473 | return FunctionPrototypeCall(translators.get('module'), this, url, code, false); | |
| 474 | 474 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,8 +15,6 @@ const { | |||
| 15 | 15 | const { | |
| 16 | 16 | ERR_INVALID_ARG_TYPE, | |
| 17 | 17 | ERR_INVALID_RETURN_PROPERTY_VALUE, | |
| 18 | - ERR_INVALID_TYPESCRIPT_SYNTAX, | ||
| 19 | - ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING, | ||
| 20 | 18 | } = require('internal/errors').codes; | |
| 21 | 19 | const { BuiltinModule } = require('internal/bootstrap/realm'); | |
| 22 | 20 | ||
@@ -27,9 +25,8 @@ const path = require('path'); | |||
| 27 | 25 | const { pathToFileURL, fileURLToPath } = require('internal/url'); | |
| 28 | 26 | const assert = require('internal/assert'); | |
| 29 | 27 | ||
| 30 | - const { Buffer } = require('buffer'); | ||
| 31 | 28 | const { getOptionValue } = require('internal/options'); | |
| 32 | - const { assertTypeScript, setOwnProperty, getLazy, isUnderNodeModules } = require('internal/util'); | ||
| 29 | + const { setOwnProperty, getLazy } = require('internal/util'); | ||
| 33 | 30 | const { inspect } = require('internal/util/inspect'); | |
| 34 | 31 | ||
| 35 | 32 | const lazyTmpdir = getLazy(() => require('os').tmpdir()); | |
@@ -314,75 +311,6 @@ function getBuiltinModule(id) { | |||
| 314 | 311 | return normalizedId ? require(normalizedId) : undefined; | |
| 315 | 312 | } | |
| 316 | 313 | ||
| 317 | - /** | ||
| 318 | - * The TypeScript parsing mode, either 'strip-only' or 'transform'. | ||
| 319 | - * @type {string} | ||
| 320 | - */ | ||
| 321 | - const getTypeScriptParsingMode = getLazy(() => | ||
| 322 | - (getOptionValue('--experimental-transform-types') ? 'transform' : 'strip-only'), | ||
| 323 | - ); | ||
| 324 | - | ||
| 325 | - /** | ||
| 326 | - * Load the TypeScript parser. | ||
| 327 | - * and returns an object with a `code` property. | ||
| 328 | - * @returns {Function} The TypeScript parser function. | ||
| 329 | - */ | ||
| 330 | - const loadTypeScriptParser = getLazy(() => { | ||
| 331 | - assertTypeScript(); | ||
| 332 | - const amaro = require('internal/deps/amaro/dist/index'); | ||
| 333 | - return amaro.transformSync; | ||
| 334 | - }); | ||
| 335 | - | ||
| 336 | - /** | ||
| 337 | - * | ||
| 338 | - * @param {string} source the source code | ||
| 339 | - * @param {object} options the options to pass to the parser | ||
| 340 | - * @returns {TransformOutput} an object with a `code` property. | ||
| 341 | - */ | ||
| 342 | - function parseTypeScript(source, options) { | ||
| 343 | - const parse = loadTypeScriptParser(); | ||
| 344 | - try { | ||
| 345 | - return parse(source, options); | ||
| 346 | - } catch (error) { | ||
| 347 | - throw new ERR_INVALID_TYPESCRIPT_SYNTAX(error); | ||
| 348 | - } | ||
| 349 | - } | ||
| 350 | - | ||
| 351 | - /** | ||
| 352 | - * @typedef {object} TransformOutput | ||
| 353 | - * @property {string} code The compiled code. | ||
| 354 | - * @property {string} [map] The source maps (optional). | ||
| 355 | - * | ||
| 356 | - * Performs type-stripping to TypeScript source code. | ||
| 357 | - * @param {string} source TypeScript code to parse. | ||
| 358 | - * @param {string} filename The filename of the source code. | ||
| 359 | - * @returns {TransformOutput} The stripped TypeScript code. | ||
| 360 | - */ | ||
| 361 | - function stripTypeScriptTypes(source, filename) { | ||
| 362 | - if (isUnderNodeModules(filename)) { | ||
| 363 | - throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING(filename); | ||
| 364 | - } | ||
| 365 | - assert(typeof source === 'string'); | ||
| 366 | - const options = { | ||
| 367 | - __proto__: null, | ||
| 368 | - mode: getTypeScriptParsingMode(), | ||
| 369 | - sourceMap: getOptionValue('--enable-source-maps'), | ||
| 370 | - filename, | ||
| 371 | - }; | ||
| 372 | - const { code, map } = parseTypeScript(source, options); | ||
| 373 | - if (map) { | ||
| 374 | - // TODO(@marco-ippolito) When Buffer.transcode supports utf8 to | ||
| 375 | - // base64 transformation, we should change this line. | ||
| 376 | - const base64SourceMap = Buffer.from(map).toString('base64'); | ||
| 377 | - return `${code}\n\n//# sourceMappingURL=data:application/json;base64,${base64SourceMap}`; | ||
| 378 | - } | ||
| 379 | - // Source map is not necessary in strip-only mode. However, to map the source | ||
| 380 | - // file in debuggers to the original TypeScript source, add a sourceURL magic | ||
| 381 | - // comment to hint that it is a generated source. | ||
| 382 | - return `${code}\n\n//# sourceURL=${filename}`; | ||
| 383 | - } | ||
| 384 | - | ||
| 385 | - | ||
| 386 | 314 | /** | |
| 387 | 315 | * Enable on-disk compiled cache for all user modules being complied in the current Node.js instance | |
| 388 | 316 | * after this method is called. | |
@@ -485,7 +413,6 @@ module.exports = { | |||
| 485 | 413 | loadBuiltinModule, | |
| 486 | 414 | makeRequireFunction, | |
| 487 | 415 | normalizeReferrerURL, | |
| 488 | - stripTypeScriptTypes, | ||
| 489 | 416 | stringify, | |
| 490 | 417 | stripBOM, | |
| 491 | 418 | toRealPath, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments