Skip to content

Commit

Permalink
Fix localRequire with package/path requests (#2425)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and wbinnssmith committed Mar 14, 2019
1 parent 54a508a commit dabc97e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
17 changes: 4 additions & 13 deletions packages/core/parcel-bundler/src/Resolver.js
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const {isGlob} = require('./utils/glob');
const fs = require('@parcel/fs');
const micromatch = require('micromatch');
const getModuleParts = require('./utils/getModuleParts');

const EMPTY_SHIM = require.resolve('./builtins/_empty');

Expand Down Expand Up @@ -104,7 +105,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 = getModuleParts(filename);
resolved = {
moduleName: parts[0],
subPath: parts[1]
Expand Down Expand Up @@ -171,7 +172,7 @@ class Resolver {
return {filePath: builtins[filename]};
}

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

while (dir !== root) {
Expand Down Expand Up @@ -374,7 +375,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 = 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 +439,6 @@ 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;
}
}

module.exports = Resolver;
11 changes: 11 additions & 0 deletions packages/core/parcel-bundler/src/utils/getModuleParts.js
@@ -0,0 +1,11 @@
const path = require('path');

module.exports = function(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;
};
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 getModuleParts = require('./getModuleParts');

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 = getModuleParts(name)[0];
await installPackage(packageName, path);
return await localResolve(name, path, true);
}
throw e;
Expand Down

0 comments on commit dabc97e

Please sign in to comment.