Skip to content

Commit

Permalink
feat: change all implementation based on latest AdonisJS
Browse files Browse the repository at this point in the history
  • Loading branch information
wahyubucil committed May 18, 2021
1 parent fcd230b commit 73c9bf9
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 140 deletions.
62 changes: 62 additions & 0 deletions commands/Mix/Base.ts
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
})
}
}
91 changes: 31 additions & 60 deletions commands/Mix/Build.ts
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)
}
}
90 changes: 36 additions & 54 deletions commands/Mix/Watch.ts
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)
}
}
9 changes: 9 additions & 0 deletions commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* 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.
*/

export default [
'adonis-mix-asset/build/commands/Mix/Build',
'adonis-mix-asset/build/commands/Mix/Watch',
Expand Down
9 changes: 9 additions & 0 deletions instructions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* 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 * as sinkStatic from '@adonisjs/sink'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { join } from 'path'
Expand Down
39 changes: 16 additions & 23 deletions providers/MixAssetProvider.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/*
* 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 { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { ViewContract } from '@ioc:Adonis/Core/View'
import { existsSync, readFileSync } from 'fs'
import { mixAsset } from '../src/mixAsset'

export default class MixAssetProvider {
constructor(protected app: ApplicationContract) {}
public static needsApplication = true

public register() {
// Register your own bindings
}

/**
* Returns the manifest file contents. During development, we make use of the
Expand All @@ -24,22 +27,12 @@ export default class MixAssetProvider {
}

public boot() {
// IoC container is ready
this.app.container.with(['Adonis/Core/View'], (view: ViewContract) => {
const manifestPath = this.app.publicPath('mix-manifest.json')
if (existsSync(manifestPath)) {
view.global('mix', (path: string) =>
mixAsset(this.app, this.getManifestContents(manifestPath), path)
)
}
})
}

public shutdown() {
// Cleanup, since app is going down
}

public ready() {
// App is ready
const View = this.app.container.resolveBinding('Adonis/Core/View')
const manifestPath = this.app.publicPath('mix-manifest.json')
if (existsSync(manifestPath)) {
View.global('mix', (path: string) =>
mixAsset(this.app, this.getManifestContents(manifestPath), path)
)
}
}
}
9 changes: 9 additions & 0 deletions setup/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*
* 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 type { Configuration } from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'

Expand Down
Loading

0 comments on commit 73c9bf9

Please # to comment.