Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@types/pretty-hrtime": "^1.0.3",
"c8": "^9.1.0",
"cross-env": "^7.0.3",
"dedent": "^1.5.1",
"del-cli": "^5.0.0",
"eslint": "^8.56.0",
"github-label-sync": "^2.3.1",
Expand All @@ -68,6 +67,7 @@
"@poppinss/chokidar-ts": "^4.1.3",
"@poppinss/cliui": "^6.3.0",
"cpy": "^11.0.0",
"dedent": "^1.5.1",
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"get-port": "^7.0.0",
Expand Down
26 changes: 24 additions & 2 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
*/

import slash from 'slash'
import dedent from 'dedent'
import fs from 'node:fs/promises'
import { relative } from 'node:path'
import type tsStatic from 'typescript'
import { fileURLToPath } from 'node:url'
import { join, relative } from 'node:path'
import { cliui, type Logger } from '@poppinss/cliui'
import { detectPackageManager } from '@antfu/install-pkg'

Expand Down Expand Up @@ -163,6 +164,27 @@ export class Bundler {
return SUPPORT_PACKAGE_MANAGERS[pkgManager as SupportedPackageManager]
}

/**
* Rewrite the ace file since the original one
* is importing ts-node which is not installed
* in a production environment.
*/
async #createAceFile(outDir: string) {
const aceFileLocation = join(outDir, 'ace.js')
const aceFileContent = dedent(/* JavaScript */ `
/**
* This file is auto-generated by the build process.
* If you had any custom code inside this file, then
* instead write it inside the "bin/console.js" file.
*/

await import('./bin/console.js')
`)

await fs.writeFile(aceFileLocation, aceFileContent)
this.#logger.info('rewrited ace file', { suffix: this.#getRelativeName(aceFileLocation) })
}

/**
* Set a custom CLI UI logger
*/
Expand Down Expand Up @@ -202,7 +224,7 @@ export class Bundler {
*/
this.#logger.info('compiling typescript source', { suffix: 'tsc' })
const buildCompleted = await this.#runTsc(outDir)
await copyFiles(['ace.js'], this.#cwdPath, outDir)
await this.#createAceFile(outDir)

/**
* Remove incomplete build directory when tsc build
Expand Down
18 changes: 18 additions & 0 deletions tests/bundler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,22 @@ test.group('Bundler', () => {
assert.fileExists('./build/pnpm-lock.yaml'),
])
})

test('remove ts-node reference in builded ace.js file', async ({ assert, fs }) => {
await Promise.all([
fs.create('ace.js', 'foo'),
fs.create(
'tsconfig.json',
JSON.stringify({ compilerOptions: { outDir: 'build', skipLibCheck: true } })
),
fs.create('adonisrc.ts', 'export default {}'),
fs.create('package.json', '{}'),
fs.create('package-lock.json', '{}'),
])

await new Bundler(fs.baseUrl, ts, {}).bundle()

const aceFile = await fs.contents('./build/ace.js')
assert.notInclude(aceFile, 'ts-node')
})
})