diff --git a/package.json b/package.json index 9982664..217564c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/bundler.ts b/src/bundler.ts index a5d5367..308eabe 100644 --- a/src/bundler.ts +++ b/src/bundler.ts @@ -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' @@ -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 */ @@ -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 diff --git a/tests/bundler.spec.ts b/tests/bundler.spec.ts index 2d0b040..689f860 100644 --- a/tests/bundler.spec.ts +++ b/tests/bundler.spec.ts @@ -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') + }) })