Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hooks for build and dev server #4428

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export default class Build extends BaseCommand {
const bundler = new assembler.Bundler(this.app.appRoot, ts, {
assets: await this.#getAssetsBundlerConfig(),
metaFiles: this.app.rcFile.metaFiles,
hooks: {
onBuildStarting: this.app.rcFile.unstable_assembler?.onBuildStarting,
onBuildCompleted: this.app.rcFile.unstable_assembler?.onBuildCompleted,
},
})

/**
Expand Down
4 changes: 4 additions & 0 deletions commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export default class Serve extends BaseCommand {
scriptArgs: [],
assets: await this.#getAssetsBundlerConfig(),
metaFiles: this.app.rcFile.metaFiles,
hooks: {
onDevServerStarted: this.app.rcFile.unstable_assembler?.onDevServerStarted,
onSourceFileChanged: this.app.rcFile.unstable_assembler?.onSourceFileChanged,
},
})

/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"index:commands": "node --loader=ts-node/esm toolkit/main.js index build/commands"
},
"devDependencies": {
"@adonisjs/assembler": "^7.1.0",
"@adonisjs/assembler": "^7.2.1",
"@adonisjs/eslint-config": "^1.2.0",
"@adonisjs/prettier-config": "^1.2.0",
"@adonisjs/tsconfig": "^1.2.0",
Expand Down Expand Up @@ -117,7 +117,7 @@
},
"dependencies": {
"@adonisjs/ace": "^13.0.0",
"@adonisjs/application": "^8.0.2",
"@adonisjs/application": "^8.1.0",
"@adonisjs/bodyparser": "^10.0.1",
"@adonisjs/config": "^5.0.1",
"@adonisjs/encryption": "^6.0.1",
Expand Down
50 changes: 50 additions & 0 deletions tests/commands/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,54 @@ test.group('Build command', (group) => {
})
)
})

test('correctly pass hooks to the bundler', async ({ assert, fs }) => {
assert.plan(2)

await fs.create(
'tsconfig.json',
JSON.stringify({
include: ['**/*'],
exclude: [],
compilerOptions: {
target: 'ESNext',
module: 'NodeNext',
lib: ['ESNext'],
strict: true,
noUnusedLocals: true,
},
})
)

await fs.create('adonisrc.ts', `export default {}`)
await fs.create('index.ts', '')

const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => {
return import(filePath)
},
})

ace.app.rcFile.unstable_assembler = {
onBuildCompleted: [
async () => ({
default: async () => {
assert.isTrue(true)
},
}),
],
onBuildStarting: [
async () => ({
default: async () => {
assert.isTrue(true)
},
}),
],
}

ace.ui.switchMode('normal')

const command = await ace.create(Build, [])
await command.exec()
})
})
44 changes: 36 additions & 8 deletions tests/commands/serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,7 @@ test.group('Serve command', () => {
)
})

test('do not attempt to serve assets when --no-assets flag is used', async ({
assert,
fs,
cleanup,
}) => {
test('do not attempt to serve assets when --no-assets flag is used', async ({ fs, cleanup }) => {
await fs.create('bin/server.js', '')
await fs.create(
'node_modules/ts-node/package.json',
Expand Down Expand Up @@ -257,11 +253,43 @@ test.group('Serve command', () => {
cleanup(() => command.devServer.close())
await command.exec()
await sleep(600)
})

assert.notExists(
ace.ui.logger.getLogs().find((log) => {
return log.message.match(/starting "vite" dev server/)
test('correctly pass hooks to the DevServer', async ({ assert, fs, cleanup }) => {
assert.plan(1)

await fs.create(
'bin/server.js',
`
process.send({ isAdonisJS: true, environment: 'web' });
`
)
await fs.create(
'node_modules/ts-node/package.json',
JSON.stringify({
name: 'ts-node',
exports: { './esm': './esm.js' },
})
)
await fs.create('node_modules/ts-node/esm.js', '')

const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(filePath),
})

ace.app.rcFile.unstable_assembler = {
onDevServerStarted: [
async () => ({
default: async () => assert.isTrue(true),
}),
],
}

ace.ui.switchMode('raw')

const command = await ace.create(Serve, ['--no-assets', '--no-clear'])
cleanup(() => command.devServer.close())
await command.exec()
await sleep(1200)
})
})
Loading