-
Notifications
You must be signed in to change notification settings - Fork 160
refactor(commands): unify webpack and Rspack commands #1424
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
Open
jbroma
wants to merge
14
commits into
callstack:main
Choose a base branch
from
jbroma:refactor/unify-commands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+576
−591
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d6a4ece
refactor(commands): add shared CompilerInterface, CompilerAsset types…
jbroma 831f368
feat(commands): add bundler auto-detection from config filename
jbroma 79b235e
refactor(commands): implement CompilerInterface on both Compiler classes
jbroma 0c8939f
refactor(commands): create unified command entry point with auto-dete…
jbroma 60f05f3
refactor: update consumers to use unified @callstack/repack/commands
jbroma 35c831b
fix: types
jbroma f030d31
Merge remote-tracking branch 'origin/main' into refactor/unify-commands
jbroma 32236c8
fix(commands): reject pending assets on webpack errors
jbroma c5254f2
fix(commands): validate bundler option
jbroma ec1d5b3
chore: add unified commands changeset
jbroma 39d67ef
chore: split unified command release notes
jbroma 6b5b3af
Merge branch 'main' into refactor/unify-commands
dannyhw eadcc91
Merge main into refactor/unify-commands
dannyhw 06daa86
Merge branch 'main' into refactor/unify-commands
dannyhw 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@callstack/repack": patch | ||
| --- | ||
|
|
||
| Reject pending webpack asset requests when compilation fails instead of leaving requests hanging. |
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,6 @@ | ||
| --- | ||
| "@callstack/repack": minor | ||
| "@callstack/repack-init": patch | ||
| --- | ||
|
|
||
| Add the unified `@callstack/repack/commands` entry point with automatic bundler detection and a `--bundler` override. Re.Pack Init now uses it, while bundler-specific entry points remain available with deprecation warnings. |
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
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
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
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,2 @@ | ||
| import commands from '../dist/commands/index.js'; | ||
| export = commands; |
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,2 @@ | ||
| const commands = require('../dist/commands/index.js'); | ||
| module.exports = commands.default; |
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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| import commands from '../dist/commands/rspack/index.js'; | ||
| import commands from '../dist/commands/index.js'; | ||
| export = commands; |
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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| const commands = require('../dist/commands/rspack/index.js'); | ||
| module.exports = commands.default; | ||
| console.warn( | ||
| '[Re.Pack] Importing "@callstack/repack/commands/rspack" is deprecated. Use "@callstack/repack/commands" instead.' | ||
| ); | ||
| const { createBoundCommands } = require('../dist/commands/index.js'); | ||
| module.exports = createBoundCommands('rspack'); |
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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| import commands from '../dist/commands/webpack/index.js'; | ||
| import commands from '../dist/commands/index.js'; | ||
| export = commands; |
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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| const commands = require('../dist/commands/webpack/index.js'); | ||
| module.exports = commands.default; | ||
| console.warn( | ||
| '[Re.Pack] Importing "@callstack/repack/commands/webpack" is deprecated. Use "@callstack/repack/commands" instead.' | ||
| ); | ||
| const { createBoundCommands } = require('../dist/commands/index.js'); | ||
| module.exports = createBoundCommands('webpack'); |
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,20 @@ | ||
| import { bundleCommandOptions, startCommandOptions } from '../options.js'; | ||
|
|
||
| describe.each([ | ||
| ['start', startCommandOptions], | ||
| ['bundle', bundleCommandOptions], | ||
| ])('%s command options', (_, options) => { | ||
| const bundlerOption = options.find( | ||
| (option) => option.name === '--bundler <string>' | ||
| ); | ||
|
|
||
| test.each(['rspack', 'webpack'])('accepts the %s bundler', (bundler) => { | ||
| expect(bundlerOption?.parse?.(bundler)).toBe(bundler); | ||
| }); | ||
|
|
||
| test('rejects an unsupported bundler', () => { | ||
| expect(() => bundlerOption?.parse?.('metro')).toThrow( | ||
| 'Invalid bundler "metro". Expected "rspack" or "webpack".' | ||
| ); | ||
| }); | ||
| }); |
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,167 @@ | ||
| import { CLIError } from '../helpers/index.js'; | ||
| import { detectBundler } from './common/config/detectBundler.js'; | ||
| import { makeCompilerConfig } from './common/config/makeCompilerConfig.js'; | ||
| import { | ||
| getMaxWorkers, | ||
| normalizeStatsOptions, | ||
| resetPersistentCache, | ||
| setupEnvironment, | ||
| setupRspackEnvironment, | ||
| writeStats, | ||
| } from './common/index.js'; | ||
| import type { | ||
| BundleArguments, | ||
| Bundler, | ||
| CliConfig, | ||
| ConfigurationObject, | ||
| } from './types.js'; | ||
|
|
||
| /** | ||
| * Minimal compiler interface for the bundle command. | ||
| * Both rspack() and webpack() return objects satisfying this shape. | ||
| * Defined here because both bundlers are optional peer dependencies. | ||
| */ | ||
| interface BundleCompiler { | ||
| run(callback: (error: Error | null, stats?: BundleStats) => void): void; | ||
| watch( | ||
| options: Record<string, unknown>, | ||
| callback: (error: Error | null, stats?: BundleStats) => void | ||
| ): unknown; | ||
| hooks: { watchClose: { tap(name: string, fn: () => void): void } }; | ||
| close(callback: (error: Error | null) => void): void; | ||
| options: { stats: unknown }; | ||
| context: string; | ||
| } | ||
|
|
||
| /** | ||
| * Minimal stats interface for the bundle command. | ||
| * Both rspack and webpack Stats objects satisfy this shape. | ||
| */ | ||
| interface BundleStats { | ||
| hasErrors(): boolean; | ||
| compilation?: { errors?: unknown[] }; | ||
| toJson(options: unknown): Record<string, unknown>; | ||
| } | ||
|
|
||
| /** | ||
| * Unified bundle command that builds and saves the bundle | ||
| * alongside any other assets to filesystem. | ||
| * | ||
| * Auto-detects the bundler engine (rspack or webpack) unless explicitly specified. | ||
| * | ||
| * @param _ Original, non-parsed arguments that were provided when running this command. | ||
| * @param cliConfig Configuration object containing platform and project settings. | ||
| * @param args Parsed command line arguments. | ||
| * @param forcedBundler Optional bundler override from deprecated entry points. | ||
| */ | ||
| export async function bundle( | ||
| _: string[], | ||
| cliConfig: CliConfig, | ||
| args: BundleArguments, | ||
| forcedBundler?: Bundler | ||
| ) { | ||
| const bundler = | ||
| forcedBundler ?? | ||
| detectBundler( | ||
| cliConfig.root, | ||
| args.config ?? args.webpackConfig, | ||
| args.bundler | ||
| ); | ||
|
|
||
| const [config] = await makeCompilerConfig<ConfigurationObject>({ | ||
| args: args, | ||
| bundler, | ||
| command: 'bundle', | ||
| rootDir: cliConfig.root, | ||
| platforms: [args.platform], | ||
| reactNativePath: cliConfig.reactNativePath, | ||
| }); | ||
|
|
||
| // remove devServer configuration to avoid schema validation errors | ||
| delete config.devServer; | ||
|
|
||
| // expose selected args as environment variables | ||
| setupEnvironment(args); | ||
|
|
||
| if (bundler === 'rspack') { | ||
| const maxWorkers = args.maxWorkers ?? getMaxWorkers(); | ||
| setupRspackEnvironment(maxWorkers.toString()); | ||
| } | ||
|
|
||
| if (!args.entryFile && !config.entry) { | ||
| throw new CLIError("Option '--entry-file <path>' argument is missing"); | ||
| } | ||
|
|
||
| if (args.resetCache) { | ||
| if (bundler === 'rspack') { | ||
| resetPersistentCache({ | ||
| bundler: 'rspack', | ||
| rootDir: cliConfig.root, | ||
| cacheConfigs: [config.experiments?.cache], | ||
| }); | ||
| } else { | ||
| resetPersistentCache({ | ||
| bundler: 'webpack', | ||
| rootDir: cliConfig.root, | ||
| cacheConfigs: [config.cache], | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Dynamic import of bundler engine — both are optional peer dependencies | ||
| let compiler: BundleCompiler; | ||
| if (bundler === 'rspack') { | ||
| const { rspack } = await import('@rspack/core'); | ||
| compiler = rspack(config) as BundleCompiler; | ||
| } else { | ||
| const webpack = (await import('webpack')).default; | ||
| compiler = webpack(config) as BundleCompiler; | ||
| } | ||
|
|
||
| return new Promise<void>((resolve) => { | ||
| const errorHandler = async (error: Error | null, stats?: BundleStats) => { | ||
| if (error) { | ||
| throw new CLIError(error.message); | ||
| } | ||
|
|
||
| if (stats?.hasErrors()) { | ||
| stats.compilation?.errors?.forEach((e) => { | ||
| console.error(e); | ||
| }); | ||
| process.exit(2); | ||
| } | ||
|
|
||
| if (args.json && stats !== undefined) { | ||
| const statsOptions = normalizeStatsOptions( | ||
| compiler.options.stats, | ||
| args.stats | ||
| ); | ||
|
|
||
| const statsJson = stats.toJson(statsOptions); | ||
|
|
||
| try { | ||
| await writeStats(statsJson, { | ||
| filepath: args.json, | ||
| rootDir: compiler.context, | ||
| }); | ||
| } catch (e) { | ||
| throw new CLIError(String(e)); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if (args.watch) { | ||
| compiler.hooks.watchClose.tap('bundle', resolve); | ||
| compiler.watch(config.watchOptions ?? {}, errorHandler); | ||
| } else { | ||
| compiler.run((error: Error | null, stats?: BundleStats) => { | ||
| // make cache work: https://webpack.js.org/api/node/#run | ||
| compiler.close(async (closeErr: Error | null) => { | ||
| if (closeErr) console.error(closeErr); | ||
| await errorHandler(error, stats); | ||
| resolve(); | ||
| }); | ||
| }); | ||
| } | ||
| }); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if a user has the webpack commands and specifies --bundler=rspack what happens? Should we care to handle it? In this case it would be overruled by the
createBoundCommandsfunction right?