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

Add unit tests for CLI and add flow compilation #186

Merged
merged 13 commits into from
Oct 5, 2017
Merged
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
**/__testfixtures__/*
coverage
test
dist
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ yarn-error.log

# Jest Coverage
coverage

# Dist build
dist

# Test Compilation
test/js/*
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ before_script:
- bash <(curl -s https://codecov.io/bash)

script:
- npm run prepublish
- npm run lint
- npm run test
12 changes: 11 additions & 1 deletion bin/config-yargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var INIT_GROUP = "Initialization:";
module.exports = function(yargs) {
yargs
.help("help")
.alias("help", "h", "?")
.alias("help", "h")
.version()
.alias("version", "v")
.options({
Expand All @@ -28,6 +28,16 @@ module.exports = function(yargs) {
"Migrate your webpack configuration from webpack 1 to webpack 2",
group: INIT_GROUP
},
"generate-loader": {
type: "boolean",
describe: "Generates a new webpack loader project",
group: INIT_GROUP
},
"generate-plugin": {
type: "boolean",
describe: "Generates a new webpack plugin project",
group: INIT_GROUP
},
config: {
type: "string",
describe: "Path to the config file",
Expand Down
126 changes: 74 additions & 52 deletions bin/convert-argv.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"use strict";

var path = require("path");
var fs = require("fs");
fs.existsSync = fs.existsSync || path.existsSync;
var interpret = require("interpret");
var prepareOptions = require("./prepareOptions");

module.exports = function(yargs, argv, convertOptions) {
var options = [];
Expand All @@ -20,7 +19,7 @@ module.exports = function(yargs, argv, convertOptions) {
argv["optimize-minimize"] = true;
argv["define"] = []
.concat(argv["define"] || [])
.concat("process.env.NODE_ENV='production'");
.concat("process.env.NODE_ENV=\"production\"");
}

var configFileLoaded = false;
Expand Down Expand Up @@ -103,14 +102,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 All @@ -134,7 +126,7 @@ module.exports = function(yargs, argv, convertOptions) {
console.error(
"Config did not export an object or a function returning an object."
);
process.exit(-1);
process.exit(-1); // eslint-disable-line
}

// process Promise
Expand All @@ -147,6 +139,22 @@ module.exports = function(yargs, argv, convertOptions) {
return processConfiguredOptions(options.default);
}

// filter multi-config by name
if (Array.isArray(options) && argv["config-name"]) {
var namedOptions = options.filter(function(opt) {
return opt.name === argv["config-name"];
});
if (namedOptions.length === 0) {
console.error(
"Configuration with name '" + argv["config-name"] + "' was not found."
);
process.exit(-1); // eslint-disable-line
} else if (namedOptions.length === 1) {
return processConfiguredOptions(namedOptions[0]);
}
options = namedOptions;
}

if (Array.isArray(options)) {
options.forEach(processOptions);
} else {
Expand All @@ -169,11 +177,12 @@ module.exports = function(yargs, argv, convertOptions) {
options.watchOptions.aggregateTimeout = +argv["watch-aggregate-timeout"];
}

if (argv["watch-poll"]) {
if (typeof argv["watch-poll"] !== undefined) {
options.watchOptions = options.watchOptions || {};
if (typeof argv["watch-poll"] !== "boolean")
if (argv["watch-poll"] === "true" || argv["watch-poll"] === "")
options.watchOptions.poll = true;
else if (!isNaN(argv["watch-poll"]))
options.watchOptions.poll = +argv["watch-poll"];
else options.watchOptions.poll = true;
}

if (argv["watch-stdin"]) {
Expand Down Expand Up @@ -233,21 +242,22 @@ module.exports = function(yargs, argv, convertOptions) {
}

function mapArgToBoolean(name, optionName) {
if (options[optionName || name]) {
options[name] = true;
}
//eslint-disable-next-line
if (name && options[name] == true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't be ===?

Copy link
Member Author

Choose a reason for hiding this comment

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

Implemented by webpack/core. You should just look at the test for now

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, let's keep it non-strict equal, cause it could be stringified

return;
}
ifArg(name, function(bool) {
if (bool === true) options[optionName || name] = true;
else if (bool === false) options[optionName || name] = false;
});
}
//eslint-disable-next-line
function mapArgToPath(name, optionName) {
ifArg(name, function(str) {
options[optionName || name] = path.resolve(str);
});
}

function loadPlugin(name) {
var loadUtils = require("loader-utils");
var args = null;
var args;
try {
var p = name && name.indexOf("?");
if (p > -1) {
Expand All @@ -256,7 +266,7 @@ module.exports = function(yargs, argv, convertOptions) {
}
} catch (e) {
console.log("Invalid plugin arguments " + name + " (" + e + ").");
process.exit(-1);
process.exit(-1); // eslint-disable-line
}

var path;
Expand All @@ -265,7 +275,7 @@ module.exports = function(yargs, argv, convertOptions) {
path = resolve.sync(process.cwd(), name);
} catch (e) {
console.log("Cannot resolve plugin " + name + ".");
process.exit(-1);
process.exit(-1); // eslint-disable-line
}
var Plugin;
try {
Expand Down Expand Up @@ -297,43 +307,53 @@ module.exports = function(yargs, argv, convertOptions) {
ifArgPair(
"entry",
function(name, entry) {
options.entry[name] = entry;
if (
typeof options.entry[name] !== "undefined" &&
options.entry[name] !== null
) {
options.entry[name] = [].concat(options.entry[name]).concat(entry);
} else {
options.entry[name] = entry;
}
},
function() {
ensureObject(options, "entry");
}
);

function bindLoaders(arg, collection) {
function bindRules(arg) {
ifArgPair(
arg,
function(name, binding) {
if (name === null) {
name = binding;
binding += "-loader";
}
options.module[collection].push({
var rule = {
/* eslint-disable no-useless-escape */
test: new RegExp(
"\\." +
// eslint thinks that the escapes are useless,
// however, when testing them, the special regex chars
// mess up with the regex we want to use to check.
// eslint-disable-next-line
name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") +
"$"
),
) /* eslint-enable no-useless-escape */,
loader: binding
});
};
if (arg === "module-bind-pre") {
rule.enforce = "pre";
} else if (arg === "module-bind-post") {
rule.enforce = "post";
}
options.module.rules.push(rule);
},
function() {
ensureObject(options, "module");
ensureArray(options.module, collection);
ensureArray(options.module, "rules");
}
);
}
bindLoaders("module-bind", "loaders");
bindLoaders("module-bind-pre", "preLoaders");
bindLoaders("module-bind-post", "postLoaders");
bindRules("module-bind");
bindRules("module-bind-pre");
bindRules("module-bind-post");

