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

Automatically install missing dependencies, part 2 #805

Merged
merged 39 commits into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
866e2b7
Automagically npm install missing dependencies
davidnagli Feb 14, 2018
60e160d
Changed automatic install to use installPackage helper
davidnagli Feb 14, 2018
1cacb59
Merge branch 'master' into master
davidnagli Feb 14, 2018
bcbb07d
Added back yarn.lock check, improved yarn.lock & project root resolution
davidnagli Feb 15, 2018
0e80fe3
Merge remote-tracking branch 'origin/master'
davidnagli Feb 15, 2018
184e6f9
Bug fix
davidnagli Feb 16, 2018
1d30d0c
refactor/cleanup code to fix the issues @brandon93s pointed out
davidnagli Feb 16, 2018
8b98fee
Dev mode check
davidnagli Feb 16, 2018
9f27b5d
Added autoinstall cli flags
davidnagli Feb 17, 2018
2bd5410
Merge remote-tracking branch 'upstream/master'
davidnagli Feb 17, 2018
9308335
Fixed Node 6 Compatiblity Issues
davidnagli Feb 18, 2018
8354b61
Fixed refactoring mistake
davidnagli Feb 21, 2018
b3ba099
Removed recursive config file search
davidnagli Feb 21, 2018
b076b7d
Merge branch 'master' of https://github.com/davidnagli/parcel into da…
devongovett Feb 22, 2018
8a3dc51
autoinstall improvements
devongovett Feb 23, 2018
9680030
Merge branch 'master' of github.com:parcel-bundler/parcel into davidn…
devongovett Mar 6, 2018
6f4a194
Merge branch 'davidnagli-master' into devon-autoinstall
devongovett Mar 6, 2018
949c1df
Removed redundent variable projectRootLocation
davidnagli Mar 6, 2018
b3ba28e
Added back reccursive yarn.lock resolution
davidnagli Mar 6, 2018
63c34f5
CHANGED CONFIG.RESOLVE() TO CHECK THE DIRECORY BEFORE GOING TO PARENT
davidnagli Mar 6, 2018
b26cb28
Changed localrequire to pass in 'basedir' instead of 'path' to install()
davidnagli Mar 6, 2018
11a5c35
Added initial unit tests
davidnagli Mar 6, 2018
9c91de6
Whoops... removed test.only() filter from autoinstall unit tests
davidnagli Mar 6, 2018
9e06479
Merge branch 'master' of https://github.com/davidnagli/parcel
davidnagli Mar 6, 2018
bdb6485
Added support for absolute and tilde paths
davidnagli Mar 6, 2018
2356c2a
Refactored parameters into config object
davidnagli Mar 6, 2018
4cf5d2d
Fixed refactored usages, cleaned up unit tests
davidnagli Mar 7, 2018
06eecdd
Renamed 'config' to 'configObj'
davidnagli Mar 7, 2018
40efbb3
Merge branch 'master' of https://github.com/davidnagli/parcel into de…
devongovett Mar 7, 2018
89a668d
Cleanup / Refactoring - Split code into seperate functions
davidnagli Mar 8, 2018
674d458
Cleaned up unit tests
davidnagli Mar 8, 2018
2030211
Retrigger CI
davidnagli Mar 8, 2018
eb9c263
Merge branch 'master' into master
davidnagli Mar 13, 2018
b32e731
Merge branch 'master' of https://github.com/davidnagli/parcel into de…
devongovett Mar 17, 2018
37c412c
Merge branch 'master' of github.com:parcel-bundler/parcel into devon-…
devongovett Mar 18, 2018
3343f28
Autoinstall improvements
devongovett Mar 18, 2018
6e07e6d
Fix tests
devongovett Mar 18, 2018
c462a4e
Remove explicit package-manager argument for now
devongovett Mar 18, 2018
d2716cd
Fix line counting on windows
devongovett Mar 18, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"mkdirp": "^0.5.1",
"node-forge": "^0.7.1",
"node-libs-browser": "^2.0.0",
"npm-programmatic": "^0.0.10",
"opn": "^5.1.0",
"physical-cpu-count": "^2.0.0",
"postcss": "^6.0.10",
Expand Down
56 changes: 37 additions & 19 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 npm = require('npm-programmatic');

/**
* The Bundler is the main entry point. It resolves and loads assets,
Expand Down Expand Up @@ -328,26 +329,43 @@ 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('.');
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

@devongovett devongovett Feb 23, 2018

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


if (!isLocalFile) {
Copy link
Contributor

@brandon93s brandon93s Feb 15, 2018

Choose a reason for hiding this comment

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

Could invert the if to reduce some of the nesting here for readability.

Copy link
Contributor Author

@davidnagli davidnagli Feb 15, 2018

Choose a reason for hiding this comment

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

Not necessary once I used your try/catch split idea :)

try {
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. Nested try statements doesn't feel right. Can they be sequential?

// 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}...`);
await npm.install([dep.name], {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason you can't use the installPackage helper that we already have? That also supports yarn.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn’t know it existed 😬 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only problem is that it saves dependencies as a dev dependencies by default with no way to override that, so I’m gonna need to change it and refactor the code that uses it.

Copy link
Member

Choose a reason for hiding this comment

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

Sure. You could just make dev dependency the default, and have an option to override it. Then you wouldn't need to change the existing calls.

cwd: Path.dirname(asset.name)
});
} catch (npmError) {
logger.error(npmError.message);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 try...catch .

}

return await this.resolveAsset(dep.name, asset.name);
} catch (e) {
if (dep.optional) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
Expand Down
8 changes: 7 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ block-stream@*:
dependencies:
inherits "~2.0.0"

bluebird@^3.0.5:
bluebird@^3.0.5, bluebird@^3.4.1:
version "3.5.1"
resolved "https://npm.cubos.io/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9"

Expand Down Expand Up @@ -4015,6 +4015,12 @@ npm-path@^2.0.2:
dependencies:
which "^1.2.10"

npm-programmatic@^0.0.10:
version "0.0.10"
resolved "https://registry.yarnpkg.com/npm-programmatic/-/npm-programmatic-0.0.10.tgz#9765820505d03083e57c6a7729bed515997a61c9"
dependencies:
bluebird "^3.4.1"

npm-run-path@^2.0.0:
version "2.0.2"
resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f"
Expand Down