Skip to content

Commit cf42a1d

Browse files
committed
fix: handle urls correctly and skip builtins
1 parent 5ab066d commit cf42a1d

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

packages/plugin-pnp/sources/PnpLinker.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,33 @@ export class PnpInstaller implements Installer {
302302
}
303303

304304
if (pnpPath.main.endsWith(`.cjs`)) {
305-
await xfs.writeFilePromise(pnpPath.esmLoader, `import { syncBuiltinESMExports, createRequire } from 'module';
305+
await xfs.writeFilePromise(pnpPath.esmLoader, `import { syncBuiltinESMExports, createRequire, builtinModules } from 'module';
306+
import { fileURLToPath, pathToFileURL, URL } from 'url';
306307
syncBuiltinESMExports();
307308
308309
const pnpapi = createRequire(import.meta.url)('pnpapi');
309310
310-
export async function resolve(specifier, context) {
311+
function isValidURL(str) {
312+
try {
313+
new URL(str);
314+
return true;
315+
} catch {
316+
return false;
317+
}
318+
}
319+
320+
const builtins = new Set([...builtinModules]);
321+
322+
export async function resolve(specifier, context, defaultResolver) {
323+
if (builtins.has(specifier) || isValidURL(specifier)) {
324+
return defaultResolver(specifier, context, defaultResolver);
325+
}
326+
311327
const { parentURL = null } = context;
312328
313329
const resolvedPath = pnpapi.resolveRequest(
314-
specifier.replace('file:///', ''),
315-
parentURL && parentURL.replace('file:///', '')
330+
specifier,
331+
parentURL ? fileURLToPath(parentURL) : undefined
316332
);
317333
318334
if (!resolvedPath) {
@@ -322,7 +338,7 @@ export async function resolve(specifier, context) {
322338
}
323339
324340
return {
325-
url: new URL(\`file:///\${resolvedPath}\`).href,
341+
url: pathToFileURL(resolvedPath).href,
326342
};
327343
}
328344
`);

0 commit comments

Comments
 (0)