convert-argv.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. var path = require("path");
  2. var fs = require("fs");
  3. fs.existsSync = fs.existsSync || path.existsSync;
  4. var interpret = require("interpret");
  5. var prepareOptions = require("../lib/prepareOptions");
  6. module.exports = function(yargs, argv, convertOptions) {
  7. var options = [];
  8. // Shortcuts
  9. if(argv.d) {
  10. argv.debug = true;
  11. argv["output-pathinfo"] = true;
  12. if(!argv.devtool) {
  13. argv.devtool = "eval-cheap-module-source-map";
  14. }
  15. }
  16. if(argv.p) {
  17. argv["optimize-minimize"] = true;
  18. argv["define"] = [].concat(argv["define"] || []).concat("process.env.NODE_ENV=\"production\"");
  19. }
  20. var configFileLoaded = false;
  21. var configFiles = [];
  22. var extensions = Object.keys(interpret.extensions).sort(function(a, b) {
  23. return a === ".js" ? -1 : b === ".js" ? 1 : a.length - b.length;
  24. });
  25. var defaultConfigFiles = ["webpack.config", "webpackfile"].map(function(filename) {
  26. return extensions.map(function(ext) {
  27. return {
  28. path: path.resolve(filename + ext),
  29. ext: ext
  30. };
  31. });
  32. }).reduce(function(a, i) {
  33. return a.concat(i);
  34. }, []);
  35. var i;
  36. if(argv.config) {
  37. var getConfigExtension = function getConfigExtension(configPath) {
  38. for(i = extensions.length - 1; i >= 0; i--) {
  39. var tmpExt = extensions[i];
  40. if(configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1) {
  41. return tmpExt;
  42. }
  43. }
  44. return path.extname(configPath);
  45. };
  46. var mapConfigArg = function mapConfigArg(configArg) {
  47. var resolvedPath = path.resolve(configArg);
  48. var extension = getConfigExtension(resolvedPath);
  49. return {
  50. path: resolvedPath,
  51. ext: extension
  52. };
  53. };
  54. var configArgList = Array.isArray(argv.config) ? argv.config : [argv.config];
  55. configFiles = configArgList.map(mapConfigArg);
  56. } else {
  57. for(i = 0; i < defaultConfigFiles.length; i++) {
  58. var webpackConfig = defaultConfigFiles[i].path;
  59. if(fs.existsSync(webpackConfig)) {
  60. configFiles.push({
  61. path: webpackConfig,
  62. ext: defaultConfigFiles[i].ext
  63. });
  64. break;
  65. }
  66. }
  67. }
  68. if(configFiles.length > 0) {
  69. var registerCompiler = function registerCompiler(moduleDescriptor) {
  70. if(moduleDescriptor) {
  71. if(typeof moduleDescriptor === "string") {
  72. require(moduleDescriptor);
  73. } else if(!Array.isArray(moduleDescriptor)) {
  74. moduleDescriptor.register(require(moduleDescriptor.module));
  75. } else {
  76. for(var i = 0; i < moduleDescriptor.length; i++) {
  77. try {
  78. registerCompiler(moduleDescriptor[i]);
  79. break;
  80. } catch(e) {
  81. // do nothing
  82. }
  83. }
  84. }
  85. }
  86. };
  87. var requireConfig = function requireConfig(configPath) {
  88. var options = require(configPath);
  89. options = prepareOptions(options, argv);
  90. return options;
  91. };
  92. configFiles.forEach(function(file) {
  93. registerCompiler(interpret.extensions[file.ext]);
  94. options.push(requireConfig(file.path));
  95. });
  96. configFileLoaded = true;
  97. }
  98. if(!configFileLoaded) {
  99. return processConfiguredOptions({});
  100. } else if(options.length === 1) {
  101. return processConfiguredOptions(options[0]);
  102. } else {
  103. return processConfiguredOptions(options);
  104. }
  105. function processConfiguredOptions(options) {
  106. if(options === null || typeof options !== "object") {
  107. console.error("Config did not export an object or a function returning an object.");
  108. process.exit(-1); // eslint-disable-line
  109. }
  110. // process Promise
  111. if(typeof options.then === "function") {
  112. return options.then(processConfiguredOptions);
  113. }
  114. // process ES6 default
  115. if(typeof options === "object" && typeof options.default === "object") {
  116. return processConfiguredOptions(options.default);
  117. }
  118. // filter multi-config by name
  119. if(Array.isArray(options) && argv["config-name"]) {
  120. var namedOptions = options.filter(function(opt) {
  121. return opt.name === argv["config-name"];
  122. });
  123. if(namedOptions.length === 0) {
  124. console.error("Configuration with name '" + argv["config-name"] + "' was not found.");
  125. process.exit(-1); // eslint-disable-line
  126. } else if(namedOptions.length === 1) {
  127. return processConfiguredOptions(namedOptions[0]);
  128. }
  129. options = namedOptions;
  130. }
  131. if(Array.isArray(options)) {
  132. options.forEach(processOptions);
  133. } else {
  134. processOptions(options);
  135. }
  136. if(argv.context) {
  137. options.context = path.resolve(argv.context);
  138. }
  139. if(!options.context) {
  140. options.context = process.cwd();
  141. }
  142. if(argv.watch) {
  143. options.watch = true;
  144. }
  145. if(argv["watch-aggregate-timeout"]) {
  146. options.watchOptions = options.watchOptions || {};
  147. options.watchOptions.aggregateTimeout = +argv["watch-aggregate-timeout"];
  148. }
  149. if(typeof argv["watch-poll"] !== "undefined") {
  150. options.watchOptions = options.watchOptions || {};
  151. if(argv["watch-poll"] === "true" || argv["watch-poll"] === "")
  152. options.watchOptions.poll = true;
  153. else if(!isNaN(argv["watch-poll"]))
  154. options.watchOptions.poll = +argv["watch-poll"];
  155. }
  156. if(argv["watch-stdin"]) {
  157. options.watchOptions = options.watchOptions || {};
  158. options.watchOptions.stdin = true;
  159. options.watch = true;
  160. }
  161. return options;
  162. }
  163. function processOptions(options) {
  164. var noOutputFilenameDefined = !options.output || !options.output.filename;
  165. function ifArg(name, fn, init, finalize) {
  166. if(Array.isArray(argv[name])) {
  167. if(init) {
  168. init();
  169. }
  170. argv[name].forEach(fn);
  171. if(finalize) {
  172. finalize();
  173. }
  174. } else if(typeof argv[name] !== "undefined" && argv[name] !== null) {
  175. if(init) {
  176. init();
  177. }
  178. fn(argv[name], -1);
  179. if(finalize) {
  180. finalize();
  181. }
  182. }
  183. }
  184. function ifArgPair(name, fn, init, finalize) {
  185. ifArg(name, function(content, idx) {
  186. var i = content.indexOf("=");
  187. if(i < 0) {
  188. return fn(null, content, idx);
  189. } else {
  190. return fn(content.substr(0, i), content.substr(i + 1), idx);
  191. }
  192. }, init, finalize);
  193. }
  194. function ifBooleanArg(name, fn) {
  195. ifArg(name, function(bool) {
  196. if(bool) {
  197. fn();
  198. }
  199. });
  200. }
  201. function mapArgToBoolean(name, optionName) {
  202. ifArg(name, function(bool) {
  203. if(bool === true)
  204. options[optionName || name] = true;
  205. else if(bool === false)
  206. options[optionName || name] = false;
  207. });
  208. }
  209. function loadPlugin(name) {
  210. var loadUtils = require("loader-utils");
  211. var args;
  212. try {
  213. var p = name && name.indexOf("?");
  214. if(p > -1) {
  215. args = loadUtils.parseQuery(name.substring(p));
  216. name = name.substring(0, p);
  217. }
  218. } catch(e) {
  219. console.log("Invalid plugin arguments " + name + " (" + e + ").");
  220. process.exit(-1); // eslint-disable-line
  221. }
  222. var path;
  223. try {
  224. var resolve = require("enhanced-resolve");
  225. path = resolve.sync(process.cwd(), name);
  226. } catch(e) {
  227. console.log("Cannot resolve plugin " + name + ".");
  228. process.exit(-1); // eslint-disable-line
  229. }
  230. var Plugin;
  231. try {
  232. Plugin = require(path);
  233. } catch(e) {
  234. console.log("Cannot load plugin " + name + ". (" + path + ")");
  235. throw e;
  236. }
  237. try {
  238. return new Plugin(args);
  239. } catch(e) {
  240. console.log("Cannot instantiate plugin " + name + ". (" + path + ")");
  241. throw e;
  242. }
  243. }
  244. function ensureObject(parent, name) {
  245. if(typeof parent[name] !== "object" || parent[name] === null) {
  246. parent[name] = {};
  247. }
  248. }
  249. function ensureArray(parent, name) {
  250. if(!Array.isArray(parent[name])) {
  251. parent[name] = [];
  252. }
  253. }
  254. ifArgPair("entry", function(name, entry) {
  255. if(typeof options.entry[name] !== "undefined" && options.entry[name] !== null) {
  256. options.entry[name] = [].concat(options.entry[name]).concat(entry);
  257. } else {
  258. options.entry[name] = entry;
  259. }
  260. }, function() {
  261. ensureObject(options, "entry");
  262. });
  263. function bindRules(arg) {
  264. ifArgPair(arg, function(name, binding) {
  265. if(name === null) {
  266. name = binding;
  267. binding += "-loader";
  268. }
  269. var rule = {
  270. test: new RegExp("\\." + name.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$"), // eslint-disable-line no-useless-escape
  271. loader: binding
  272. };
  273. if(arg === "module-bind-pre") {
  274. rule.enforce = "pre";
  275. } else if(arg === "module-bind-post") {
  276. rule.enforce = "post";
  277. }
  278. options.module.rules.push(rule);
  279. }, function() {
  280. ensureObject(options, "module");
  281. ensureArray(options.module, "rules");
  282. });
  283. }
  284. bindRules("module-bind");
  285. bindRules("module-bind-pre");
  286. bindRules("module-bind-post");
  287. var defineObject;
  288. ifArgPair("define", function(name, value) {
  289. if(name === null) {
  290. name = value;
  291. value = true;
  292. }
  293. defineObject[name] = value;
  294. }, function() {
  295. defineObject = {};
  296. }, function() {
  297. ensureArray(options, "plugins");
  298. var DefinePlugin = require("../lib/DefinePlugin");
  299. options.plugins.push(new DefinePlugin(defineObject));
  300. });
  301. ifArg("output-path", function(value) {
  302. ensureObject(options, "output");
  303. options.output.path = path.resolve(value);
  304. });
  305. ifArg("output-filename", function(value) {
  306. ensureObject(options, "output");
  307. options.output.filename = value;
  308. noOutputFilenameDefined = false;
  309. });
  310. ifArg("output-chunk-filename", function(value) {
  311. ensureObject(options, "output");
  312. options.output.chunkFilename = value;
  313. });
  314. ifArg("output-source-map-filename", function(value) {
  315. ensureObject(options, "output");
  316. options.output.sourceMapFilename = value;
  317. });
  318. ifArg("output-public-path", function(value) {
  319. ensureObject(options, "output");
  320. options.output.publicPath = value;
  321. });
  322. ifArg("output-jsonp-function", function(value) {
  323. ensureObject(options, "output");
  324. options.output.jsonpFunction = value;
  325. });
  326. ifBooleanArg("output-pathinfo", function() {
  327. ensureObject(options, "output");
  328. options.output.pathinfo = true;
  329. });
  330. ifArg("output-library", function(value) {
  331. ensureObject(options, "output");
  332. options.output.library = value;
  333. });
  334. ifArg("output-library-target", function(value) {
  335. ensureObject(options, "output");
  336. options.output.libraryTarget = value;
  337. });
  338. ifArg("records-input-path", function(value) {
  339. options.recordsInputPath = path.resolve(value);
  340. });
  341. ifArg("records-output-path", function(value) {
  342. options.recordsOutputPath = path.resolve(value);
  343. });
  344. ifArg("records-path", function(value) {
  345. options.recordsPath = path.resolve(value);
  346. });
  347. ifArg("target", function(value) {
  348. options.target = value;
  349. });
  350. mapArgToBoolean("cache");
  351. ifBooleanArg("hot", function() {
  352. ensureArray(options, "plugins");
  353. var HotModuleReplacementPlugin = require("../lib/HotModuleReplacementPlugin");
  354. options.plugins.push(new HotModuleReplacementPlugin());
  355. });
  356. ifBooleanArg("debug", function() {
  357. ensureArray(options, "plugins");
  358. var LoaderOptionsPlugin = require("../lib/LoaderOptionsPlugin");
  359. options.plugins.push(new LoaderOptionsPlugin({
  360. debug: true
  361. }));
  362. });
  363. ifArg("devtool", function(value) {
  364. options.devtool = value;
  365. });
  366. function processResolveAlias(arg, key) {
  367. ifArgPair(arg, function(name, value) {
  368. if(!name) {
  369. throw new Error("--" + arg + " <string>=<string>");
  370. }
  371. ensureObject(options, key);
  372. ensureObject(options[key], "alias");
  373. options[key].alias[name] = value;
  374. });
  375. }
  376. processResolveAlias("resolve-alias", "resolve");
  377. processResolveAlias("resolve-loader-alias", "resolveLoader");
  378. ifArg("resolve-extensions", function(value) {
  379. ensureObject(options, "resolve");
  380. if(Array.isArray(value)) {
  381. options.resolve.extensions = value;
  382. } else {
  383. options.resolve.extensions = value.split(/,\s*/);
  384. }
  385. });
  386. ifArg("optimize-max-chunks", function(value) {
  387. ensureArray(options, "plugins");
  388. var LimitChunkCountPlugin = require("../lib/optimize/LimitChunkCountPlugin");
  389. options.plugins.push(new LimitChunkCountPlugin({
  390. maxChunks: parseInt(value, 10)
  391. }));
  392. });
  393. ifArg("optimize-min-chunk-size", function(value) {
  394. ensureArray(options, "plugins");
  395. var MinChunkSizePlugin = require("../lib/optimize/MinChunkSizePlugin");
  396. options.plugins.push(new MinChunkSizePlugin({
  397. minChunkSize: parseInt(value, 10)
  398. }));
  399. });
  400. ifBooleanArg("optimize-minimize", function() {
  401. ensureArray(options, "plugins");
  402. var UglifyJsPlugin = require("../lib/optimize/UglifyJsPlugin");
  403. var LoaderOptionsPlugin = require("../lib/LoaderOptionsPlugin");
  404. options.plugins.push(new UglifyJsPlugin({
  405. sourceMap: options.devtool && (options.devtool.indexOf("sourcemap") >= 0 || options.devtool.indexOf("source-map") >= 0)
  406. }));
  407. options.plugins.push(new LoaderOptionsPlugin({
  408. minimize: true
  409. }));
  410. });
  411. ifArg("prefetch", function(request) {
  412. ensureArray(options, "plugins");
  413. var PrefetchPlugin = require("../lib/PrefetchPlugin");
  414. options.plugins.push(new PrefetchPlugin(request));
  415. });
  416. ifArg("provide", function(value) {
  417. ensureArray(options, "plugins");
  418. var idx = value.indexOf("=");
  419. var name;
  420. if(idx >= 0) {
  421. name = value.substr(0, idx);
  422. value = value.substr(idx + 1);
  423. } else {
  424. name = value;
  425. }
  426. var ProvidePlugin = require("../lib/ProvidePlugin");
  427. options.plugins.push(new ProvidePlugin(name, value));
  428. });
  429. ifArg("plugin", function(value) {
  430. ensureArray(options, "plugins");
  431. options.plugins.push(loadPlugin(value));
  432. });
  433. mapArgToBoolean("bail");
  434. mapArgToBoolean("profile");
  435. if(noOutputFilenameDefined) {
  436. ensureObject(options, "output");
  437. if(convertOptions && convertOptions.outputFilename) {
  438. options.output.path = path.resolve(path.dirname(convertOptions.outputFilename));
  439. options.output.filename = path.basename(convertOptions.outputFilename);
  440. } else if(argv._.length > 0) {
  441. options.output.filename = argv._.pop();
  442. options.output.path = path.resolve(path.dirname(options.output.filename));
  443. options.output.filename = path.basename(options.output.filename);
  444. } else if(configFileLoaded) {
  445. throw new Error("'output.filename' is required, either in config file or as --output-filename");
  446. } else {
  447. console.error("No configuration file found and no output filename configured via CLI option.");
  448. console.error("A configuration file could be named 'webpack.config.js' in the current directory.");
  449. console.error("Use --help to display the CLI options.");
  450. process.exit(-1); // eslint-disable-line
  451. }
  452. }
  453. if(argv._.length > 0) {
  454. if(Array.isArray(options.entry) || typeof options.entry === "string") {
  455. options.entry = {
  456. main: options.entry
  457. };
  458. }
  459. ensureObject(options, "entry");
  460. var addTo = function addTo(name, entry) {
  461. if(options.entry[name]) {
  462. if(!Array.isArray(options.entry[name])) {
  463. options.entry[name] = [options.entry[name]];
  464. }
  465. options.entry[name].push(entry);
  466. } else {
  467. options.entry[name] = entry;
  468. }
  469. };
  470. argv._.forEach(function(content) {
  471. var i = content.indexOf("=");
  472. var j = content.indexOf("?");
  473. if(i < 0 || (j >= 0 && j < i)) {
  474. var resolved = path.resolve(content);
  475. if(fs.existsSync(resolved)) {
  476. addTo("main", `${resolved}${fs.statSync(resolved).isDirectory() ? path.sep : ""}`);
  477. } else {
  478. addTo("main", content);
  479. }
  480. } else {
  481. addTo(content.substr(0, i), content.substr(i + 1));
  482. }
  483. });
  484. }
  485. if(!options.entry) {
  486. if(configFileLoaded) {
  487. console.error("Configuration file found but no entry configured.");
  488. } else {
  489. console.error("No configuration file found and no entry configured via CLI option.");
  490. console.error("When using the CLI you need to provide at least two arguments: entry and output.");
  491. console.error("A configuration file could be named 'webpack.config.js' in the current directory.");
  492. }
  493. console.error("Use --help to display the CLI options.");
  494. process.exit(-1); // eslint-disable-line
  495. }
  496. }
  497. };