Skip to content

Commit 8cf1ecc

Browse files
committed
resolve file path only when url is for a file path (Fixes #281)
1 parent f3888ef commit 8cf1ecc

5 files changed

Lines changed: 21 additions & 9 deletions

File tree

lib/loaders/get-format.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { send } from './ipc.mjs';
55
const require = createRequire(import.meta.url);
66

77
export async function getFormat(url, context, defaultGetFormat) {
8-
const filePath = fileURLToPath(url);
8+
const required = url.startsWith('file://') ? fileURLToPath(url) : url;
99

10-
send({ required: filePath });
10+
send({ required });
1111

1212
try {
1313
return await defaultGetFormat(url, context, defaultGetFormat);
1414
} catch (error) {
1515
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
16-
return require('get-package-type')(filePath).then(format => ({ format }));
16+
return require('get-package-type')(required).then(format => ({ format }));
1717
}
1818
}

lib/loaders/load.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { send } from './ipc.mjs';
55
const require = createRequire(import.meta.url);
66

77
export async function load(url, context, defaultLoad) {
8-
const filePath = fileURLToPath(url);
8+
const required = url.startsWith('file://') ? fileURLToPath(url) : url;
99

10-
send({ required: filePath });
10+
send({ required });
1111

1212
try {
1313
return await defaultLoad(url, context, defaultLoad);
1414
} catch (error) {
1515
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
16-
return require('get-package-type')(filePath).then(format => ({ format, source: null }));
16+
return require('get-package-type')(required).then(format => ({ format, source: null }));
1717
}
1818
}

lib/loaders/resolve.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { send } from './ipc.mjs';
33

44
export function resolve(specifier, parentModule, defaultResolve) {
55
const resolved = defaultResolve(specifier, parentModule);
6+
const { url } = resolved;
7+
const required = url.startsWith('file://') ? fileURLToPath(url) : url;
68

7-
if (parentModule) {
8-
send({ required: fileURLToPath(resolved.url) });
9-
}
9+
if (parentModule) send({ required });
1010

1111
return resolved;
1212
}

test/fixture/builtin.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { join } from 'path';
2+
3+
console.log(join('hello', 'world'));

test/spawn/builtin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const tap = require('tap');
2+
3+
const { spawn } = require('../utils');
4+
5+
tap.test('can import builtin modules', t => {
6+
spawn('builtin.mjs', out => {
7+
if (out.match(/^hello[/\\]world/)) return { exit: t.end.bind(t) };
8+
});
9+
});

0 commit comments

Comments
 (0)