serve.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.VueViteServeCLI = exports.VueServeRunner = void 0;
  27. const cli_framework_1 = require("@ionic/cli-framework");
  28. const cli_framework_output_1 = require("@ionic/cli-framework-output");
  29. const utils_network_1 = require("@ionic/utils-network");
  30. const color_1 = require("../../color");
  31. const serve_1 = require("../../serve");
  32. class VueServeRunner extends serve_1.ServeRunner {
  33. constructor(e) {
  34. super();
  35. this.e = e;
  36. }
  37. async getCommandMetadata() {
  38. return {};
  39. }
  40. modifyOpenUrl(url, _options) {
  41. return url;
  42. }
  43. async serveProject(options) {
  44. const [externalIP, availableInterfaces] = await this.selectExternalIP(options);
  45. const port = (options.port = await (0, utils_network_1.findClosestOpenPort)(options.port));
  46. const vueScripts = new VueViteServeCLI(this.e);
  47. await vueScripts.serve(options);
  48. return {
  49. custom: vueScripts.resolvedProgram !== vueScripts.program,
  50. protocol: options.https ? 'https' : 'http',
  51. localAddress: 'localhost',
  52. externalAddress: externalIP,
  53. externalNetworkInterfaces: availableInterfaces,
  54. port,
  55. externallyAccessible: ![serve_1.BIND_ALL_ADDRESS, ...serve_1.LOCAL_ADDRESSES].includes(externalIP),
  56. };
  57. }
  58. }
  59. exports.VueServeRunner = VueServeRunner;
  60. class VueViteServeCLI extends serve_1.ServeCLI {
  61. constructor() {
  62. super(...arguments);
  63. this.name = 'Vite CLI Service';
  64. this.pkg = 'vite';
  65. this.program = 'vite';
  66. this.prefix = 'vite';
  67. this.script = serve_1.SERVE_SCRIPT;
  68. this.chunks = 0;
  69. }
  70. async serve(options) {
  71. this.on('compile', (chunks) => {
  72. if (chunks > 0) {
  73. this.e.log.info(`... and ${(0, color_1.strong)(chunks.toString())} additional chunks`);
  74. }
  75. });
  76. return super.serve(options);
  77. }
  78. stdoutFilter(line) {
  79. if (this.resolvedProgram !== this.program) {
  80. return super.stdoutFilter(line);
  81. }
  82. const strippedLine = (0, cli_framework_output_1.stripAnsi)(line);
  83. const compileMsgs = [
  84. 'Compiled successfully',
  85. 'Compiled with warnings',
  86. 'Failed to compile',
  87. "ready in"
  88. ];
  89. if (compileMsgs.some((msg) => strippedLine.includes(msg))) {
  90. this.emit('ready');
  91. return false;
  92. }
  93. if (strippedLine.match(/.*chunk\s{\d+}.+/)) {
  94. this.chunks++;
  95. return false;
  96. }
  97. if (strippedLine.includes('Compiled successfully')) {
  98. this.emit('compile', this.chunks);
  99. this.chunks = 0;
  100. }
  101. return true;
  102. }
  103. stderrFilter(line) {
  104. if (this.resolvedProgram !== this.program) {
  105. return super.stderrFilter(line);
  106. }
  107. const strippedLine = (0, cli_framework_output_1.stripAnsi)(line);
  108. if (strippedLine.includes('webpack.Progress')) {
  109. return false;
  110. }
  111. return true;
  112. }
  113. async buildArgs(options) {
  114. const args = {
  115. _: [],
  116. host: options.host,
  117. port: options.port ? options.port.toString() : undefined,
  118. };
  119. const { pkgManagerArgs } = await Promise.resolve().then(() => __importStar(require('../../utils/npm')));
  120. const separatedArgs = options['--'];
  121. if (this.resolvedProgram === this.program) {
  122. return [...(0, cli_framework_1.unparseArgs)(args), ...separatedArgs];
  123. }
  124. else {
  125. const [, ...pkgArgs] = await pkgManagerArgs(this.e.config.get('npmClient'), {
  126. command: 'run',
  127. script: this.script,
  128. scriptArgs: [...(0, cli_framework_1.unparseArgs)(args), ...separatedArgs],
  129. });
  130. return pkgArgs;
  131. }
  132. }
  133. async buildEnvVars(options) {
  134. const env = {};
  135. // // Vue CLI binds to `localhost` by default, but if specified it prints a
  136. // // warning, so don't set `HOST` if the host is set to `localhost`.
  137. if (options.host !== serve_1.DEFAULT_ADDRESS) {
  138. env.HOST = options.host;
  139. }
  140. env.PORT = String(options.port);
  141. env.HTTPS = options.https ? 'true' : 'false';
  142. return { ...(await super.buildEnvVars(options)), ...env };
  143. }
  144. }
  145. exports.VueViteServeCLI = VueViteServeCLI;