-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Automatically install missing dependencies, part 2 #805
Changes from 2 commits
866e2b7
60e160d
1cacb59
bcbb07d
0e80fe3
184e6f9
1d30d0c
8b98fee
9f27b5d
2bd5410
9308335
8354b61
b3ba099
b076b7d
8a3dc51
9680030
6f4a194
949c1df
b3ba28e
63c34f5
b26cb28
11a5c35
9c91de6
9e06479
bdb6485
2356c2a
4cf5d2d
06eecdd
40efbb3
89a668d
674d458
2030211
eb9c263
b32e731
37c412c
3343f28
6e07e6d
c462a4e
d2716cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ const config = require('./utils/config'); | |
const emoji = require('./utils/emoji'); | ||
const loadEnv = require('./utils/env'); | ||
const PromiseQueue = require('./utils/PromiseQueue'); | ||
const installPackage = require('./utils/installPackage'); | ||
|
||
/** | ||
* The Bundler is the main entry point. It resolves and loads assets, | ||
|
@@ -328,26 +329,42 @@ class Bundler extends EventEmitter { | |
let thrown = err; | ||
|
||
if (thrown.message.indexOf(`Cannot find module '${dep.name}'`) === 0) { | ||
if (dep.optional) { | ||
return; | ||
} | ||
|
||
thrown.message = `Cannot resolve dependency '${dep.name}'`; | ||
|
||
// Add absolute path to the error message if the dependency specifies a relative path | ||
if (dep.name.startsWith('.')) { | ||
const absPath = Path.resolve(Path.dirname(asset.name), dep.name); | ||
err.message += ` at '${absPath}'`; | ||
} | ||
|
||
// Generate a code frame where the dependency was used | ||
if (dep.loc) { | ||
await asset.loadIfNeeded(); | ||
thrown.loc = dep.loc; | ||
thrown = asset.generateErrorMessage(thrown); | ||
let isLocalFile = dep.name.startsWith('.'); | ||
|
||
if (!isLocalFile) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could invert the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary once I used your try/catch split idea :) |
||
try { | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. Nested // Install
try {
logger.status(emoji.progress, `Installing ${dep.name}...`);
let dir = Path.dirname(asset.name);
await installPackage(dir, [dep.name], true, false);
} catch (npmError) {
logger.error(npmError.message);
}
// Resolve
try {
return await this.resolveAsset(dep.name, asset.name);
} catch (e) {
if (dep.optional) {
return;
} Or handle all in a single catch based on the error? |
||
logger.status(emoji.progress, `Installing ${dep.name}...`); | ||
let dir = Path.dirname(asset.name); | ||
await installPackage(dir, [dep.name], true, false); | ||
} catch (npmError) { | ||
logger.error(npmError.message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If install fails, should we continue or should it bail out? If the build would fail, could just bail out. If that's the case, could probably consolidate into a single |
||
} | ||
|
||
return await this.resolveAsset(dep.name, asset.name); | ||
} catch (e) { | ||
if (dep.optional) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should "optional" dependencies be installed automatically, or should we just skip these like before? Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the looks of it, optional deps are pretty much dependencies that are supposed to get installed/bundled, but aren’t supposed to throw an error if they aren’t able to be resolved or installed. So ideally we should attempt to install any optional deps, but we shouldn’t block the build if the install fails. |
||
return; | ||
} | ||
|
||
thrown.message = `Cannot resolve dependency '${dep.name}'`; | ||
|
||
// Add absolute path to the error message if the dependency specifies a relative path | ||
if (isLocalFile) { | ||
const absPath = Path.resolve(Path.dirname(asset.name), dep.name); | ||
err.message += ` at '${absPath}'`; | ||
} | ||
|
||
// Generate a code frame where the dependency was used | ||
if (dep.loc) { | ||
await asset.loadIfNeeded(); | ||
thrown.loc = dep.loc; | ||
thrown = asset.generateErrorMessage(thrown); | ||
} | ||
|
||
thrown.fileName = asset.name; | ||
} | ||
} | ||
|
||
thrown.fileName = asset.name; | ||
} | ||
throw thrown; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,9 @@ const config = require('./config'); | |
const path = require('path'); | ||
const promisify = require('./promisify'); | ||
const resolve = promisify(require('resolve')); | ||
const commandExists = require('command-exists').sync; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is used in an async method. Should use the promise version exposed by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya that’s exactly what I originally wanted to do that 😃 But... because of the way the Pretty much, I guess it’s possible to do it using a try/catch, but that’s really messy. The only other way to use the async version of So I can still use the async version... just it won’t be as clean, and I don’t really think there’s that much of a performance loss from doing it synchronously, especially considering that the function it’s running in is gonna be executing asynchronously anyway. Your call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're already inside of a promise chain ( hasYarnInstalled = async () => {
try {
return await commandExists('yarn');
} catch (err) {
return false;
}
}
if (await hasYarnInstalled() && fs.exists(...)) {
...
} |
||
|
||
async function install(dir, modules, installPeers = true) { | ||
async function install(dir, modules, installPeers = true, saveDev = true) { | ||
let location = await config.resolve(dir, ['yarn.lock', 'package.json']); | ||
|
||
return new Promise((resolve, reject) => { | ||
|
@@ -13,12 +14,18 @@ async function install(dir, modules, installPeers = true) { | |
cwd: location ? path.dirname(location) : dir | ||
}; | ||
|
||
if (location && path.basename(location) === 'yarn.lock') { | ||
install = spawn('yarn', ['add', ...modules, '--dev'], options); | ||
} else { | ||
install = spawn('npm', ['install', ...modules, '--save-dev'], options); | ||
let args = ['add', ...modules]; | ||
if (saveDev) { | ||
args.push('-D'); | ||
} | ||
|
||
let command = 'npm'; | ||
if (commandExists('yarn')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better preserve the old logic of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting... is there a case where you wouldn’t want to use yarn if you already have it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure there is! I have yarn and I don't use it for every project. Specially when working with a team, everyone must use the same package manager for that project. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it :) We should keep both though, right? So we should check for the yarn lock file AND the yarn command, that way if they have a yarn lock but no yarn they can still use npm. Or is that also not necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no need for keeping both checks, but I see no harm as well. Your call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for keeping the |
||
command = 'yarn'; | ||
} | ||
|
||
install = spawn(command, args, options); | ||
|
||
install.stdout.pipe(process.stdout); | ||
install.stderr.pipe(process.stderr); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could the dependency be an absolute path, which would still be a local file? Trying to install in that scenario would blow up on
npm install
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya you’re right.
I would also have to add support for tilde paths etc now that #850 landed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need something like
let isLocalFile = /^[/~.]/.test(dep.name);
in order to support the new resolver. See https://github.com/parcel-bundler/parcel/blob/master/src/Resolver.js#L88-L114