Skip to content

Commit

Permalink
fix: handle urls correctly and skip builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Dec 5, 2020
1 parent c518e45 commit aa834f8
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions packages/plugin-pnp/sources/PnpLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,33 @@ export class PnpInstaller implements Installer {
}

if (pnpPath.main.endsWith(`.cjs`)) {
await xfs.writeFilePromise(pnpPath.esmLoader, `import { syncBuiltinESMExports, createRequire } from 'module';
await xfs.writeFilePromise(pnpPath.esmLoader, `import { syncBuiltinESMExports, createRequire, builtinModules } from 'module';
import { fileURLToPath, pathToFileURL, URL } from 'url';
syncBuiltinESMExports();
const pnpapi = createRequire(import.meta.url)('pnpapi');
export async function resolve(specifier, context) {
function isValidURL(str) {
try {
new URL(str);
return true;
} catch {
return false;
}
}
const builtins = new Set([...builtinModules]);
export async function resolve(specifier, context, defaultResolver) {
if (builtins.has(specifier) || isValidURL(specifier)) {
return defaultResolver(specifier, context, defaultResolver);
}
const { parentURL = null } = context;
const resolvedPath = pnpapi.resolveRequest(
specifier.replace('file:///', ''),
parentURL && parentURL.replace('file:///', '')
specifier,
parentURL ? fileURLToPath(parentURL) : undefined
);
if (!resolvedPath) {
Expand All @@ -322,7 +338,7 @@ export async function resolve(specifier, context) {
}
return {
url: new URL(\`file:///\${resolvedPath}\`).href,
url: pathToFileURL(resolvedPath).href,
};
}
`);
Expand Down

0 comments on commit aa834f8

Please sign in to comment.