Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix localRequire with package/path requests #2425

Merged
merged 3 commits into from Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/core/parcel-bundler/src/Resolver.js
Expand Up @@ -104,7 +104,7 @@ class Resolver {

// If we couldn't resolve the node_modules path, just return the module name info
if (!resolved) {
let parts = this.getModuleParts(filename);
let parts = Resolver.getModuleParts(filename);
resolved = {
moduleName: parts[0],
subPath: parts[1]
Expand Down Expand Up @@ -171,7 +171,7 @@ class Resolver {
return {filePath: builtins[filename]};
}

let parts = this.getModuleParts(filename);
let parts = Resolver.getModuleParts(filename);
let root = path.parse(dir).root;

while (dir !== root) {
Expand Down Expand Up @@ -374,7 +374,7 @@ class Resolver {
alias = this.lookupAlias(aliases, filename, dir);
if (alias == null) {
// If it didn't match, try only the module name.
let parts = this.getModuleParts(filename);
let parts = Resolver.getModuleParts(filename);
alias = this.lookupAlias(aliases, parts[0], dir);
if (typeof alias === 'string') {
// Append the filename back onto the aliased module.
Expand Down Expand Up @@ -438,16 +438,16 @@ class Resolver {
let pkg = await this.findPackage(dir);
return this.resolveAliases(filename, pkg);
}
}

getModuleParts(name) {
let parts = path.normalize(name).split(path.sep);
if (parts[0].charAt(0) === '@') {
// Scoped module (e.g. @scope/module). Merge the first two parts back together.
parts.splice(0, 2, `${parts[0]}/${parts[1]}`);
}

return parts;
Resolver.getModuleParts = function(name) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the static keyword and leave this in the class. A better way might be to just extract this function as a util file, that way localRequire isn't dependent on the resolver. In v2, the resolver will be in a separate package.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just extract this function as a util file

Done. How should this be tested? Determining a package which is definitely not already installed by some parcel package doesn't seem very reliable.

let parts = path.normalize(name).split(path.sep);
if (parts[0].charAt(0) === '@') {
// Scoped module (e.g. @scope/module). Merge the first two parts back together.
parts.splice(0, 2, `${parts[0]}/${parts[1]}`);
}
}

return parts;
};

module.exports = Resolver;
4 changes: 3 additions & 1 deletion packages/core/parcel-bundler/src/utils/localRequire.js
Expand Up @@ -2,6 +2,7 @@ const {dirname} = require('path');
const {promisify} = require('@parcel/utils');
const resolve = promisify(require('resolve'));
const installPackage = require('./installPackage');
const Resolver = require('../Resolver');

const cache = new Map();

Expand All @@ -19,7 +20,8 @@ async function localResolve(name, path, triedInstall = false) {
resolved = await resolve(name, {basedir});
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND' && !triedInstall) {
await installPackage(name, path);
const packageName = Resolver.getModuleParts(name)[0];
await installPackage(packageName, path);
return await localResolve(name, path, true);
}
throw e;
Expand Down