-
Notifications
You must be signed in to change notification settings - Fork 9
feat: introduce optimize command
#23
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
73a0788
feat: add optimize command
usualoma 88f8e03
chore: add esbuild to dependencies
usualoma 4425e5c
feat(optimize): try to find entry files from default candidates if no…
usualoma 39e8ece
refactor(optimize): sourcemap is not needed
usualoma 3da1546
refactor(optimize): Assign the serialized code to a variable once, ev…
usualoma f2dc0f0
chore: add "vitest/globals" to types in tsconfig.json
usualoma 198ea35
test(optimize): add tests
usualoma 5333d5c
Merge branch 'main' into feat/optimize
usualoma 8b7d975
test(optimize): import vitest explicitly
usualoma e35d335
feat(optimize): changed specifications to export bundled files.
usualoma 16e4e0b
Update README.md
usualoma 03193f4
refactor(optimize): Remove unused code
usualoma 529f9d6
refactor(optimize): write files directly from `esbuild.build`
usualoma 2c09f8c
Update README.md
usualoma dba02c6
feat(optimize): add minify option
usualoma 026e426
feat(optimize): add stat to output
usualoma 701aec4
refactor(optimize): improve output messages
usualoma 75596ab
refactor(optimize): improve output messages
usualoma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,4 +50,4 @@ | |
| "vitest": "^3.2.4" | ||
| }, | ||
| "packageManager": "bun@1.2.20" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import { Command } from 'commander' | ||
| import { describe, it, expect, beforeEach } from 'vitest' | ||
| import { execFile } from 'node:child_process' | ||
| import { mkdirSync, mkdtempSync, writeFileSync, readFileSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { optimizeCommand } from './index' | ||
|
|
||
| const program = new Command() | ||
| optimizeCommand(program) | ||
|
|
||
| const npmInstall = async () => | ||
| new Promise<void>((resolve) => { | ||
| const child = execFile('npm', ['install']) | ||
| child.on('exit', () => { | ||
| resolve() | ||
| }) | ||
| }) | ||
|
|
||
| describe('optimizeCommand', () => { | ||
| let dir: string | ||
| beforeEach(() => { | ||
| dir = mkdtempSync(join(tmpdir(), 'hono-cli-optimize-test')) | ||
| mkdirSync(join(dir, 'src'), { recursive: true }) | ||
| process.chdir(dir) | ||
| }) | ||
|
|
||
| it('should throws an error if entry file not found', async () => { | ||
| await expect( | ||
| program.parseAsync(['node', 'hono', 'optimize', './non-existent-file.ts']) | ||
| ).rejects.toThrowError() | ||
| }) | ||
|
|
||
| it.each([ | ||
| { | ||
| name: 'src/index.ts', | ||
| files: [ | ||
| { | ||
| path: './src/index.ts', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono<{ Bindings: { FOO: string } }>() | ||
| app.get('/', (c) => c.text('Hello, World!')) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/index.js', | ||
| content: `this.router = new PreparedRegExpRouter(...routerParams)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'src/index.tsx', | ||
| files: [ | ||
| { | ||
| path: './src/index.tsx', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono() | ||
| app.get('/', (c) => c.html(<div>Hello, World!</div>)) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/index.js', | ||
| content: `this.router = new PreparedRegExpRouter(...routerParams)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'src/index.js', | ||
| files: [ | ||
| { | ||
| path: './src/index.js', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono() | ||
| app.get('/', (c) => c.text('Hello, World!')) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/index.js', | ||
| content: `this.router = new PreparedRegExpRouter(...routerParams)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'src/index.jsx', | ||
| files: [ | ||
| { | ||
| path: './src/index.jsx', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono() | ||
| app.get('/', (c) => c.html(<div>Hello, World!</div>)) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/index.js', | ||
| content: `this.router = new PreparedRegExpRouter(...routerParams)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'specify entry file', | ||
| args: ['./src/app.ts'], | ||
| files: [ | ||
| { | ||
| path: './src/app.ts', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono<{ Bindings: { FOO: string } }>() | ||
| app.get('/', (c) => c.text('Hello, World!')) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/index.js', | ||
| content: `this.router = new PreparedRegExpRouter(...routerParams)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'specify outfile option', | ||
| args: ['-o', './dist/app.js'], | ||
| files: [ | ||
| { | ||
| path: './src/index.ts', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono<{ Bindings: { FOO: string } }>() | ||
| app.get('/', (c) => c.text('Hello, World!')) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/app.js', | ||
| content: `this.router = new PreparedRegExpRouter(...routerParams)`, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'fallback to TrieRouter', | ||
| files: [ | ||
| { | ||
| path: './src/index.ts', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono<{ Bindings: { FOO: string } }>() | ||
| app.get('/foo/:capture{ba(r|z)}', (c) => c.text('Hello, World!')) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/index.js', | ||
| content: `this.router = new TrieRouter()`, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'hono@4.9.10 does not have PreparedRegExpRouter', | ||
| honoVersion: '4.9.10', | ||
| files: [ | ||
| { | ||
| path: './src/index.ts', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono() | ||
| app.get('/', (c) => c.text('Hello, World!')) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/index.js', | ||
| content: `this.router = new RegExpRouter()`, | ||
| }, | ||
| }, | ||
| { | ||
| name: 'specify minify option', | ||
| args: ['-m'], | ||
| files: [ | ||
| { | ||
| path: './src/index.ts', | ||
| content: ` | ||
| import { Hono } from 'hono' | ||
| const app = new Hono<{ Bindings: { FOO: string } }>() | ||
| app.get('/', (c) => c.text('Hello, World!')) | ||
| export default app | ||
| `, | ||
| }, | ||
| ], | ||
| result: { | ||
| path: './dist/index.js', | ||
| lineCount: 2, | ||
| }, | ||
| }, | ||
| ])( | ||
| 'should success to optimize: $name', | ||
| { timeout: 0 }, | ||
| async ({ honoVersion, files, result, args }) => { | ||
| writeFileSync( | ||
| join(dir, 'package.json'), | ||
| JSON.stringify({ | ||
| name: 'hono-cli-optimize-test', | ||
| type: 'module', | ||
| dependencies: { | ||
| hono: honoVersion ?? '4.9.11', | ||
| }, | ||
| }) | ||
| ) | ||
| await npmInstall() | ||
| for (const file of files) { | ||
| writeFileSync(join(dir, file.path), file.content) | ||
| } | ||
| await program.parseAsync(['node', 'hono', 'optimize', ...(args ?? [])]) | ||
|
|
||
| const content = readFileSync(join(dir, result.path), 'utf-8') | ||
| if (result.lineCount) { | ||
| expect(content.split('\n').length).toBe(result.lineCount) | ||
| } | ||
| if (result.content) { | ||
| expect(content).toMatch(result.content) | ||
| } | ||
| } | ||
| ) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.