4
4
ObjectPrototypeHasOwnProperty,
5
5
PromisePrototypeThen,
6
6
PromiseResolve,
7
+ StringPrototypeCharCodeAt,
7
8
StringPrototypeSlice,
8
9
} = primordials ;
9
- const { basename, extname , relative } = require ( 'path' ) ;
10
+ const { basename, relative } = require ( 'path' ) ;
10
11
const { getOptionValue } = require ( 'internal/options' ) ;
11
12
const {
12
13
extensionFormatMap,
@@ -41,15 +42,37 @@ function getDataProtocolModuleFormat(parsed) {
41
42
return mimeToFormat ( mime ) ;
42
43
}
43
44
45
+ const DOT_CODE = 46 ;
46
+ const SLASH_CODE = 47 ;
47
+
48
+ /**
49
+ * Returns the file extension from a file: URL. Should give similar result than
50
+ * require('node:path').extname(require('node:url').fileURLToPath(url)).
51
+ * @param {URL } url A file: URL.
52
+ * @returns {string }
53
+ */
54
+ function extname ( url ) {
55
+ const { pathname } = url ;
56
+ for ( let i = pathname . length - 1 ; i > 0 ; i -- ) {
57
+ switch ( StringPrototypeCharCodeAt ( pathname , i ) ) {
58
+ case SLASH_CODE :
59
+ return '' ;
60
+
61
+ case DOT_CODE :
62
+ return StringPrototypeSlice ( pathname , i ) ;
63
+ }
64
+ }
65
+ return '' ;
66
+ }
67
+
44
68
/**
45
69
* @param {URL } url
46
70
* @param {{parentURL: string} } context
47
71
* @param {boolean } ignoreErrors
48
72
* @returns {string }
49
73
*/
50
74
function getFileProtocolModuleFormat ( url , context , ignoreErrors ) {
51
- const filepath = fileURLToPath ( url ) ;
52
- const ext = extname ( filepath ) ;
75
+ const ext = extname ( url ) ;
53
76
if ( ext === '.js' ) {
54
77
return getPackageType ( url ) === 'module' ? 'module' : 'commonjs' ;
55
78
}
@@ -59,6 +82,7 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) {
59
82
60
83
// Explicit undefined return indicates load hook should rerun format check
61
84
if ( ignoreErrors ) { return undefined ; }
85
+ const filepath = fileURLToPath ( url ) ;
62
86
let suggestion = '' ;
63
87
if ( getPackageType ( url ) === 'module' && ext === '' ) {
64
88
const config = getPackageScopeConfig ( url ) ;
0 commit comments