Skip to content

Commit

Permalink
resolve file path only when url is for a file path (Fixes fgnass#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed Mar 27, 2022
1 parent f3888ef commit 874335a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/loaders/get-format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { send } from './ipc.mjs';
const require = createRequire(import.meta.url);

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

send({ required: filePath });
send({ required });

try {
return await defaultGetFormat(url, context, defaultGetFormat);
} catch (error) {
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
return require('get-package-type')(filePath).then(format => ({ format }));
return require('get-package-type')(required).then(format => ({ format }));
}
}
6 changes: 3 additions & 3 deletions lib/loaders/load.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { send } from './ipc.mjs';
const require = createRequire(import.meta.url);

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

send({ required: filePath });
send({ required });

try {
return await defaultLoad(url, context, defaultLoad);
} catch (error) {
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
return require('get-package-type')(filePath).then(format => ({ format, source: null }));
return require('get-package-type')(required).then(format => ({ format, source: null }));
}
}
6 changes: 3 additions & 3 deletions lib/loaders/resolve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { send } from './ipc.mjs';

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

if (parentModule) {
send({ required: fileURLToPath(resolved.url) });
}
if (parentModule) send({ required });

return resolved;
}
3 changes: 3 additions & 0 deletions test/fixture/builtin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { join } from 'path';

console.log(join('hello', 'world'));
9 changes: 9 additions & 0 deletions test/spawn/builtin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const tap = require('tap');

const { spawn } = require('../utils');

tap.test('can import builtin modules', t => {
spawn('builtin.mjs', out => {
if (out.match(/^hello[/\\]world/)) return { exit: t.end.bind(t) };
});
});

0 comments on commit 874335a

Please sign in to comment.