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 all commits
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
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