Skip to content

Repository files navigation

rspack lazyCompilation bug repro (CLI vs API)

Problem

When using rspack with Lazy Compilation, the CLI (rspack dev) correctly applies lazy compilation settings However, when running the dev server programmatically via the API (new RspackDevServer(options, compiler)), all Lazy Compilation settings appear to be ignored.

Expected behavior (CLI)

With lazyCompilation: true, only index.html and main.js should be emitted on initial build. Then its dynamic imports should compile on demand when the browser requests them.

Actual behavior (API)

Entry chunks & imports are all are compiled and emitted eagerly at startup, as if lazyCompilation were ignored.

Repo structure

This repo provides both multiCompiler and single-compiler configs, each with CLI and API scripts.

src/
  index.js                # app1 entry - dynamic import('./render.js')
  app2.js                 # app2 entry - dynamic import('./render.js')
  render.js               # shared module (dynamically imported)
  render.css              # CSS imported by render.js
rspack.config.mjs         # multiCompiler array config (app1 + app2)
rspack.config.single.mjs  # single compiler config (app1 only)
dev-server.mjs            # API dev server - multiCompiler
dev-server.single.mjs     # API dev server - single compiler

An AssetEmitLogger plugin is included in both configs. It hooks into compiler.hooks.assetEmitted and compiler.hooks.done to log when assets are written and when compilation finishes.

Reproduction steps

Prerequisites

pnpm install

Scripts

Script Config Method
pnpm dev:cli multiCompiler CLI
pnpm dev:api multiCompiler API
pnpm dev:cli:single single compiler CLI
pnpm dev:api:single single compiler API
pnpm dev:cli:test single compiler using test option CLI
pnpm dev:api:test single compiler using test option API

All scripts start a dev server on port 3000. Stop one before starting another.

Test 1: CLI (lazy compilation works)

pnpm dev:cli

Watch the terminal. With working lazy compilation, only index.html and main.js should be emitted on initial build:

●  ━━━━━━━━━━━━━━━━━━━━━━━━━ (93%) sealing asset processing
●  ━━━━━━━━━━━━━━━━━━━━━━━━━ (93%) sealing after asset optimization
[app1] asset emitted: index.html
●  ━━━━━━━━━━━━━━━━━━━━━━━━━ (100%) emitting after emit
●  ━━━━━━━━━━━━━━━━━━━━━━━━━ (98%) emitting emit
[app2] asset emitted: index.html
[app2] asset emitted: main.js
●  ━━━━━━━━━━━━━━━━━━━━━━━━━ (100%) emitting after emit
●  ━━━━━━━━━━━━━━━━━━━━━━━━━ (100%) emitting after emit
[app2] compilation finished in 64 ms

Dynamic imports (src_render_js.js, src_render_js.css) compile later when the browser requests them.

Test 2: API (entries not deferred)

pnpm dev:api

Watch the terminal. All assets are emitted immediately at startup — entry compilation is not deferred:

[app1] asset emitted: index.html
[app1] asset emitted: src_render_js.js
[app1] asset emitted: src_render_js.css
[app1] asset emitted: main.js
[app2] asset emitted: index.html
[app2] asset emitted: src_render_js.css
[app2] asset emitted: src_render_js.js
[app2] asset emitted: main.js
[app1] compilation finished in 41 ms
[app2] compilation finished in 42 ms

The single-compiler variants (dev:cli:single / dev:api:single & dev:cli:test / dev:api:test) exhibit the same behavior.

About

issue example repo

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

Generated from rstackjs/rspack-repro