Skip to content

feat: serve lazy compilation from the dev server's own server - #5807

Open
alexander-akait wants to merge 7 commits into
mainfrom
feat/lazy-compilation-shared-server
Open

alexander-akait wants to merge 7 commits into
mainfrom
feat/lazy-compilation-shared-server

Conversation

@alexander-akait

@alexander-akait alexander-akait commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Summary

With experiments.lazyCompilation, webpack's backend starts its own server on a random port. It ignores the dev server's host, port and https, and a client that reconnects after a restart reaches a port that is gone.

webpack/webpack#23435 adds a backendOptions hook to LazyCompilationPlugin. This PR taps it, where the compiler's webpack has it, and hands the backend this server and the url it is reached at:

LazyCompilationPlugin.getCompilerHooks(compiler).backendOptions.tap(pluginName, (options) =>
  options.listen || options.server
    ? options // the configuration's own backend server stays
    : { ...options, server: this.server, urlBase: "http://localhost:8080" }
);

The urlBase is handed over because in plugin mode the server listens only after the first build, which waits for the backend. Nothing changes with a webpack without the hook or with ipc.

Measured with webpack/webpack#23435 linked, lazyCompilation: { entries: false } and port: 18765:

Lazy compilation url in the bundle
before http://localhost:<random port>/lazy-compilation-using-
after http://127.0.0.1:18765/lazy-compilation-using- (answered with text/event-stream; other requests still served)

Depends on webpack/webpack#23435.

What kind of change does this PR introduce?

feat

Did you add tests for your changes?

Yes, in test/e2e/lazy-compilation.test.js. The installed webpack predates the hook, so a stand-in hook checks that the dev server passes its server and url, and keeps a listen the configuration names. It fails without the change.

Does this PR introduce a breaking change?

No.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

The docs could say that lazy compilation runs on the dev server's own host and port when webpack supports it, and that experiments.lazyCompilation.backend.listen or server opts out.

Use of AI

Claude Code wrote the change and its test, ran the test, the repository's lint and an end-to-end check against webpack/webpack#23435, at the requester's direction.


Generated by Claude Code

Summary by CodeRabbit

  • New Features
    • Lazy compilation can connect through the development server using its configured host, port, and HTTP or HTTPS protocol when supported by webpack. Unset or wildcard hosts use localhost.
    • Existing connection settings remain unchanged when custom listener or server options are provided, backend options are functions, no server is available, or the server uses a dynamically assigned port.
    • This behavior applies to eligible compilers using the development server; IPC configurations remain unchanged.

Tap webpack's `LazyCompilationPlugin` `backendOptions` hook, where the
compiler's webpack has it, and hand the backend this server and the url
it is reached at, so lazy compilation shares its host, port and https.
A `listen` or `server` the configuration names is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FhutTE6yv5FArV4ThNJrq
@changeset-bot

changeset-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c85801b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
webpack-dev-server Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

Walkthrough

Server.initialize() now sets up lazy-compilation backend options after creating the HTTP server. The setup skips IPC, disabled child compilers, and compilers without webpack’s lazy-compilation plugin. For supported compilers, it preserves options that already specify listen or server; otherwise, it supplies the dev server and a URL based on its TLS state, host, and port. End-to-end tests check default options, supplied listener options, and the behavior when the configured port is 0. A changeset documents the update.

Merge Risk: Moderate · previous review ebb20

Lazy compilation now runs on the dev server's own host, port and protocol. Setups that bind to 0.0.0.0 and are opened from another machine may fail to load lazy chunks because the lazy URL points at localhost. The lazy endpoint also bypasses the dev server's allowedHosts and allowedOrigins checks. Resolve or explicitly accept these before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "feat: serve lazy compilation from the dev server's own server" matches the main change. The changeset adds setupLazyCompilation() in lib/Server.js, which taps webpack's LazyCompilationPlugi…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "feat: serve lazy compilation from the dev server's own server" matches the main change. The changeset adds setupLazyCompilation() in lib/Server.js, which taps webpack's LazyCompilationPlugin backendOptions hook and passes the dev server's server and URL base. The objectives describe the same goal: lazy-compilation traffic uses the dev server's server instead of a separate random-port server. The title is specific and uses a conventional commit prefix.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5ec28b13-39ae-487c-bb5e-c7404e76058a
Commits

Reviewing files that changed from the base of the PR and between 796262e and 5b926df.

Files selected for processing: 3

  • .changeset/lazy-compilation-shared-server.md
  • lib/Server.js
  • test/e2e/lazy-compilation.test.js

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Comment thread lib/Server.js Resolved
Comment thread lib/Server.js Resolved
Comment thread lib/Server.js Resolved
With `port: 0` the url is only known once listening, which plugin mode
does after the first build, so the backend keeps its own server. An
explicit `listen: 0` now counts as the configuration's own setting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FhutTE6yv5FArV4ThNJrq

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (2)

Major · Do not advertise localhost to remote browsers. · Server.js:2891-2893

lib/Server.js:2891-2893
Major · Functional Correctness · Heavy lift

