Skip to content

Commit 8141e0e

Browse files
committed
chore: rebase
1 parent 6c766f8 commit 8141e0e

19 files changed

+1537
-64
lines changed

cli.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env node
2+
3+
"use strict";
4+
5+
require("v8-compile-cache");
6+
7+
const importLocal = require("import-local");
8+
9+
// Prefer the local installation of webpack-cli
10+
if (importLocal(__filename)) {
11+
return;
12+
}
13+
process.title = "webpack";
14+
process.cliLogger = require("webpack-log")({
15+
name: "webpack"
16+
});
17+
18+
const updateNotifier = require("update-notifier");
19+
const packageJson = require("./package.json");
20+
21+
updateNotifier({ pkg: packageJson }).notify();
22+
23+
const semver = require("semver");
24+
25+
const version = packageJson.engines.node;
26+
27+
if (!semver.satisfies(process.version, version)) {
28+
const rawVersion = version.replace(/[^\d\.]*/, "");
29+
process.cliLogger.error(
30+
"webpack CLI requires at least Node v" +
31+
rawVersion +
32+
". " +
33+
"You have " +
34+
process.version +
35+
".\n" +
36+
"See https://webpack.js.org/ " +
37+
"for migration help and similar."
38+
);
39+
process.exit(1);
40+
}
41+
42+
require("./lib/bootstrap");

lib/bootstrap.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const webpackCli = require("./webpack-cli");
2+
const { core, commands } = require("./utils/cli-flags");
3+
const cmdArgs = require("command-line-args");
4+
5+
require("./utils/process-log");
6+
7+
async function runCLI(cli, commandIsUsed) {
8+
let args;
9+
if (commandIsUsed) {
10+
commandIsUsed.defaultOption = true;
11+
args = process.argv
12+
.slice(2)
13+
.filter(p => p.indexOf("--") < 0 && p !== commandIsUsed.name && p !== commandIsUsed.alias);
14+
return await cli.runCommand(commandIsUsed, ...args);
15+
} else {
16+
args = cmdArgs(core, { stopAtFirstUnknown: false });
17+
try {
18+
const result = await cli.run(args, core);
19+
if (!result) {
20+
return;
21+
}
22+
} catch (err) {
23+
process.cliLogger.error(err);
24+
process.exit(1);
25+
}
26+
}
27+
}
28+
29+
function isCommandUsed(commands) {
30+
return commands.find(cmd => {
31+
if (cmd.alias) {
32+
return process.argv.includes(cmd.name) || process.argv.includes(cmd.alias);
33+
}
34+
return process.argv.includes(cmd.name);
35+
});
36+
}
37+
(async () => {
38+
const commandIsUsed = isCommandUsed(commands);
39+
const cli = new webpackCli();
40+
runCLI(cli, commandIsUsed);
41+
})();

lib/commands/external.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const { prompt } = require("inquirer");
2+
3+
class ExternalCommand {
4+
static async runCommand(command, args = []) {
5+
const cp = require("child_process");
6+
const executedCommand = await cp.spawn(command, args, {
7+
stdio: "inherit",
8+
shell: true
9+
});
10+
return new Promise((resolve, reject) => {
11+
executedCommand.on("error", error => {
12+
reject(error);
13+
});
14+
15+
executedCommand.on("exit", code => {
16+
resolve();
17+
});
18+
});
19+
}
20+
21+
static validateEnv(extName) {
22+
let packageIsInstalled;
23+
try {
24+
const path = require("path");
25+
const pathForCmd = path.resolve(process.cwd(), "node_modules", "@webpack-cli", extName);
26+
require.resolve(pathForCmd);
27+
packageIsInstalled = pathForCmd;
28+
} catch (err) {
29+
packageIsInstalled = false;
30+
}
31+
return packageIsInstalled;
32+
}
33+
static async promptInstallation(scopeName, name) {
34+
const path = require("path");
35+
const fs = require("fs");
36+
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
37+
38+
const packageManager = isYarn ? "yarn" : "npm";
39+
const options = ["install", "-D", scopeName];
40+
41+
if (isYarn) {
42+
options[0] = "add";
43+
}
44+
45+
const commandToBeRun = `${packageManager} ${options.join(" ")}`;
46+
process.cliLogger.error(`The command moved into a separate package: ${name}`);
47+
const question = `Would you like to install ${name}? (That will run ${commandToBeRun})`;
48+
const answer = await prompt([
49+
{
50+
type: "confirm",
51+
name: "installConfirm",
52+
message: question,
53+
default: "Y",
54+
choices: ["Yes", "No", "Y", "N", "y", "n"]
55+
}
56+
]);
57+
if (answer.installConfirm === true) {
58+
await ExternalCommand.runCommand(commandToBeRun);
59+
return ExternalCommand.validateEnv(name);
60+
}
61+
process.exitCode = -1;
62+
}
63+
64+
static async run(name, ...args) {
65+
let pkgLoc = ExternalCommand.validateEnv(name);
66+
const scopeName = "@webpack-cli/" + name;
67+
if (!pkgLoc) {
68+
pkgLoc = await ExternalCommand.promptInstallation(scopeName, name);
69+
}
70+
// Serve needs to be checked for
71+
if (name === "serve") {
72+
return pkgLoc ? require(pkgLoc).serve(args) : null;
73+
}
74+
return pkgLoc ? require(pkgLoc).default(args) : null;
75+
}
76+
}
77+
78+
module.exports = ExternalCommand;

