Skip to content

Commit

Permalink
Fixup NodePackageManager with builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Aug 24, 2022
1 parent eddc73f commit 02290e2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 7 additions & 5 deletions packages/core/package-manager/src/NodePackageManager.js
Expand Up @@ -22,6 +22,7 @@ import Module from 'module';
import path from 'path';
import semver from 'semver';

import {getModuleParts} from '@parcel/utils';
import {getConflictingLocalDependencies} from './utils';
import {installPackage} from './installPackage';
import pkg from '../package.json';
Expand Down Expand Up @@ -138,7 +139,7 @@ export class NodePackageManager implements PackageManager {
}

async resolve(
name: DependencySpecifier,
id: DependencySpecifier,
from: FilePath,
options?: ?{|
range?: ?SemverRange,
Expand All @@ -147,11 +148,12 @@ export class NodePackageManager implements PackageManager {
|},
): Promise<ResolveResult> {
let basedir = path.dirname(from);
let key = basedir + ':' + name;
let key = basedir + ':' + id;
let resolved = cache.get(key);
if (!resolved) {
let [name] = getModuleParts(id);
try {
resolved = await this.resolver.resolve(name, from);
resolved = await this.resolver.resolve(id, from);
} catch (e) {
if (
e.code !== 'MODULE_NOT_FOUND' ||
Expand Down Expand Up @@ -189,7 +191,7 @@ export class NodePackageManager implements PackageManager {
saveDev: options?.saveDev ?? true,
});

return this.resolve(name, from, {
return this.resolve(id, from, {
...options,
shouldAutoInstall: false,
});
Expand Down Expand Up @@ -230,7 +232,7 @@ export class NodePackageManager implements PackageManager {

if (conflicts == null && options?.shouldAutoInstall === true) {
await this.install([{name, range}], from);
return this.resolve(name, from, {
return this.resolve(id, from, {
...options,
shouldAutoInstall: false,
});
Expand Down
8 changes: 5 additions & 3 deletions packages/utils/node-resolver-core/src/NodeResolver.js
Expand Up @@ -300,9 +300,11 @@ export default class NodeResolver {
// ignore
}

if (builtin != null) {
// Autoinstall/verify version of builtin polyfills
if (builtin?.range != null) {
// This assumes that there are no polyfill packages that are scoped
let packageName = builtin.name.split('/')[0];
// Append '/' to force this.packageManager to look up the package in node_modules
let packageName = builtin.name.split('/')[0] + '/';
let packageManager = this.packageManager;
if (resolved == null) {
// Auto install the Node builtin polyfills
Expand Down Expand Up @@ -383,7 +385,7 @@ export default class NodeResolver {
this.projectRoot + '/index',
{
saveDev: true,
shouldAutoInstall: true,
shouldAutoInstall: this.shouldAutoInstall,
range: builtin.range,
},
);
Expand Down

0 comments on commit 02290e2

Please sign in to comment.