Do not advertise localhost to remote browsers.

With host: "0.0.0.0", a browser on another machine can load the app using the dev machine's LAN address. The generated lazy client instead connects to localhost, which points to the browser's own machine. The lazy import cannot activate on the dev server. Resolve the shared endpoint against the page origin for browser clients when the bind address is not a client-facing hostname. Keep a reachable absolute URL for non-browser clients and preserve explicit backend overrides. (raw.githubusercontent.com)

Major · Lazy compilation stays bound to the first HTTP server after a restart. · Server.js:2885

lib/Server.js:2885
Major · Stability & Availability · Heavy lift

Lazy compilation stays bound to the first HTTP server after a restart.

The lazy-compilation backend starts once per compiler. The backendOptions hook runs only on that first build, and the plugin keeps the backend afterward. When stop() and start() reuse the same compiler, the next build keeps the backend attached to the closed server. The new listener does not route lazy requests to that backend, so lazy imports can fail after the restart.

Before this change, the lazy listener stayed open across a restart. This change removes that behavior for shared-server setups.

Make lazy requests reach a live listener after stop() and start() on the same compiler. Choose one approach: rebind the backend to the listener that start() creates, or keep the shared server open across restarts. The supplied plugin source exposes no reset for the cached backend, so this change needs a webpack-side hook or a different restart policy. Add a test that restarts a dev server with lazy compilation enabled and requests a lazy module afterward.

Minor comments: 1

lib/Server.js: 1

2885-2885: Minor · Security & Privacy · Poor tradeoff

Apply the dev server's Host and Origin rules to the shared lazy endpoint.

When server: this.server is passed to webpack's lazy-compilation backend, the backend's dispatcher answers requests for its URL prefix before the Express app runs. The host-header-check and cross-origin-header-check middleware in lib/Server.js do not run for those requests. A request with a disallowed Host or Origin header can therefore open the lazy event stream and activate a known lazy module, which triggers a rebuild.

Require the lazy endpoint to pass the same Host and Origin rules before it opens the event stream or activates a module. Requests that pass these rules must still activate lazy modules.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 955da598-19c1-44b3-bc08-f2865a8a2be2
Commits

Reviewing files that changed from the base of the PR and between 5b926df and 5a3de7a.

Files selected for processing: 2

  • lib/Server.js
  • test/e2e/lazy-compilation.test.js

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FhutTE6yv5FArV4ThNJrq
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.98%. Comparing base (796262e) to head (04e55a7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5807      +/-   ##
==========================================
+ Coverage   95.91%   95.98%   +0.06%     
==========================================
  Files           6        6              
  Lines        5707     5798      +91     
==========================================
+ Hits         5474     5565      +91     
  Misses        233      233              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick comments: 1

test/e2e/lazy-compilation.test.js: 1

119-120: Trivial · Maintainability & Code Quality · Heavy lift

Add a request-level test with webpack’s real hook.

Keep the stand-in to test option handling on older webpack versions. When webpack exposes the real hook, also exercise a lazy import through the shared listener. Check that a permitted client receives the event stream and that a request denied by the dev server’s policy does not. The stand-in cannot detect a dispatcher that skips that policy. (raw.githubusercontent.com)

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ac35c85b-1d2d-4b9c-a7d9-b7bceb394856
Commits

Reviewing files that changed from the base of the PR and between 5a1e6ec and bdde17a.

Files selected for processing: 2

  • lib/Server.js
  • test/e2e/lazy-compilation.test.js

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

A backend function the configuration or another plugin brings stays as is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FhutTE6yv5FArV4ThNJrq

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments: 1

lib/Server.js: 1

2886-2886: Minor · Functional Correctness · Poor tradeoff

The lazy-compilation route depends on backend creation order, so a restart can misroute an existing client.

On a shared server, webpack numbers each backend prefix from a process-level counter when the backend is created. A fresh dev-server process can create the compiler backends in a different order. A client opened before the restart then reconnects to the prefix that another compiler now holds. That backend filters the requested module key against its own keys and activates nothing, so the original compiler's lazy module is not activated. Reloading the page recovers it.

The route must stay bound to one compiler across process restarts. The prefix assignment is in webpack's lazyCompilationBackend.js, not in lib/Server.js. The dev server's transport tap cannot change the prefix, so this change does not belong in the tap at line 2886.

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 20a4f675-ffa2-413c-ba95-0863987bd0b6
Commits

Reviewing files that changed from the base of the PR and between bdde17a and ebb20b2.

Files selected for processing: 2

  • lib/Server.js
  • test/e2e/lazy-compilation.test.js

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

alexander-akait and others added 2 commits November 18, 2026 16:13
The `transport` hook is tapped once per compiler and reads the server the
compiler is served from now, so `stop()` and `start()` again, or a new
server for the same compiler, hand the backend the current one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FhutTE6yv5FArV4ThNJrq
The `transport` hook carries an options object with a custom backend
under `backend`, so this server's `server` and `urlBase` reach it too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013FhutTE6yv5FArV4ThNJrq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant