Skip to content

Commit a2801ff

Browse files
authored
refactor: rewrite builded ace file (#67)
* feat: rewrite ace file * style: lint
1 parent fa322d7 commit a2801ff

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@types/pretty-hrtime": "^1.0.3",
5151
"c8": "^9.1.0",
5252
"cross-env": "^7.0.3",
53-
"dedent": "^1.5.1",
5453
"del-cli": "^5.0.0",
5554
"eslint": "^8.56.0",
5655
"github-label-sync": "^2.3.1",
@@ -68,6 +67,7 @@
6867
"@poppinss/chokidar-ts": "^4.1.3",
6968
"@poppinss/cliui": "^6.3.0",
7069
"cpy": "^11.0.0",
70+
"dedent": "^1.5.1",
7171
"execa": "^8.0.1",
7272
"fast-glob": "^3.3.2",
7373
"get-port": "^7.0.0",

src/bundler.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
*/
99

1010
import slash from 'slash'
11+
import dedent from 'dedent'
1112
import fs from 'node:fs/promises'
12-
import { relative } from 'node:path'
1313
import type tsStatic from 'typescript'
1414
import { fileURLToPath } from 'node:url'
15+
import { join, relative } from 'node:path'
1516
import { cliui, type Logger } from '@poppinss/cliui'
1617
import { detectPackageManager } from '@antfu/install-pkg'
1718

@@ -163,6 +164,27 @@ export class Bundler {
163164
return SUPPORT_PACKAGE_MANAGERS[pkgManager as SupportedPackageManager]
164165
}
165166

167+
/**
168+
* Rewrite the ace file since the original one
169+
* is importing ts-node which is not installed
170+
* in a production environment.
171+
*/
172+
async #createAceFile(outDir: string) {
173+
const aceFileLocation = join(outDir, 'ace.js')
174+
const aceFileContent = dedent(/* JavaScript */ `
175+
/**
176+
* This file is auto-generated by the build process.
177+
* If you had any custom code inside this file, then
178+
* instead write it inside the "bin/console.js" file.
179+
*/
180+
181+
await import('./bin/console.js')
182+
`)
183+
184+
await fs.writeFile(aceFileLocation, aceFileContent)
185+
this.#logger.info('rewrited ace file', { suffix: this.#getRelativeName(aceFileLocation) })
186+
}
187+
166188
/**
167189
* Set a custom CLI UI logger
168190
*/
@@ -202,7 +224,7 @@ export class Bundler {
202224
*/
203225
this.#logger.info('compiling typescript source', { suffix: 'tsc' })
204226
const buildCompleted = await this.#runTsc(outDir)
205-
await copyFiles(['ace.js'], this.#cwdPath, outDir)
227+
await this.#createAceFile(outDir)
206228

207229
/**
208230
* Remove incomplete build directory when tsc build

tests/bundler.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,22 @@ test.group('Bundler', () => {
136136
assert.fileExists('./build/pnpm-lock.yaml'),
137137
])
138138
})
139+
140+
test('remove ts-node reference in builded ace.js file', async ({ assert, fs }) => {
141+
await Promise.all([
142+
fs.create('ace.js', 'foo'),
143+
fs.create(
144+
'tsconfig.json',
145+
JSON.stringify({ compilerOptions: { outDir: 'build', skipLibCheck: true } })
146+
),
147+
fs.create('adonisrc.ts', 'export default {}'),
148+
fs.create('package.json', '{}'),
149+
fs.create('package-lock.json', '{}'),
150+
])
151+
152+
await new Bundler(fs.baseUrl, ts, {}).bundle()
153+
154+
const aceFile = await fs.contents('./build/ace.js')
155+
assert.notInclude(aceFile, 'ts-node')
156+
})
139157
})

0 commit comments

Comments
 (0)