-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change all implementation based on latest AdonisJS
- Loading branch information
1 parent
fcd230b
commit 73c9bf9
Showing
9 changed files
with
184 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* adonis-mix-asset | ||
* | ||
* (c) Wahyu Budi Saputra <wahyubucil@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { BaseCommand } from '@adonisjs/ace' | ||
import { flags } from '@adonisjs/core/build/standalone' | ||
import { spawn } from 'child_process' | ||
|
||
/** | ||
* Base class to provide helpers for Mix commands | ||
*/ | ||
export abstract class BaseMix extends BaseCommand { | ||
@flags.string({ | ||
description: "The path to your Mix configuration file. Default: 'webpack.mix.js'", | ||
}) | ||
public mixConfig = 'webpack.mix.js' | ||
|
||
@flags.boolean({ description: 'Enable progress reporting. Default: true' }) | ||
public progress = true | ||
|
||
protected get isTesting() { | ||
return process.env.TESTING | ||
} | ||
|
||
protected get isTTY() { | ||
if (this.isTesting && process.env.IS_TTY !== undefined) { | ||
return process.env.IS_TTY === 'true' | ||
} | ||
|
||
if (this.isTesting && process.stdout.isTTY === undefined) { | ||
return true | ||
} | ||
|
||
return process.stdout.isTTY | ||
} | ||
|
||
protected runScript(script: string, scriptEnv: NodeJS.ProcessEnv) { | ||
if (this.isTesting) { | ||
process.stdout.write(JSON.stringify({ script, env: scriptEnv })) | ||
return | ||
} | ||
|
||
const child = spawn(script, { | ||
stdio: 'inherit', | ||
shell: true, | ||
env: { ...process.env, ...scriptEnv }, | ||
}) | ||
|
||
child.on('exit', (code, signal) => { | ||
if (code === null) { | ||
code = signal === 'SIGINT' ? 130 : 1 | ||
} | ||
|
||
process.exitCode = code | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,58 @@ | ||
import { BaseCommand, flags } from '@adonisjs/core/build/standalone' | ||
import { spawn } from 'child_process' | ||
/* | ||
* adonis-mix-asset | ||
* | ||
* (c) Wahyu Budi Saputra <wahyubucil@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { flags } from '@adonisjs/core/build/standalone' | ||
import { existsSync } from 'fs' | ||
import { join } from 'path' | ||
|
||
export default class MixBuild extends BaseCommand { | ||
import { relative } from 'path' | ||
import { BaseMix } from './Base' | ||
|
||
/** | ||
* Command to build assets | ||
* | ||
* Reference: https://github.com/JeffreyWay/laravel-mix/blob/8cfacdde47/bin/cli.js | ||
*/ | ||
export default class MixBuild extends BaseMix { | ||
public static commandName = 'mix:build' | ||
public static description = 'Compile Mix' | ||
public static settings = { | ||
stayAlive: true, | ||
} | ||
|
||
@flags.boolean({ description: 'Build assets for production', default: false }) | ||
@flags.boolean({ description: 'Build assets for production' }) | ||
public production: boolean | ||
|
||
@flags.boolean({ | ||
description: 'Open bundle analyzer', | ||
default: false, | ||
}) | ||
@flags.boolean({ description: 'Open bundle analyzer' }) | ||
public analyze: boolean | ||
|
||
@flags.string({ | ||
description: | ||
"The path to your Mix configuration file. The default is your root 'webpack.mix.js'", | ||
default: 'webpack.mix.js', | ||
}) | ||
public mixConfig: string | ||
|
||
public async run() { | ||
let webpackConfigPath = require.resolve('laravel-mix/setup/webpack.config.js') | ||
if (!existsSync(webpackConfigPath)) { | ||
this.logger.error('Please install Laravel Mix') | ||
return | ||
} | ||
|
||
if (this.analyze) { | ||
webpackConfigPath = require.resolve('../../setup/webpack.config.js') | ||
} | ||
|
||
const mixConfigPath = join(this.application.cliCwd!, this.mixConfig) | ||
const mixConfigPath = this.application.makePath(this.mixConfig) | ||
if (!existsSync(mixConfigPath)) { | ||
this.logger.error(`The Mix configuration file '${this.mixConfig}' is not found`) | ||
return | ||
} | ||
|
||
let commandScript: string | ||
if (this.isTTY()) commandScript = 'npx webpack --progress' | ||
if (this.isTTY && this.progress) commandScript = 'npx webpack --progress' | ||
else commandScript = 'npx webpack' | ||
|
||
const script = [ | ||
`npx cross-env NODE_ENV=${this.production ? 'production' : 'development'}`, | ||
`MIX_FILE=${this.mixConfig}`, | ||
commandScript, | ||
`--config=${webpackConfigPath}`, | ||
].join(' ') | ||
let configPath = 'laravel-mix/setup/webpack.config.js' | ||
if (this.analyze) configPath = '../../setup/webpack.config.js' | ||
|
||
if (this.isTesting()) { | ||
process.stdout.write(script) | ||
return | ||
} | ||
|
||
const child = spawn(script, { | ||
stdio: 'inherit', | ||
shell: true, | ||
}) | ||
|
||
child.on('exit', (code) => { | ||
if (code) process.exitCode = code | ||
}) | ||
} | ||
const webpackConfigPath = relative(this.application.appRoot, require.resolve(configPath)) | ||
|
||
private isTesting() { | ||
return process.env.TESTING | ||
} | ||
|
||
private isTTY() { | ||
if (this.isTesting() && process.env.IS_TTY !== undefined) { | ||
return process.env.IS_TTY === 'true' | ||
} | ||
const script = [commandScript, `--config="${webpackConfigPath}"`].join(' ') | ||
|
||
if (this.isTesting() && process.stdout.isTTY === undefined) { | ||
return true | ||
const scriptEnv = { | ||
NODE_ENV: this.production ? 'production' : 'development', | ||
MIX_FILE: this.mixConfig, | ||
} | ||
|
||
return process.stdout.isTTY | ||
this.runScript(script, scriptEnv) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,62 @@ | ||
import { BaseCommand, flags } from '@adonisjs/core/build/standalone' | ||
import { spawn } from 'child_process' | ||
/* | ||
* adonis-mix-asset | ||
* | ||
* (c) Wahyu Budi Saputra <wahyubucil@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { flags } from '@adonisjs/core/build/standalone' | ||
import { existsSync } from 'fs' | ||
import { join } from 'path' | ||
|
||
export default class MixWatch extends BaseCommand { | ||
import { relative } from 'path' | ||
import { BaseMix } from './Base' | ||
|
||
/** | ||
* Command to watch assets | ||
* | ||
* Reference: https://github.com/JeffreyWay/laravel-mix/blob/8cfacdde47/bin/cli.js | ||
*/ | ||
export default class MixWatch extends BaseMix { | ||
public static commandName = 'mix:watch' | ||
public static description = 'Build and watch files for changes' | ||
public static settings = { | ||
stayAlive: true, | ||
} | ||
|
||
@flags.boolean({ description: 'Enable hot reloading', default: false }) | ||
@flags.boolean({ description: 'Enable hot reloading' }) | ||
public hot: boolean | ||
|
||
@flags.string({ | ||
description: | ||
"The path to your Mix configuration file. The default is your root 'webpack.mix.js'", | ||
default: 'webpack.mix.js', | ||
}) | ||
public mixConfig: string | ||
@flags.boolean({ description: 'Enable https' }) | ||
public https: boolean | ||
|
||
public async run() { | ||
const webpackConfigPath = require.resolve('laravel-mix/setup/webpack.config.js') | ||
if (!existsSync(webpackConfigPath)) { | ||
this.logger.error('Please install Laravel Mix') | ||
return | ||
} | ||
|
||
const mixConfigPath = join(this.application.cliCwd!, this.mixConfig) | ||
const mixConfigPath = this.application.makePath(this.mixConfig) | ||
if (!existsSync(mixConfigPath)) { | ||
this.logger.error(`The Mix configuration file '${this.mixConfig}' is not found`) | ||
return | ||
} | ||
|
||
let commandScript: string | ||
if (this.hot) commandScript = 'npx webpack serve --hot' | ||
else { | ||
if (this.isTTY()) commandScript = 'npx webpack --progress --watch' | ||
if (!this.hot) { | ||
if (this.isTTY && this.progress) commandScript = 'npx webpack --progress --watch' | ||
else commandScript = 'npx webpack --watch' | ||
} else { | ||
commandScript = 'npx webpack serve --hot' + (this.https ? ' --https' : '') | ||
} | ||
|
||
const script = [ | ||
'npx cross-env NODE_ENV=development', | ||
`MIX_FILE=${this.mixConfig}`, | ||
commandScript, | ||
`--config=${webpackConfigPath}`, | ||
].join(' ') | ||
|
||
if (this.isTesting()) { | ||
process.stdout.write(script) | ||
return | ||
} | ||
const webpackConfigPath = relative( | ||
this.application.appRoot, | ||
require.resolve('laravel-mix/setup/webpack.config.js') | ||
) | ||
|
||
const child = spawn(script, { | ||
stdio: 'inherit', | ||
shell: true, | ||
}) | ||
|
||
child.on('exit', (code) => { | ||
if (code) process.exitCode = code | ||
}) | ||
} | ||
|
||
private isTesting() { | ||
return process.env.TESTING | ||
} | ||
|
||
private isTTY() { | ||
if (this.isTesting() && process.env.IS_TTY !== undefined) { | ||
return process.env.IS_TTY === 'true' | ||
} | ||
const script = [commandScript, `--config="${webpackConfigPath}"`].join(' ') | ||
|
||
if (this.isTesting() && process.stdout.isTTY === undefined) { | ||
return true | ||
const scriptEnv = { | ||
NODE_ENV: 'development', | ||
MIX_FILE: this.mixConfig, | ||
} | ||
|
||
return process.stdout.isTTY | ||
this.runScript(script, scriptEnv) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.