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 Nov 23, 2020
1 parent 5865163 commit b043a03
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 @@ -283,17 +283,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 @@ -303,7 +319,7 @@ export async function resolve(specifier, context) {
}
return {
url: new URL(\`file:///\${resolvedPath}\`).href,
url: pathToFileURL(resolvedPath).href,
};
}
`);
Expand Down

0 comments on commit b043a03

Please sign in to comment.