Skip to content

Commit

Permalink
Merge pull request #5196 from AndersDJohnson/multi-compiler-functions
Browse files Browse the repository at this point in the history
feat: support config functions with multi-compiler
  • Loading branch information
sokra committed Jul 6, 2017
2 parents 2eccb19 + 24ac48d commit 0ab36c4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
9 changes: 2 additions & 7 deletions bin/convert-argv.js
Expand Up @@ -2,6 +2,7 @@ var path = require("path");
var fs = require("fs");
fs.existsSync = fs.existsSync || path.existsSync;
var interpret = require("interpret");
var prepareOptions = require("../lib/prepareOptions");

module.exports = function(yargs, argv, convertOptions) {

Expand Down Expand Up @@ -94,13 +95,7 @@ module.exports = function(yargs, argv, convertOptions) {

var requireConfig = function requireConfig(configPath) {
var options = require(configPath);
var isES6DefaultExportedFunc = (
typeof options === "object" && options !== null && typeof options.default === "function"
);
if(typeof options === "function" || isES6DefaultExportedFunc) {
options = isES6DefaultExportedFunc ? options.default : options;
options = options(argv.env, argv);
}
options = prepareOptions(options, argv);
return options;
};

Expand Down
29 changes: 29 additions & 0 deletions lib/prepareOptions.js
@@ -0,0 +1,29 @@
"use strict";

module.exports = function prepareOptions(options, argv) {
argv = argv || {};

options = handleExport(options);

if(Array.isArray(options)) {
options = options.map(_options => handleFunction(_options, argv));
} else {
options = handleFunction(options, argv);
}
return options;
};

function handleExport(options) {
const isES6DefaultExported = (
typeof options === "object" && options !== null && typeof options.default !== "undefined"
);
options = isES6DefaultExported ? options.default : options;
return options;
}

function handleFunction(options, argv) {
if(typeof options === "function") {
options = options(argv.env, argv);
}
return options;
}
3 changes: 2 additions & 1 deletion test/ConfigTestCases.test.js
Expand Up @@ -10,6 +10,7 @@ const checkArrayExpectation = require("./checkArrayExpectation");

const Stats = require("../lib/Stats");
const webpack = require("../lib/webpack");
const prepareOptions = require("../lib/prepareOptions");

describe("ConfigTestCases", () => {
const casesPath = path.join(__dirname, "configCases");
Expand Down Expand Up @@ -39,7 +40,7 @@ describe("ConfigTestCases", () => {
this.timeout(30000);
const testDirectory = path.join(casesPath, category.name, testName);
const outputDirectory = path.join(__dirname, "js", "config", category.name, testName);
const options = require(path.join(testDirectory, "webpack.config.js"));
const options = prepareOptions(require(path.join(testDirectory, "webpack.config.js")));
const optionsArr = [].concat(options);
optionsArr.forEach((options, idx) => {
if(!options.context) options.context = testDirectory;
Expand Down
@@ -0,0 +1,3 @@
it("should run a multi compiler with functions with export default", function() {

});
@@ -0,0 +1,5 @@
exports.default = [
function() {
return {};
}
];
3 changes: 3 additions & 0 deletions test/configCases/simple/multi-compiler-functions/index.js
@@ -0,0 +1,3 @@
it("should run a multi compiler with functions", function() {

});
@@ -0,0 +1,5 @@
module.exports = [
function() {
return {};
}
];

0 comments on commit 0ab36c4

Please sign in to comment.