var defineObject;
ifArgPair(
Expand All @@ -357,7 +377,7 @@ module.exports = function(yargs, argv, convertOptions) {

ifArg("output-path", function(value) {
ensureObject(options, "output");
options.output.path = value;
options.output.path = path.resolve(value);
});

ifArg("output-filename", function(value) {
Expand Down Expand Up @@ -502,7 +522,7 @@ module.exports = function(yargs, argv, convertOptions) {

ifArg("prefetch", function(request) {
ensureArray(options, "plugins");
var PrefetchPlugin = require("webpack/PrefetchPlugin");
var PrefetchPlugin = require("webpack/lib/PrefetchPlugin");
options.plugins.push(new PrefetchPlugin(request));
});

Expand All @@ -516,16 +536,10 @@ module.exports = function(yargs, argv, convertOptions) {
} else {
name = value;
}
var ProvidePlugin = require("webpack/ProvidePlugin");
var ProvidePlugin = require("webpack/lib/ProvidePlugin");
options.plugins.push(new ProvidePlugin(name, value));
});

ifBooleanArg("labeled-modules", function() {
ensureArray(options, "plugins");
var LabeledModulesPlugin = require("webpack/lib/dependencies/LabeledModulesPlugin");
options.plugins.push(new LabeledModulesPlugin());
});

ifArg("plugin", function(value) {
ensureArray(options, "plugins");
options.plugins.push(loadPlugin(value));
Expand All @@ -534,15 +548,18 @@ module.exports = function(yargs, argv, convertOptions) {
mapArgToBoolean("bail");

mapArgToBoolean("profile");

if (noOutputFilenameDefined) {
ensureObject(options, "output");
if (convertOptions && convertOptions.outputFilename) {
options.output.path = path.dirname(convertOptions.outputFilename);
options.output.path = path.resolve(
path.dirname(convertOptions.outputFilename)
);
options.output.filename = path.basename(convertOptions.outputFilename);
} else if (argv._.length > 0) {
options.output.filename = argv._.pop();
options.output.path = path.dirname(options.output.filename);
options.output.path = path.resolve(
path.dirname(options.output.filename)
);
options.output.filename = path.basename(options.output.filename);
} else if (configFileLoaded) {
throw new Error(
Expand All @@ -556,7 +573,7 @@ module.exports = function(yargs, argv, convertOptions) {
"A configuration file could be named 'webpack.config.js' in the current directory."
);
console.error("Use --help to display the CLI options.");
process.exit(-1);
process.exit(-1); // eslint-disable-line
}
}

Expand Down Expand Up @@ -584,7 +601,12 @@ module.exports = function(yargs, argv, convertOptions) {
if (i < 0 || (j >= 0 && j < i)) {
var resolved = path.resolve(content);
if (fs.existsSync(resolved)) {
addTo("main", resolved);
addTo(
"main",
`${resolved}${fs.statSync(resolved).isDirectory()
? path.sep
: ""}`
);
} else {
addTo("main", content);
}
Expand All @@ -609,7 +631,7 @@ module.exports = function(yargs, argv, convertOptions) {
);
}
console.error("Use --help to display the CLI options.");
process.exit(-1);
process.exit(-1); // eslint-disable-line
}
}
};
30 changes: 30 additions & 0 deletions bin/prepareOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"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;
}
Loading