cli.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const start = Date.now();
  7. const argv = require('minimist')(process.argv.slice(2));
  8. // make sure to set debug flag before requiring anything
  9. if (argv.debug) {
  10. process.env.DEBUG = `vite:` + (argv.debug === true ? '*' : argv.debug);
  11. try {
  12. // this is only present during local development
  13. require('source-map-support').install();
  14. }
  15. catch (e) { }
  16. }
  17. const os_1 = __importDefault(require("os"));
  18. const path_1 = __importDefault(require("path"));
  19. const chalk_1 = __importDefault(require("chalk"));
  20. const config_1 = require("./config");
  21. const command = argv._[0];
  22. const defaultMode = command === 'build' ? 'production' : 'development';
  23. function logHelp() {
  24. console.log(`
  25. Usage: vite [command] [args] [--options]
  26. Commands:
  27. vite Start server in current directory.
  28. vite serve [root=cwd] Start server in target directory.
  29. vite build [root=cwd] Build target directory.
  30. Options:
  31. --help, -h [boolean] show help
  32. --version, -v [boolean] show version
  33. --config, -c [string] use specified config file
  34. --port [number] port to use for serve
  35. --open [boolean] open browser on server start
  36. --entry [string] entry file for build (default: index.html)
  37. --base [string] public base path for build (default: /)
  38. --outDir [string] output directory for build (default: dist)
  39. --assetsDir [string] directory under outDir to place assets in (default: assets)
  40. --assetsInlineLimit [number] static asset base64 inline threshold in bytes (default: 4096)
  41. --sourcemap [boolean] output source maps for build (default: false)
  42. --minify [boolean | 'terser' | 'esbuild'] enable/disable minification, or specify
  43. minifier to use. (default: 'terser')
  44. --mode, -m [string] specify env mode (default: 'development' for dev, 'production' for build)
  45. --ssr [boolean] build for server-side rendering
  46. --jsx ['vue' | 'preact' | 'react'] choose jsx preset (default: 'vue')
  47. --jsx-factory [string] (default: React.createElement)
  48. --jsx-fragment [string] (default: React.Fragment)
  49. --force [boolean] force the optimizer to ignore the cache and re-bundle
  50. `);
  51. }
  52. console.log(chalk_1.default.cyan(`vite v${require('../../package.json').version}`));
  53. (async () => {
  54. const { help, h, mode, m, version, v } = argv;
  55. if (help || h) {
  56. logHelp();
  57. return;
  58. }
  59. else if (version || v) {
  60. // noop, already logged
  61. return;
  62. }
  63. const envMode = mode || m || defaultMode;
  64. const options = await resolveOptions(envMode);
  65. process.env.NODE_ENV = process.env.NODE_ENV || envMode;
  66. if (!options.command || options.command === 'serve') {
  67. runServe(options);
  68. }
  69. else if (options.command === 'build') {
  70. runBuild(options);
  71. }
  72. else if (options.command === 'optimize') {
  73. runOptimize(options);
  74. }
  75. else {
  76. console.error(chalk_1.default.red(`unknown command: ${options.command}`));
  77. process.exit(1);
  78. }
  79. })();
  80. async function resolveOptions(mode) {
  81. // specify env mode
  82. argv.mode = mode;
  83. // map jsx args
  84. if (argv['jsx-factory']) {
  85. ;
  86. (argv.jsx || (argv.jsx = {})).factory = argv['jsx-factory'];
  87. }
  88. if (argv['jsx-fragment']) {
  89. ;
  90. (argv.jsx || (argv.jsx = {})).fragment = argv['jsx-fragment'];
  91. }
  92. // cast xxx=true | false into actual booleans
  93. Object.keys(argv).forEach((key) => {
  94. if (argv[key] === 'false') {
  95. argv[key] = false;
  96. }
  97. if (argv[key] === 'true') {
  98. argv[key] = true;
  99. }
  100. });
  101. // command
  102. if (argv._[0]) {
  103. argv.command = argv._[0];
  104. }
  105. // normalize root
  106. // assumes all commands are in the form of `vite [command] [root]`
  107. if (!argv.root && argv._[1]) {
  108. argv.root = argv._[1];
  109. }
  110. if (argv.root) {
  111. argv.root = path_1.default.isAbsolute(argv.root) ? argv.root : path_1.default.resolve(argv.root);
  112. }
  113. const userConfig = await config_1.resolveConfig(mode, argv.config || argv.c);
  114. if (userConfig) {
  115. return {
  116. ...userConfig,
  117. ...argv // cli options take higher priority
  118. };
  119. }
  120. // deprecation warning
  121. if (argv.sw || argv.serviceWorker) {
  122. console.warn(chalk_1.default.yellow(`[vite] service worker mode has been removed due to insufficient performance gains.`));
  123. }
  124. return argv;
  125. }
  126. function runServe(options) {
  127. const server = require('./server').createServer(options);
  128. let port = options.port || 3000;
  129. let hostname = options.hostname || 'localhost';
  130. const protocol = options.https ? 'https' : 'http';
  131. server.on('error', (e) => {
  132. if (e.code === 'EADDRINUSE') {
  133. console.log(`Port ${port} is in use, trying another one...`);
  134. setTimeout(() => {
  135. server.close();
  136. server.listen(++port);
  137. }, 100);
  138. }
  139. else {
  140. console.error(chalk_1.default.red(`[vite] server error:`));
  141. console.error(e);
  142. }
  143. });
  144. server.listen(port, () => {
  145. console.log();
  146. console.log(` Dev server running at:`);
  147. const interfaces = os_1.default.networkInterfaces();
  148. Object.keys(interfaces).forEach((key) => {
  149. ;
  150. (interfaces[key] || [])
  151. .filter((details) => details.family === 'IPv4')
  152. .map((detail) => {
  153. return {
  154. type: detail.address.includes('127.0.0.1')
  155. ? 'Local: '
  156. : 'Network: ',
  157. host: detail.address.replace('127.0.0.1', hostname)
  158. };
  159. })
  160. .forEach(({ type, host }) => {
  161. const url = `${protocol}://${host}:${chalk_1.default.bold(port)}/`;
  162. console.log(` > ${type} ${chalk_1.default.cyan(url)}`);
  163. });
  164. });
  165. console.log();
  166. require('debug')('vite:server')(`server ready in ${Date.now() - start}ms.`);
  167. if (options.open) {
  168. require('./utils/openBrowser').openBrowser(`${protocol}://${hostname}:${port}`);
  169. }
  170. });
  171. }
  172. async function runBuild(options) {
  173. try {
  174. await require('./build')[options.ssr ? 'ssrBuild' : 'build'](options);
  175. process.exit(0);
  176. }
  177. catch (err) {
  178. console.error(chalk_1.default.red(`[vite] Build errored out.`));
  179. console.error(err);
  180. process.exit(1);
  181. }
  182. }
  183. async function runOptimize(options) {
  184. try {
  185. await require('./optimizer').optimizeDeps(options, true /* as cli command */);
  186. process.exit(0);
  187. }
  188. catch (err) {
  189. console.error(chalk_1.default.red(`[vite] Dep optimization errored out.`));
  190. console.error(err);
  191. process.exit(1);
  192. }
  193. }
  194. //# sourceMappingURL=cli.js.map