You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
14 KiB

4 years ago
  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("no-cache", function() {
  386. options.cache = false;
  387. });
  388. ifBooleanArg("debug", function() {
  389. const LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
  390. addPlugin(
  391. options,
  392. new LoaderOptionsPlugin({
  393. debug: true
  394. })
  395. );
  396. });
  397. ifArg("devtool", function(value) {
  398. options.devtool = value;
  399. });
  400. function processResolveAlias(arg, key) {
  401. ifArgPair(arg, function(name, value) {
  402. if (!name) {
  403. throw new Error("--" + arg + " <string>=<string>");
  404. }
  405. ensureObject(options, key);
  406. ensureObject(options[key], "alias");
  407. options[key].alias[name] = value;
  408. });
  409. }
  410. processResolveAlias("resolve-alias", "resolve");
  411. processResolveAlias("resolve-loader-alias", "resolveLoader");
  412. ifArg("resolve-extensions", function(value) {
  413. ensureObject(options, "resolve");
  414. if (Array.isArray(value)) {
  415. options.resolve.extensions = value;
  416. } else {
  417. options.resolve.extensions = value.split(/,\s*/);
  418. }
  419. });
  420. ifArg("optimize-max-chunks", function(value) {
  421. const LimitChunkCountPlugin = require("webpack").optimize.LimitChunkCountPlugin;
  422. addPlugin(
  423. options,
  424. new LimitChunkCountPlugin({
  425. maxChunks: parseInt(value, 10)
  426. })
  427. );
  428. });
  429. ifArg("optimize-min-chunk-size", function(value) {
  430. const MinChunkSizePlugin = require("webpack").optimize.MinChunkSizePlugin;
  431. addPlugin(
  432. options,
  433. new MinChunkSizePlugin({
  434. minChunkSize: parseInt(value, 10)
  435. })
  436. );
  437. });
  438. ifBooleanArg("optimize-minimize", function() {
  439. const LoaderOptionsPlugin = require("webpack").LoaderOptionsPlugin;
  440. addPlugin(
  441. options,
  442. new LoaderOptionsPlugin({
  443. minimize: true
  444. })
  445. );
  446. });
  447. ifArg("prefetch", function(request) {
  448. const PrefetchPlugin = require("webpack").PrefetchPlugin;
  449. addPlugin(options, new PrefetchPlugin(request));
  450. });
  451. ifArg("provide", function(value) {
  452. const idx = value.indexOf("=");
  453. let name;
  454. if (idx >= 0) {
  455. name = value.substr(0, idx);
  456. value = value.substr(idx + 1);
  457. } else {
  458. name = value;
  459. }
  460. const ProvidePlugin = require("webpack").ProvidePlugin;
  461. addPlugin(options, new ProvidePlugin(name, value));
  462. });
  463. ifArg("plugin", function(value) {
  464. addPlugin(options, loadPlugin(value));
  465. });
  466. mapArgToBoolean("bail");
  467. mapArgToBoolean("profile");
  468. if (argv._.length > 0) {
  469. ensureObject(options, "entry", true);
  470. const addTo = function addTo(name, entry) {
  471. if (options.entry[name]) {
  472. if (!Array.isArray(options.entry[name])) {
  473. options.entry[name] = [options.entry[name]];
  474. }
  475. options.entry[name].push(entry);
  476. } else {
  477. options.entry[name] = entry;
  478. }
  479. };
  480. argv._.forEach(function(content) {
  481. const i = content.indexOf("=");
  482. const j = content.indexOf("?");
  483. if (i < 0 || (j >= 0 && j < i)) {
  484. const resolved = path.resolve(content);
  485. if (fs.existsSync(resolved)) {
  486. addTo("main", `${resolved}${fs.statSync(resolved).isDirectory() ? path.sep : ""}`);
  487. } else {
  488. addTo("main", content);
  489. }
  490. } else {
  491. addTo(content.substr(0, i), content.substr(i + 1));
  492. }
  493. });
  494. }
  495. }
  496. };