-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(egg-bin): add bundle subcommand #5888
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
5 commits
Select commit
Hold shift + click to select a range
45abecf
feat(egg-bin): add bundle subcommand
killagu 005b155
fix(egg-bin): address bundle command review
killagu 31450d7
fix(bundler): publish egg-bundler package
killagu db55b59
test(egg-bin): cover bundle command flags
killagu 5db1226
fix(egg-bin): inherit bundle framework config
killagu 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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import path from 'node:path'; | ||
| import { debuglog } from 'node:util'; | ||
|
|
||
| import { getFrameworkPath } from '@eggjs/utils'; | ||
| import { Flags } from '@oclif/core'; | ||
|
|
||
| import { BaseCommand } from '../baseCommand.ts'; | ||
|
|
||
| const debug = debuglog('egg/bin/commands/bundle'); | ||
| const bundleModes = ['production', 'development'] as const; | ||
| type BundleMode = (typeof bundleModes)[number]; | ||
|
|
||
| function getBundleMode(mode: string): BundleMode { | ||
| if (mode === 'production' || mode === 'development') { | ||
| return mode; | ||
| } | ||
| throw new Error(`Unsupported bundle mode: ${mode}`); | ||
| } | ||
|
|
||
| export default class Bundle extends BaseCommand<typeof Bundle> { | ||
| static override description = 'Bundle an egg app into a deployable artifact using @eggjs/egg-bundler'; | ||
|
|
||
| static override examples = [ | ||
| '<%= config.bin %> <%= command.id %>', | ||
| '<%= config.bin %> <%= command.id %> --output ./dist-bundle', | ||
| '<%= config.bin %> <%= command.id %> --mode development', | ||
| '<%= config.bin %> <%= command.id %> --framework egg --output ./out', | ||
| ]; | ||
|
|
||
| static override flags = { | ||
| output: Flags.string({ | ||
| char: 'o', | ||
| description: 'output directory for the bundled artifact', | ||
| default: './dist-bundle', | ||
| }), | ||
| manifest: Flags.string({ | ||
| description: 'path to manifest.json (defaults to <baseDir>/.egg/manifest.json)', | ||
| }), | ||
| framework: Flags.string({ | ||
| char: 'f', | ||
| description: 'framework name or absolute path', | ||
| }), | ||
| mode: Flags.string({ | ||
| description: 'build mode', | ||
| options: [...bundleModes], | ||
| default: 'production', | ||
| }), | ||
| 'no-tegg': Flags.boolean({ | ||
| description: 'disable tegg decoratedFile collection', | ||
| default: false, | ||
| }), | ||
| 'force-external': Flags.string({ | ||
| description: 'package name to always mark as external (repeatable)', | ||
| multiple: true, | ||
| default: [], | ||
| }), | ||
| 'inline-external': Flags.string({ | ||
| description: 'package name to force-inline even if auto-detected as external (repeatable)', | ||
| multiple: true, | ||
|
killagu marked this conversation as resolved.
|
||
| default: [], | ||
| }), | ||
| }; | ||
|
|
||
| public async run(): Promise<void> { | ||
| const { flags } = this; | ||
| const baseDir = flags.base; | ||
| const outputDir = path.isAbsolute(flags.output) ? flags.output : path.join(baseDir, flags.output); | ||
| const manifestPath = flags.manifest | ||
| ? path.isAbsolute(flags.manifest) | ||
| ? flags.manifest | ||
| : path.join(baseDir, flags.manifest) | ||
| : undefined; | ||
|
|
||
| debug( | ||
| 'bundle: baseDir=%s, outputDir=%s, framework=%s, mode=%s, tegg=%s', | ||
| baseDir, | ||
| outputDir, | ||
| flags.framework, | ||
| flags.mode, | ||
| !flags['no-tegg'], | ||
| ); | ||
|
|
||
| const { bundle } = await import('@eggjs/egg-bundler'); | ||
| const result = await bundle({ | ||
| baseDir, | ||
| outputDir, | ||
| manifestPath, | ||
| framework: getFrameworkPath({ framework: flags.framework, baseDir }), | ||
| mode: getBundleMode(flags.mode), | ||
| tegg: !flags['no-tegg'], | ||
| externals: { | ||
| force: flags['force-external'], | ||
| inline: flags['inline-external'], | ||
| }, | ||
|
killagu marked this conversation as resolved.
|
||
| }); | ||
|
killagu marked this conversation as resolved.
|
||
|
|
||
| this.log(`bundled to ${result.outputDir} (${result.files.length} files)`); | ||
| this.log(`manifest: ${result.manifestPath}`); | ||
| } | ||
| } | ||
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,78 @@ | ||
| import path from 'node:path'; | ||
|
|
||
| import { getFrameworkPath } from '@eggjs/utils'; | ||
| import { describe, expect, it, vi, beforeEach } from 'vitest'; | ||
|
|
||
| import Bundle from '../../src/commands/bundle.ts'; | ||
| import { getFixtures } from '../helper.ts'; | ||
|
|
||
| const bundleMock = vi.hoisted(() => vi.fn()); | ||
|
|
||
| vi.mock('@eggjs/egg-bundler', () => ({ | ||
| bundle: bundleMock, | ||
| })); | ||
|
|
||
| describe('test/commands/bundle.test.ts', () => { | ||
| const baseDir = getFixtures('demo-app'); | ||
|
|
||
| beforeEach(() => { | ||
| bundleMock.mockReset(); | ||
| bundleMock.mockResolvedValue({ | ||
| outputDir: path.join(baseDir, 'dist-bundle'), | ||
| manifestPath: path.join(baseDir, '.egg/manifest.json'), | ||
| files: ['server.js'], | ||
| }); | ||
| }); | ||
|
|
||
| it('should pass default options to egg-bundler', async () => { | ||
| await Bundle.run(['--base', baseDir]); | ||
|
|
||
| expect(bundleMock).toHaveBeenCalledTimes(1); | ||
| expect(bundleMock).toHaveBeenCalledWith({ | ||
| baseDir, | ||
| outputDir: path.join(baseDir, 'dist-bundle'), | ||
| manifestPath: undefined, | ||
| framework: getFrameworkPath({ baseDir }), | ||
| mode: 'production', | ||
|
killagu marked this conversation as resolved.
|
||
| tegg: true, | ||
| externals: { | ||
| force: [], | ||
| inline: [], | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should pass resolved flag options to egg-bundler', async () => { | ||
| await Bundle.run([ | ||
| '--base', | ||
| baseDir, | ||
| '--output', | ||
| 'bundle-output', | ||
| '--manifest', | ||
| '.egg/custom-manifest.json', | ||
| '--mode', | ||
| 'development', | ||
| '--no-tegg', | ||
| '--force-external', | ||
| '@scope/foo', | ||
| '--force-external', | ||
| 'bar', | ||
| '--inline-external', | ||
| 'baz', | ||
| ]); | ||
|
|
||
| expect(bundleMock).toHaveBeenCalledTimes(1); | ||
| expect(bundleMock).toHaveBeenCalledWith({ | ||
| baseDir, | ||
| outputDir: path.join(baseDir, 'bundle-output'), | ||
| manifestPath: path.join(baseDir, '.egg/custom-manifest.json'), | ||
| framework: getFrameworkPath({ baseDir }), | ||
| mode: 'development', | ||
| tegg: false, | ||
| externals: { | ||
| force: ['@scope/foo', 'bar'], | ||
| inline: ['baz'], | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
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
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.