Skip to content

Commit

Permalink
Merge 0260a2a into b8b8d29
Browse files Browse the repository at this point in the history
  • Loading branch information
ericclemmons committed May 23, 2016
2 parents b8b8d29 + 0260a2a commit a2bed94
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 61 deletions.
10 changes: 9 additions & 1 deletion example/webpack1-dev-server/webpack.config.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ module.exports = {
},

plugins: [
new NpmInstallPlugin(),
new NpmInstallPlugin({
dev: function(module, path) {
return [
"babel-preset-react-hmre",
"webpack-dev-middleware",
"webpack-hot-middleware",
].indexOf(module) !== -1;
},
}),
],

resolve: {
Expand Down
10 changes: 9 additions & 1 deletion example/webpack1/webpack.config.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ module.exports = {
},

plugins: [
new NpmInstallPlugin(),
new NpmInstallPlugin({
dev: function(module, path) {
return [
"babel-preset-react-hmre",
"webpack-dev-middleware",
"webpack-hot-middleware",
].indexOf(module) !== -1;
},
}),
],

resolve: {
Expand Down
10 changes: 9 additions & 1 deletion example/webpack2/webpack.config.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ module.exports = {
},

plugins: [
new NpmInstallPlugin(),
new NpmInstallPlugin({
dev: function(module, path) {
return [
"babel-preset-react-hmre",
"webpack-dev-middleware",
"webpack-hot-middleware",
].indexOf(module) !== -1;
},
}),
],

resolve: {
Expand Down
24 changes: 7 additions & 17 deletions src/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var EXTERNAL = /^\w[a-z\-0-9\.]+$/; // Match "react", "path", "fs", "lodash.rand
var INTERNAL = /^\./; // Match "./client", "../something", etc.
var PEERS = /UNMET PEER DEPENDENCY ([a-z\-0-9\.]+)@(.+)/gm;

var defaultOptions = { dev: false, peerDependencies: true };
var erroneous = [];

module.exports.check = function(request) {
Expand Down Expand Up @@ -131,6 +132,8 @@ module.exports.checkPackage = function checkPackage() {
spawn.sync("npm", ["init -y"], { stdio: "inherit" });
};

module.exports.defaultOptions = defaultOptions;

module.exports.install = function install(deps, options) {
if (!deps) {
return;
Expand All @@ -140,6 +143,8 @@ module.exports.install = function install(deps, options) {
deps = [deps];
}

options = Object.assign({}, defaultOptions, options);

// Ignore known, erroneous modules
deps = deps.filter(function(dep) {
return erroneous.indexOf(dep) === -1;
Expand All @@ -151,22 +156,7 @@ module.exports.install = function install(deps, options) {

var args = ["install"].concat(deps).filter(Boolean);

if (options) {
for (option in options) {
var arg = util.format("--%s", kebabCase(option));
var value = options[option];

if (value === false) {
continue;
}

if (value === true) {
args.push(arg);
} else {
args.push(util.format("%s='%s'", arg, value));
}
}
}
args.push(options.dev ? "--save-dev" : "--save");

deps.forEach(function(dep) {
console.info("Installing %s...", dep);
Expand Down Expand Up @@ -199,7 +189,7 @@ module.exports.install = function install(deps, options) {
peers.push(util.format("%s@%s", dep, version));
}

if (peers.length) {
if (options.peerDependencies && peers.length) {
console.info("Installing peerDependencies...");
this.install(peers, options);
console.info("");
Expand Down
38 changes: 22 additions & 16 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var depFromErr = function(err) {
function NpmInstallPlugin(options) {
this.preCompiler = null;
this.compiler = null;
this.options = options || {};
this.options = Object.assign(installer.defaultOptions, options);
this.resolving = {};

installer.checkPackage();
Expand All @@ -54,6 +54,24 @@ NpmInstallPlugin.prototype.apply = function(compiler) {
compiler.resolvers.normal.plugin("module", this.resolveModule.bind(this));
};

NpmInstallPlugin.prototype.install = function(result) {
if (!result) {
return;
}

var dep = installer.check(result.request);

if (dep) {
var dev = this.options.dev;

if (typeof this.options.dev === "function") {
dev = !!this.options.dev(result.request, result.path);
}

installer.install(dep, Object.assign({}, this.options, { dev: dev }));
}
}

NpmInstallPlugin.prototype.preCompile = function(compilation, next) {
if (!this.preCompiler) {
var options = this.compiler.options;
Expand Down Expand Up @@ -98,11 +116,7 @@ NpmInstallPlugin.prototype.resolveExternal = function(context, request, callback

this.resolve(result, function(err, filepath) {
if (err) {
var dep = installer.check(depFromErr(err));

if (dep) {
installer.install(dep, this.options);
}
this.install(Object.assign({}, result, { request: depFromErr(err) }));
}

callback();
Expand Down Expand Up @@ -141,11 +155,7 @@ NpmInstallPlugin.prototype.resolveLoader = function(result, next) {
loader += "-loader";
}

var dep = installer.check(loader);

if (dep) {
installer.install(dep, this.options);
}
this.install(Object.assign({}, result, { request: loader }));

return next();
};
Expand All @@ -166,11 +176,7 @@ NpmInstallPlugin.prototype.resolveModule = function(result, next) {
this.resolving[result.request] = false;

if (err) {
var dep = installer.check(depFromErr(err));

if (dep) {
installer.install(dep, this.options);
}
this.install(Object.assign({}, result, { request: depFromErr(err) }));
}

return next();
Expand Down
64 changes: 41 additions & 23 deletions test/installer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ var spawn = require("cross-spawn");
var installer = require("../src/installer");

describe("installer", function() {
describe(".defaultOptions", function() {
it("should default dev to false", function() {
expect(installer.defaultOptions.dev).toEqual(false);
});

it("should default peerDependencies to true", function() {
expect(installer.defaultOptions.peerDependencies).toEqual(true);
});
})

describe(".check", function() {
context("given nothing", function() {
it("should return undefined", function() {
Expand Down Expand Up @@ -280,32 +290,27 @@ describe("installer", function() {
});

context("given a dependency", function() {
it("should install it", function() {
var result = installer.install("foo");
context("with no options", function() {
it("should install it with --save", function() {
var result = installer.install("foo");

expect(this.sync).toHaveBeenCalled();
expect(this.sync.calls.length).toEqual(1);
expect(this.sync.calls[0].arguments[0]).toEqual("npm");
expect(this.sync.calls[0].arguments[1]).toEqual(["install", "foo"]);
expect(this.sync).toHaveBeenCalled();
expect(this.sync.calls.length).toEqual(1);
expect(this.sync.calls[0].arguments[0]).toEqual("npm");
expect(this.sync.calls[0].arguments[1]).toEqual(["install", "foo", "--save"]);
});
});

context("given options", function() {
it("should pass them to child process", function() {
context("with dev set to true", function() {
it("should install it with --save-dev", function() {
var result = installer.install("foo", {
save: true,
saveExact: false,
registry: "https://registry.npmjs.com/",
dev: true,
});

expect(this.sync).toHaveBeenCalled();
expect(this.sync.calls.length).toEqual(1);
expect(this.sync.calls[0].arguments[0]).toEqual("npm");
expect(this.sync.calls[0].arguments[1]).toEqual([
"install",
"foo",
"--save",
"--registry='https://registry.npmjs.com/'",
]);
expect(this.sync.calls[0].arguments[1]).toEqual(["install", "foo", "--save-dev"]);
});
});

Expand All @@ -328,14 +333,27 @@ describe("installer", function() {
});
});

it("should install peerDependencies", function() {
var result = installer.install("redbox-react");
context("given no options", function() {
it("should install peerDependencies", function() {
var result = installer.install("redbox-react");

expect(this.sync.calls.length).toEqual(2);
expect(this.sync.calls[0].arguments[1]).toEqual(["install", "redbox-react", "--save"]);
expect(this.sync.calls[1].arguments[1]).toEqual(["install", "react@\">=0.13.2 || ^0.14.0-rc1 || ^15.0.0-rc\"", "--save"]);
});
});

context("given peerDependencies set to false", function() {
it("should not install peerDependencies", function() {
var result = installer.install("redbox-react", {
peerDependencies: false,
});

expect(this.sync.calls.length).toEqual(2);
expect(this.sync.calls[0].arguments[1]).toEqual(["install", "redbox-react"]);
expect(this.sync.calls[1].arguments[1]).toEqual(["install", "react@\">=0.13.2 || ^0.14.0-rc1 || ^15.0.0-rc\""]);
expect(this.sync.calls.length).toEqual(1);
expect(this.sync.calls[0].arguments[1]).toEqual(["install", "redbox-react", "--save"]);
});
});
})
});
});
});
});
4 changes: 2 additions & 2 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe("plugin", function() {
this.next = expect.createSpy();

this.options = {
save: true,
saveDev: false,
dev: false,
peerDependencies: true,
};

this.plugin = new Plugin(this.options);
Expand Down

0 comments on commit a2bed94

Please sign in to comment.