|
| 1 | +/* |
| 2 | + * @adonisjs/assembler |
| 3 | + * |
| 4 | + * (c) Harminder Virk <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import execa from 'execa' |
| 11 | +import Emittery from 'emittery' |
| 12 | +import { logger as uiLogger } from '@poppinss/cliui' |
| 13 | +import { resolveDir } from '@poppinss/utils/build/helpers' |
| 14 | + |
| 15 | +/** |
| 16 | + * Assets bundler uses webpack encore to build frontend dependencies |
| 17 | + */ |
| 18 | +export class AssetsBundler extends Emittery { |
| 19 | + /** |
| 20 | + * Binary to execute |
| 21 | + */ |
| 22 | + private binaryName = 'encore' |
| 23 | + |
| 24 | + /** |
| 25 | + * Options passed to spawn a child process |
| 26 | + */ |
| 27 | + private execaOptions = { |
| 28 | + preferLocal: true, |
| 29 | + buffer: false, |
| 30 | + stdio: 'pipe' as const, |
| 31 | + localDir: this.projectRoot, |
| 32 | + cwd: this.projectRoot, |
| 33 | + env: { |
| 34 | + FORCE_COLOR: 'true', |
| 35 | + ...this.env, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + constructor( |
| 40 | + private projectRoot: string, |
| 41 | + private encoreArgs: string[] = [], |
| 42 | + private buildAssets: boolean = true, |
| 43 | + private logger: typeof uiLogger, |
| 44 | + private env: { [key: string]: string } = {} |
| 45 | + ) { |
| 46 | + super() |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Find if encore is installed |
| 51 | + */ |
| 52 | + private isEncoreInstalled() { |
| 53 | + try { |
| 54 | + resolveDir(this.projectRoot, '@symfony/webpack-encore') |
| 55 | + return true |
| 56 | + } catch { |
| 57 | + return false |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Notify user that we are about use encore |
| 63 | + */ |
| 64 | + private notifyAboutEncore() { |
| 65 | + this.logger.info(`detected { ${this.logger.colors.dim().yellow('@symfony/webpack-encore')} }`) |
| 66 | + this.logger.info( |
| 67 | + `building frontend assets. Use { ${this.logger.colors |
| 68 | + .dim() |
| 69 | + .yellow('--no-assets')} } to disable` |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Logs the line to stdout |
| 75 | + */ |
| 76 | + private log(line: Buffer | string) { |
| 77 | + line = line.toString().trim() |
| 78 | + if (!line.length) { |
| 79 | + return |
| 80 | + } |
| 81 | + console.log(`[ ${this.logger.colors.cyan('encore')} ] ${line}`) |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Logs the line to stderr |
| 86 | + */ |
| 87 | + private logError(line: Buffer | string) { |
| 88 | + line = line.toString().trim() |
| 89 | + if (!line.length) { |
| 90 | + return |
| 91 | + } |
| 92 | + console.error(`[ ${this.logger.colors.cyan('encore')} ] ${line}`) |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Execute command |
| 97 | + */ |
| 98 | + private exec(args: string[]): Promise<void> { |
| 99 | + return new Promise((resolve, reject) => { |
| 100 | + const childProcess = execa(this.binaryName, args, this.execaOptions) |
| 101 | + |
| 102 | + childProcess.stdout?.on('data', (line: Buffer) => this.log(line)) |
| 103 | + childProcess.stderr?.on('data', (line: Buffer) => this.logError(line)) |
| 104 | + childProcess.on('error', (error) => reject(error)) |
| 105 | + childProcess.on('close', (code) => { |
| 106 | + if (code && code !== 0) { |
| 107 | + reject(`Process exited with code ${code}`) |
| 108 | + } else { |
| 109 | + resolve() |
| 110 | + } |
| 111 | + }) |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Build assets using encore |
| 117 | + */ |
| 118 | + public async build(): Promise<{ hasErrors: boolean }> { |
| 119 | + if (!this.buildAssets) { |
| 120 | + return { hasErrors: false } |
| 121 | + } |
| 122 | + |
| 123 | + if (!this.isEncoreInstalled()) { |
| 124 | + return { hasErrors: false } |
| 125 | + } |
| 126 | + |
| 127 | + this.notifyAboutEncore() |
| 128 | + |
| 129 | + try { |
| 130 | + await this.exec(['dev'].concat(this.encoreArgs)) |
| 131 | + return { hasErrors: false } |
| 132 | + } catch (error) { |
| 133 | + return { hasErrors: true } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Build assets for production |
| 139 | + */ |
| 140 | + public async buildForProduction(): Promise<{ hasErrors: boolean }> { |
| 141 | + if (!this.buildAssets) { |
| 142 | + return { hasErrors: false } |
| 143 | + } |
| 144 | + |
| 145 | + if (!this.isEncoreInstalled()) { |
| 146 | + return { hasErrors: false } |
| 147 | + } |
| 148 | + |
| 149 | + this.notifyAboutEncore() |
| 150 | + |
| 151 | + try { |
| 152 | + await this.exec(['production'].concat(this.encoreArgs)) |
| 153 | + return { hasErrors: false } |
| 154 | + } catch (error) { |
| 155 | + return { hasErrors: true } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Start the webpack dev server |
| 161 | + */ |
| 162 | + public startDevServer(): { state: 'not-installed' | 'no-assets' | 'running' } { |
| 163 | + if (!this.isEncoreInstalled()) { |
| 164 | + return { state: 'not-installed' } |
| 165 | + } |
| 166 | + |
| 167 | + if (!this.buildAssets) { |
| 168 | + return { state: 'no-assets' } |
| 169 | + } |
| 170 | + |
| 171 | + const childProcess = execa( |
| 172 | + this.binaryName, |
| 173 | + ['dev-server'].concat(this.encoreArgs), |
| 174 | + this.execaOptions |
| 175 | + ) |
| 176 | + |
| 177 | + childProcess.stdout?.on('data', (line: Buffer) => this.log(line)) |
| 178 | + childProcess.stderr?.on('data', (line: Buffer) => this.logError(line)) |
| 179 | + childProcess.on('close', (code, signal) => this.emit('close', { code, signal })) |
| 180 | + childProcess.on('exit', (code, signal) => this.emit('exit', { code, signal })) |
| 181 | + |
| 182 | + return { state: 'running' } |
| 183 | + } |
| 184 | +} |
0 commit comments