Skip to content

Commit c643491

Browse files
committed
esm: skip file: URL conversion to path when possible
1 parent de6c0e6 commit c643491

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

lib/internal/modules/esm/get_format.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const {
44
ObjectPrototypeHasOwnProperty,
55
PromisePrototypeThen,
66
PromiseResolve,
7+
StringPrototypeCharCodeAt,
78
StringPrototypeSlice,
89
} = primordials;
9-
const { basename, extname, relative } = require('path');
10+
const { basename, relative } = require('path');
1011
const { getOptionValue } = require('internal/options');
1112
const {
1213
extensionFormatMap,
@@ -41,15 +42,37 @@ function getDataProtocolModuleFormat(parsed) {
4142
return mimeToFormat(mime);
4243
}
4344

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+
4468
/**
4569
* @param {URL} url
4670
* @param {{parentURL: string}} context
4771
* @param {boolean} ignoreErrors
4872
* @returns {string}
4973
*/
5074
function getFileProtocolModuleFormat(url, context, ignoreErrors) {
51-
const filepath = fileURLToPath(url);
52-
const ext = extname(filepath);
75+
const ext = extname(url);
5376
if (ext === '.js') {
5477
return getPackageType(url) === 'module' ? 'module' : 'commonjs';
5578
}
@@ -59,6 +82,7 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) {
5982

6083
// Explicit undefined return indicates load hook should rerun format check
6184
if (ignoreErrors) { return undefined; }
85+
const filepath = fileURLToPath(url);
6286
let suggestion = '';
6387
if (getPackageType(url) === 'module' && ext === '') {
6488
const config = getPackageScopeConfig(url);

lib/internal/modules/esm/translators.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ const {
3535
Module: CJSModule,
3636
cjsParseCache
3737
} = require('internal/modules/cjs/loader');
38-
const internalURLModule = require('internal/url');
39-
const { fileURLToPath, URL } = require('url');
38+
const { fileURLToPath, URL } = require('internal/url');
4039
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
4140
debug = fn;
4241
});
@@ -147,9 +146,7 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
147146
isMain) {
148147
debug(`Translating CJSModule ${url}`);
149148

150-
let filename = internalURLModule.fileURLToPath(new URL(url));
151-
if (isWindows)
152-
filename = StringPrototypeReplaceAll(filename, '/', '\\');
149+
const filename = fileURLToPath(new URL(url));
153150

154151
if (!cjsParse) await initCJSParse();
155152
const { module, exportNames } = cjsPreparseModuleExports(filename);

0 commit comments

Comments
 (0)