diff --git a/README.md b/README.md index ca20e28..5642e3b 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,23 @@ Elm.Main.init({node: document.getElementById("main")}); Elm.Path.To.OtherModule.init({node: document.getElementById("other")}); ``` +#### maxInstances (default `undefined`) + +You can add `maxInstances=8` to the loader: + +```js + ... + use: { + loader: 'elm-webpack-loader', + options: { + maxInstances: 8 + } + } + ... +``` + +Set a limit to the number of maxInstances of elm that can spawned. + ##### Hot module reloading Hot module reloading is supported by installing [elm-hot-webpack-loader](https://github.com/klazuka/elm-hot-webpack-loader) diff --git a/index.js b/index.js index 21bc8dd..b21debf 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,8 @@ var temp = require("temp").track(); var loaderUtils = require("loader-utils"); var elmCompiler = require("node-elm-compiler"); +var runningInstances = 0; + var getFiles = function(options) { var files = options && options.files; @@ -95,6 +97,14 @@ module.exports = function() { var files = getFiles.call(this, options); var resourcePath = this.resourcePath; + var maxInstances = options.maxInstances; + + if (typeof maxInstances === "undefined"){ + maxInstances = null; + } else { + delete options.maxInstances; + } + var promises = []; // we only need to track deps if we are in watch mode @@ -124,30 +134,43 @@ module.exports = function() { promises.push(dependencies); } - var compilation = compile(files, options) - .then(function(v) { return { kind: "success", result: v }; }) - .catch(function(v) { return { kind: "error", error: v }; }); - - promises.push(compilation); - - Promise.all(promises) - .then(function(results) { - var output = results[results.length - 1]; // compilation output is always last + var intervalId; + + var run = function() { + if (maxInstances !== null && runningInstances >= maxInstances) { return false }; + runningInstances += 1; + clearInterval(intervalId); + + var compilation = compile(files, options) + .then(function(v) { runningInstances -= 1; return { kind: "success", result: v }; }) + .catch(function(v) { runningInstances -= 1; return { kind: "error", error: v }; }); + + promises.push(compilation); + + Promise.all(promises) + .then(function(results) { + var output = results[results.length - 1]; // compilation output is always last + + if (output.kind === "success") { + callback(null, output.result); + } else { + if (typeof output.error === "string") { + output.error = new Error(output.error); + } + + output.error.message = "Compiler process exited with error " + output.error.message; + output.error.stack = null; + callback(output.error); + } + }).catch(function(err){ + callback(err); + }); - if (output.kind === "success") { - callback(null, output.result); - } else { - if (typeof output.error === "string") { - output.error = new Error(output.error); - } + }; - output.error.message = "Compiler process exited with error " + output.error.message; - output.error.stack = null; - callback(output.error); - } - }).catch(function(err){ - callback(err); - }); + if (run() === false) { + intervalId = setInterval(run, 200); + } }