Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

convert-argv.js 14KB

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