webpack-contrib / install-webpack-plugin

Speed up development by automatically installing & saving dependencies with Webpack.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Check whether module is actually installed

d4rkr00t opened this issue · comments

Hi,

I use your great webpack plugin in my prototyping tool — Aik. And it works pretty well. And covers most of use cases except one:

If I have package.json but haven't yet installed node modules npm-install-webpack-plugin thinks that they are installed because of this check:

https://github.com/ericclemmons/npm-install-webpack-plugin/blob/master/src/installer.js#L42-L44

What do you think about checking on file system whether directory exists or not instead of checking in package.json?

I can send a pull request if you think it's a good idea :)

The plugin is alreeady checking if node_modules/${dep} exists, but is only bailing out if it exists and is a symlink.

What if we were to replace this:

  try {
    var pkgPath = require.resolve(path.join(process.cwd(), "package.json"));
    var pkg = require(pkgPath);

    // Remove cached copy for future checks
    delete require.cache[pkgPath];
  } catch(e) {
    throw e;
  }

  var hasDep = pkg.dependencies && pkg.dependencies[dep];
  var hasDevDep = pkg.devDependencies && pkg.devDependencies[dep];

  // Bail early if we've already installed this dependency
  if (hasDep || hasDevDep) {
    return;
  }

  // Ignore linked modules
  try {
    var stats = fs.lstatSync(path.join(process.cwd(), "node_modules", dep));

    if (stats.isSymbolicLink()) {
      return;
    }
  } catch(e) {
    // Module exists in node_modules, but isn't symlinked
  }

  // Ignore NPM global modules (e.g. "path", "fs", etc.)
  try {
    var resolved = require.resolve(dep);

    // Global modules resolve to their name, not an actual path
    if (resolved.match(EXTERNAL)) {
      return;
    }
  } catch(e) {
    // Module is not resolveable
  }

With this, (using node-resolve) which just checks if the dependency is resolvable from the cwd and bails if so:

  try {
    resolve.sync(dep, {basedir: process.cwd()});
    return;
  } catch(e) {
    // Module is not resolveable
  }

It doesn't actually check if deps exists, it checks if it is in package.json

Yeah, your solution looks good :)