lib/groups/advanced.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const GroupHelper = require("../utils/group-helper");
2+
3+
class AdvancedGroup extends GroupHelper {
4+
constructor(options) {
5+
super(options);
6+
}
7+
loadPlugin(name) {
8+
const loadUtils = require("loader-utils");
9+
let args;
10+
try {
11+
const p = name && name.indexOf("?");
12+
if (p > -1) {
13+
args = loadUtils.parseQuery(name.substring(p));
14+
name = name.substring(0, p);
15+
}
16+
} catch (e) {
17+
console.log("Invalid plugin arguments " + name + " (" + e + ").");
18+
process.exit(-1); // eslint-disable-line
19+
}
20+
21+
let path;
22+
try {
23+
const resolve = require("enhanced-resolve");
24+
path = resolve.sync(process.cwd(), name);
25+
} catch (e) {
26+
console.log("Cannot resolve plugin " + name + ".");
27+
process.exit(-1); // eslint-disable-line
28+
}
29+
let Plugin;
30+
try {
31+
Plugin = require(path);
32+
} catch (e) {
33+
console.log("Cannot load plugin " + name + ". (" + path + ")");
34+
throw e;
35+
}
36+
try {
37+
return new Plugin(args);
38+
} catch (e) {
39+
console.log("Cannot instantiate plugin " + name + ". (" + path + ")");
40+
throw e;
41+
}
42+
}
43+
resolveOptions() {
44+
const { args } = this;
45+
if (args.hot) {
46+
const HotModuleReplacementPlugin = require("webpack").HotModuleReplacementPlugin;
47+
this.opts.options.plugins = [new HotModuleReplacementPlugin()];
48+
}
49+
if (args.debug) {
50+
const LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
51+
const loaderPluginVal = new LoaderOptionsPlugin({
52+
debug: true
53+
});
54+
55+
if (this.opts.options && this.opts.options.plugins) {
56+
this.opts.options.plugins.unshift(loaderPluginVal);
57+
} else {
58+
this.opts.options.plugins = [loaderPluginVal];
59+
}
60+
}
61+
if (args.prefetch) {
62+
const PrefetchPlugin = require("webpack").PrefetchPlugin;
63+
const prefetchVal = new PrefetchPlugin(args.prefetch);
64+
if (this.opts.options && this.opts.options.plugins) {
65+
this.opts.options.plugins.unshift(prefetchVal);
66+
} else {
67+
this.opts.options.plugins = [prefetchVal];
68+
}
69+
}
70+
if (args.plugin) {
71+
if (this.opts.options && this.opts.options.plugins) {
72+
this.opts.options.plugins.unshift(this.loadPlugin(args.plugin));
73+
} else {
74+
this.opts.options.plugins = [this.loadPlugin(args.plugin)];
75+
}
76+
}
77+
if (args.global) {
78+
let value = args.global;
79+
const idx = value.indexOf("=");
80+
let name;
81+
if (idx >= 0) {
82+
name = value.substr(0, idx);
83+
value = value.substr(idx + 1);
84+
} else {
85+
name = value;
86+
}
87+
const ProvidePlugin = require("webpack").ProvidePlugin;
88+
if (this.opts.options && this.opts.options.plugins) {
89+
this.opts.options.plugins.unshift(new ProvidePlugin(name, value));
90+
} else {
91+
this.opts.options.plugins = [this.loadPlugin(args.plugin)];
92+
}
93+
}
94+
if (args.target) {
95+
this.opts.options.target = args.target;
96+
}
97+
}
98+
run() {
99+
this.resolveOptions();
100+
return this.opts;
101+
}
102+
}
103+
104+
module.exports = AdvancedGroup;

lib/groups/basic.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const GroupHelper = require("../utils/group-helper");
2+
3+
class BasicGroup extends GroupHelper {
4+
constructor(options) {
5+
super(options);
6+
this.WEBPACK_OPTION_FLAGS = ["prod", "dev", "watch", "w", "prod", "p", "interactive", "i"];
7+
this.resolveFlags();
8+
}
9+
resolveFlags() {
10+
const { args } = this;
11+
12+
const old_args = Object.keys(args);
13+
const newArgs = old_args
14+
.map(arg => {
15+
if (arg === "entry") {
16+
const isCJS = args[arg] === null;
17+
if (isCJS) {
18+
args["dev"] = true;
19+
args["prod"] = false;
20+
} else {
21+
args["prod"] = true;
22+
args["dev"] = false;
23+
}
24+
}
25+
if (arg === "interactive") {
26+
args["interactive"] = true;
27+
}
28+
return arg;
29+
})
30+
.filter(e => e);
31+
32+
newArgs.forEach(arg => {
33+
if (this.WEBPACK_OPTION_FLAGS.includes(arg)) {
34+
this.opts.outputOptions[arg] = this.resolveWebpackOptions(arg, args[arg]);
35+
}
36+
if (arg == "analyze") {
37+
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
38+
this.opts.options.plugins = [new BundleAnalyzerPlugin()];
39+
}
40+
if (arg == "sourcemap") {
41+
this.opts.options.devtool = args[arg] || "eval";
42+
}
43+
if (arg == "entry") {
44+
if (args[arg] === "help") {
45+
return;
46+
}
47+
this.opts.options[arg] = this.resolveFilePath(args[arg], "index");
48+
}
49+
});
50+
if (this.opts.outputOptions["dev"]) {
51+
this.opts.outputOptions["prod"] = false;
52+
}
53+
}
54+
55+
resolveWebpackOptions(key, val) {
56+
return val;
57+
}
58+
run() {
59+
return this.opts;
60+
}
61+
}
62+
63+
module.exports = BasicGroup;

0 commit comments

Comments
 (0)