|
| 1 | +import { ParsedArgs, unparseArgs } from '@ionic/cli-framework'; |
| 2 | +import { stripAnsi } from '@ionic/cli-framework-output'; |
| 3 | +import { findClosestOpenPort } from '@ionic/utils-network'; |
| 4 | +import { CommandMetadata, ServeDetails, ReactServeOptions, } from '../../../definitions'; |
| 5 | + |
| 6 | +import { strong } from '../../color'; |
| 7 | +import { BIND_ALL_ADDRESS, DEFAULT_ADDRESS, LOCAL_ADDRESSES, SERVE_SCRIPT, ServeCLI, ServeRunner, ServeRunnerDeps, } from '../../serve'; |
| 8 | + |
| 9 | +export class ReactViteServeRunner extends ServeRunner<ReactServeOptions> { |
| 10 | + constructor(protected readonly e: ServeRunnerDeps) { |
| 11 | + super(); |
| 12 | + } |
| 13 | + |
| 14 | + async getCommandMetadata(): Promise<Partial<CommandMetadata>> { |
| 15 | + return {}; |
| 16 | + } |
| 17 | + |
| 18 | + modifyOpenUrl(url: string, _options: ReactServeOptions): string { |
| 19 | + return url; |
| 20 | + } |
| 21 | + |
| 22 | + async serveProject(options: ReactServeOptions): Promise<ServeDetails> { |
| 23 | + const [externalIP, availableInterfaces] = await this.selectExternalIP(options); |
| 24 | + |
| 25 | + const port = (options.port = await findClosestOpenPort(options.port)); |
| 26 | + |
| 27 | + const reactScripts = new ReactViteServeCLI(this.e); |
| 28 | + await reactScripts.serve(options); |
| 29 | + |
| 30 | + return { |
| 31 | + custom: reactScripts.resolvedProgram !== reactScripts.program, |
| 32 | + protocol: options.https ? 'https' : 'http', |
| 33 | + localAddress: 'localhost', |
| 34 | + externalAddress: externalIP, |
| 35 | + externalNetworkInterfaces: availableInterfaces, |
| 36 | + port, |
| 37 | + externallyAccessible: ![BIND_ALL_ADDRESS, ...LOCAL_ADDRESSES].includes( |
| 38 | + externalIP |
| 39 | + ), |
| 40 | + }; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export class ReactViteServeCLI extends ServeCLI<ReactServeOptions> { |
| 45 | + readonly name = 'Vite CLI Service'; |
| 46 | + readonly pkg = 'vite'; |
| 47 | + readonly program = 'vite'; |
| 48 | + readonly prefix = 'vite'; |
| 49 | + readonly script = SERVE_SCRIPT; |
| 50 | + protected chunks = 0; |
| 51 | + |
| 52 | + async serve(options: ReactServeOptions): Promise<void> { |
| 53 | + this.on('compile', (chunks) => { |
| 54 | + if (chunks > 0) { |
| 55 | + this.e.log.info( |
| 56 | + `... and ${strong(chunks.toString())} additional chunks` |
| 57 | + ); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + return super.serve(options); |
| 62 | + } |
| 63 | + |
| 64 | + protected stdoutFilter(line: string): boolean { |
| 65 | + if (this.resolvedProgram !== this.program) { |
| 66 | + return super.stdoutFilter(line); |
| 67 | + } |
| 68 | + const strippedLine = stripAnsi(line); |
| 69 | + const compileMsgs = [ |
| 70 | + 'Compiled successfully', |
| 71 | + 'Compiled with warnings', |
| 72 | + 'Failed to compile', |
| 73 | + "ready in" |
| 74 | + ]; |
| 75 | + if (compileMsgs.some((msg) => strippedLine.includes(msg))) { |
| 76 | + this.emit('ready'); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + if (strippedLine.match(/.*chunk\s{\d+}.+/)) { |
| 81 | + this.chunks++; |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + if (strippedLine.includes('Compiled successfully')) { |
| 86 | + this.emit('compile', this.chunks); |
| 87 | + this.chunks = 0; |
| 88 | + } |
| 89 | + |
| 90 | + if(strippedLine.includes('has unexpectedly closed')) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + protected stderrFilter(line: string): boolean { |
| 97 | + if (this.resolvedProgram !== this.program) { |
| 98 | + return super.stderrFilter(line); |
| 99 | + } |
| 100 | + const strippedLine = stripAnsi(line); |
| 101 | + if (strippedLine.includes('webpack.Progress')) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + if(strippedLine.includes('has unexpectedly closed')) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + protected async buildArgs(options: ReactServeOptions): Promise<string[]> { |
| 112 | + const args: ParsedArgs = { |
| 113 | + _: [], |
| 114 | + host: options.host, |
| 115 | + port: options.port ? options.port.toString() : undefined, |
| 116 | + }; |
| 117 | + const { pkgManagerArgs } = await import('../../utils/npm'); |
| 118 | + |
| 119 | + const separatedArgs = options['--']; |
| 120 | + |
| 121 | + if (this.resolvedProgram === this.program) { |
| 122 | + return [...unparseArgs(args), ...separatedArgs]; |
| 123 | + } else { |
| 124 | + const [, ...pkgArgs] = await pkgManagerArgs( |
| 125 | + this.e.config.get('npmClient'), |
| 126 | + { |
| 127 | + command: 'run', |
| 128 | + script: this.script, |
| 129 | + scriptArgs: [...unparseArgs(args), ...separatedArgs], |
| 130 | + } |
| 131 | + ); |
| 132 | + return pkgArgs; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected async buildEnvVars( |
| 137 | + options: ReactServeOptions |
| 138 | + ): Promise<NodeJS.ProcessEnv> { |
| 139 | + const env: NodeJS.ProcessEnv = {}; |
| 140 | + // // Vite binds to `localhost` by default, but if specified it prints a |
| 141 | + // // warning, so don't set `HOST` if the host is set to `localhost`. |
| 142 | + if (options.host !== DEFAULT_ADDRESS) { |
| 143 | + env.HOST = options.host; |
| 144 | + } |
| 145 | + |
| 146 | + env.PORT = String(options.port); |
| 147 | + |
| 148 | + env.HTTPS = options.https ? 'true' : 'false'; |
| 149 | + |
| 150 | + return { ...(await super.buildEnvVars(options)), ...env }; |
| 151 | + } |
| 152 | +} |
0 commit comments