@@ -453,17 +453,6 @@ function initializeCJS() {
453
453
// TODO(joyeecheung): deprecate this in favor of a proper hook?
454
454
Module . runMain =
455
455
require ( 'internal/modules/run_main' ) . executeUserEntryPoint ;
456
-
457
- const tsEnabled = getOptionValue ( '--experimental-strip-types' ) ;
458
- if ( tsEnabled ) {
459
- Module . _extensions [ '.cts' ] = loadCTS ;
460
- Module . _extensions [ '.ts' ] = loadTS ;
461
- }
462
- if ( getOptionValue ( '--experimental-require-module' ) ) {
463
- if ( tsEnabled ) {
464
- Module . _extensions [ '.mts' ] = loadMTS ;
465
- }
466
- }
467
456
}
468
457
469
458
// Given a module name, and a list of paths to test, returns the first
@@ -669,31 +658,6 @@ function resolveExports(nmPath, request, conditions) {
669
658
}
670
659
}
671
660
672
- // We don't cache this in case user extends the extensions.
673
- function getDefaultExtensions ( ) {
674
- let extensions = ObjectKeys ( Module . _extensions ) ;
675
- const tsEnabled = getOptionValue ( '--experimental-strip-types' ) ;
676
- if ( tsEnabled ) {
677
- // remove .ts and .cts from the default extensions
678
- // to avoid extensionless require of .ts and .cts files.
679
- extensions = ArrayPrototypeFilter ( extensions , ( ext ) =>
680
- ( ext !== '.ts' || Module . _extensions [ '.ts' ] !== loadTS ) &&
681
- ( ext !== '.cts' || Module . _extensions [ '.cts' ] !== loadCTS ) ,
682
- ) ;
683
- }
684
-
685
- if ( ! getOptionValue ( '--experimental-require-module' ) ) {
686
- return extensions ;
687
- }
688
-
689
- if ( tsEnabled ) {
690
- extensions = ArrayPrototypeFilter ( extensions , ( ext ) =>
691
- ext !== '.mts' || Module . _extensions [ '.mts' ] !== loadMTS ,
692
- ) ;
693
- }
694
- return extensions ;
695
- }
696
-
697
661
/**
698
662
* Get the absolute path to a module.
699
663
* @param {string } request Relative or absolute file path
@@ -785,7 +749,7 @@ Module._findPath = function(request, paths, isMain, conditions = getCjsCondition
785
749
if ( ! filename ) {
786
750
// Try it with each of the extensions
787
751
if ( exts === undefined ) {
788
- exts = getDefaultExtensions ( ) ;
752
+ exts = ObjectKeys ( Module . _extensions ) ;
789
753
}
790
754
filename = tryExtensions ( basePath , exts , isMain ) ;
791
755
}
@@ -794,7 +758,7 @@ Module._findPath = function(request, paths, isMain, conditions = getCjsCondition
794
758
if ( ! filename && rc === 1 ) { // Directory.
795
759
// try it with each of the extensions at "index"
796
760
if ( exts === undefined ) {
797
- exts = getDefaultExtensions ( ) ;
761
+ exts = ObjectKeys ( Module . _extensions ) ;
798
762
}
799
763
filename = tryPackage ( basePath , exts , isMain , request ) ;
800
764
}
@@ -1459,12 +1423,6 @@ Module.prototype.load = function(filename) {
1459
1423
1460
1424
const extension = findLongestRegisteredExtension ( filename ) ;
1461
1425
1462
- if ( getOptionValue ( '--experimental-strip-types' ) ) {
1463
- if ( StringPrototypeEndsWith ( filename , '.mts' ) && ! Module . _extensions [ '.mts' ] ) {
1464
- throw new ERR_REQUIRE_ESM ( filename , true ) ;
1465
- }
1466
- }
1467
-
1468
1426
Module . _extensions [ extension ] ( this , filename ) ;
1469
1427
this . loaded = true ;
1470
1428
@@ -1776,55 +1734,6 @@ function loadSource(mod, filename, formatFromNode) {
1776
1734
return { source : mod [ kModuleSource ] , format : mod [ kFormat ] } ;
1777
1735
}
1778
1736
1779
- /**
1780
- * Built-in handler for `.mts` files.
1781
- * @param {Module } mod CJS module instance
1782
- * @param {string } filename The file path of the module
1783
- */
1784
- function loadMTS ( mod , filename ) {
1785
- const loadResult = loadSource ( mod , filename , 'module-typescript' ) ;
1786
- mod . _compile ( loadResult . source , filename , loadResult . format ) ;
1787
- }
1788
-
1789
- /**
1790
- * Built-in handler for `.cts` files.
1791
- * @param {Module } module CJS module instance
1792
- * @param {string } filename The file path of the module
1793
- */
1794
- function loadCTS ( module , filename ) {
1795
- const loadResult = loadSource ( module , filename , 'commonjs-typescript' ) ;
1796
- module . _compile ( loadResult . source , filename , loadResult . format ) ;
1797
- }
1798
-
1799
- /**
1800
- * Built-in handler for `.ts` files.
1801
- * @param {Module } module CJS module instance
1802
- * @param {string } filename The file path of the module
1803
- */
1804
- function loadTS ( module , filename ) {
1805
- const pkg = packageJsonReader . getNearestParentPackageJSON ( filename ) ;
1806
- const typeFromPjson = pkg ?. data . type ;
1807
-
1808
- let format ;
1809
- if ( typeFromPjson === 'module' ) {
1810
- format = 'module-typescript' ;
1811
- } else if ( typeFromPjson === 'commonjs' ) {
1812
- format = 'commonjs-typescript' ;
1813
- } else {
1814
- format = 'typescript' ;
1815
- }
1816
- const loadResult = loadSource ( module , filename , format ) ;
1817
-
1818
- // Function require shouldn't be used in ES modules when require(esm) is disabled.
1819
- if ( typeFromPjson === 'module' && ! getOptionValue ( '--experimental-require-module' ) ) {
1820
- const err = getRequireESMError ( module , pkg , loadResult . source , filename ) ;
1821
- throw err ;
1822
- }
1823
-
1824
- module [ kFormat ] = loadResult . format ;
1825
- module . _compile ( loadResult . source , filename , loadResult . format ) ;
1826
- } ;
1827
-
1828
1737
function reconstructErrorStack ( err , parentPath , parentSource ) {
1829
1738
const errLine = StringPrototypeSplit (
1830
1739
StringPrototypeSlice ( err . stack , StringPrototypeIndexOf (
@@ -1878,6 +1787,7 @@ function getRequireESMError(mod, pkg, content, filename) {
1878
1787
*/
1879
1788
Module . _extensions [ '.js' ] = function ( module , filename ) {
1880
1789
let format , pkg ;
1790
+ const tsEnabled = getOptionValue ( '--experimental-strip-types' ) ;
1881
1791
if ( StringPrototypeEndsWith ( filename , '.cjs' ) ) {
1882
1792
format = 'commonjs' ;
1883
1793
} else if ( StringPrototypeEndsWith ( filename , '.mjs' ) ) {
@@ -1888,10 +1798,25 @@ Module._extensions['.js'] = function(module, filename) {
1888
1798
if ( typeFromPjson === 'module' || typeFromPjson === 'commonjs' || ! typeFromPjson ) {
1889
1799
format = typeFromPjson ;
1890
1800
}
1801
+ } else if ( StringPrototypeEndsWith ( filename , '.mts' ) && tsEnabled ) {
1802
+ format = 'module-typescript' ;
1803
+ } else if ( StringPrototypeEndsWith ( filename , '.cts' ) && tsEnabled ) {
1804
+ format = 'commonjs-typescript' ;
1805
+ } else if ( StringPrototypeEndsWith ( filename , '.ts' ) && tsEnabled ) {
1806
+ pkg = packageJsonReader . getNearestParentPackageJSON ( filename ) ;
1807
+ const typeFromPjson = pkg ?. data . type ;
1808
+ if ( typeFromPjson === 'module' ) {
1809
+ format = 'module-typescript' ;
1810
+ } else if ( typeFromPjson === 'commonjs' ) {
1811
+ format = 'commonjs-typescript' ;
1812
+ } else {
1813
+ format = 'typescript' ;
1814
+ }
1891
1815
}
1892
1816
const { source, format : loadedFormat } = loadSource ( module , filename , format ) ;
1893
1817
// Function require shouldn't be used in ES modules when require(esm) is disabled.
1894
- if ( loadedFormat === 'module' && ! getOptionValue ( '--experimental-require-module' ) ) {
1818
+ if ( ( loadedFormat === 'module' || loadedFormat === 'module-typescript' ) &&
1819
+ ! getOptionValue ( '--experimental-require-module' ) ) {
1895
1820
const err = getRequireESMError ( module , pkg , source , filename ) ;
1896
1821
throw err ;
1897
1822
}
0 commit comments