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

[WIP-HELP-WANTED] feat: use arg parsing instead of object-oriented flag setting #1738

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions packages/webpack-cli/lib/utils/GroupHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,102 @@ class GroupHelper {
this.strategy = undefined;
}

ifArg(name, fn, init, finalize) {
const isArray = Array.isArray(argv[name]);
const isSet = typeof argv[name] !== "undefined" && argv[name] !== null;
if (!isArray && !isSet) return;

init && init();
if (isArray) argv[name].forEach(fn);
else if (isSet) fn(argv[name], -1);
finalize && finalize();
}

ifArgPair(name, fn, init, finalize) {
Copy link
Member

Choose a reason for hiding this comment

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

Can't understand code

ifArg(
name,
function (content, idx) {
const i = content.indexOf("=");
if (i < 0) {
return fn(null, content, idx);
} else {
return fn(content.substr(0, i), content.substr(i + 1), idx);
}
},
init,
finalize
);
}

ifBooleanArg(name, fn) {
ifArg(name, function (bool) {
if (bool) {
fn();
}
});
}

mapArgToBoolean(name, optionName) {
ifArg(name, function (bool) {
if (bool === true) options[optionName || name] = true;
else if (bool === false) options[optionName || name] = false;
});
Copy link
Member

Choose a reason for hiding this comment

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

Very misleading, we wee need this - optionName || name?

}

loadPlugin(name) {
const loadUtils = require("loader-utils");
let args;
try {
const p = name && name.indexOf("?");
Copy link
Member

Choose a reason for hiding this comment

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

Plugins can't have ? in name

if (p > -1) {
args = loadUtils.parseQuery(name.substring(p));
name = name.substring(0, p);
}
} catch (e) {
console.log("Invalid plugin arguments " + name + " (" + e + ").");
process.exit(-1); // eslint-disable-line
}

let path;
try {
const resolve = require("enhanced-resolve");
path = resolve.sync(process.cwd(), name);
Copy link
Member

Choose a reason for hiding this comment

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

You don't need this, because you have require.resolve

} catch (e) {
console.log("Cannot resolve plugin " + name + ".");
process.exit(-1); // eslint-disable-line
}
let Plugin;
try {
Plugin = require(path);
} catch (e) {
console.log("Cannot load plugin " + name + ". (" + path + ")");
throw e;
}
try {
return new Plugin(args);
} catch (e) {
console.log("Cannot instantiate plugin " + name + ". (" + path + ")");
throw e;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

We don't need it, plugin only for webpack.conifg.js configuration, trying to load enhanced-resolve break yarn PnP


ensureObject(parent, name, force) {
if (force || typeof parent[name] !== "object" || parent[name] === null) {
parent[name] = {};
}
}

ensureArray(parent, name) {
if (!Array.isArray(parent[name])) {
parent[name] = [];
}
}

addPlugin(options, plugin) {
ensureArray(options, "plugins");
options.plugins.unshift(plugin);
}

hyphenToUpperCase(name) {
if (!name) {
return name;
Expand Down