@rspack/dev-middleware 2.0.0-beta.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -39,9 +39,9 @@ bun add -D @rspack/dev-middleware
39
39
  ## Usage
40
40
 
41
41
  ```js
42
- const { devMiddleware } = require("@rspack/dev-middleware");
43
- const express = require("express");
44
- const { rspack } = require("@rspack/core");
42
+ import { rspack } from "@rspack/core";
43
+ import { devMiddleware } from "@rspack/dev-middleware";
44
+ import express from "express";
45
45
 
46
46
  const compiler = rspack({
47
47
  // Rspack options
@@ -72,7 +72,7 @@ See [below](#other-servers) for examples of use with other servers.
72
72
  | **[`etag`](#tag)** | `boolean\| "weak"\| "strong"` | `undefined` | Enable or disable etag generation. |
73
73
  | **[`lastModified`](#lastmodified)** | `boolean` | `undefined` | Enable or disable `Last-Modified` header. Uses the file system's last modified value. |
74
74
  | **[`cacheControl`](#cachecontrol)** | `boolean\|number\|string\|Object` | `undefined` | Enable or disable setting `Cache-Control` response header. |
75
- | **[`cacheImmutable`](#cacheimmutable)** | `boolean\` | `undefined` | Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets. |
75
+ | **[`cacheImmutable`](#cacheimmutable)** | `boolean` | `false` | Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets. |
76
76
  | **[`publicPath`](#publicpath)** | `string` | `undefined` | The public path that the middleware is bound to. |
77
77
  | **[`stats`](#stats)** | `boolean\|string\|Object` | `stats` (from a configuration) | Stats options object or preset name. |
78
78
  | **[`serverSideRender`](#serversiderender)** | `boolean` | `undefined` | Instructs the module to enable or disable the server-side rendering mode. |
@@ -196,21 +196,28 @@ Default: `undefined`
196
196
 
197
197
  Depending on the setting, the following headers will be generated:
198
198
 
199
- - `Boolean` - `Cache-Control: public, max-age=31536000000`
200
- - `Number` - `Cache-Control: public, max-age=YOUR_NUMBER`
199
+ - `Boolean` - `Cache-Control: public, max-age=31536000`
200
+ - `Number` - `Cache-Control: public, max-age=YOUR_NUMBER_IN_SECONDS`
201
201
  - `String` - `Cache-Control: YOUR_STRING`
202
- - `{ maxAge?: number, immutable?: boolean }` - `Cache-Control: public, max-age=YOUR_MAX_AGE_or_31536000000`, also `, immutable` can be added if you set the `immutable` option to `true`
202
+ - `{ maxAge?: number, immutable?: boolean }` - `Cache-Control: public, max-age=YOUR_MAX_AGE_IN_SECONDS_or_31536000`, also `, immutable` is added when you set the `immutable` option to `true`
203
+
204
+ Numeric `cacheControl` and `cacheControl.maxAge` values are interpreted as milliseconds, clamped to `0..31536000000`, and converted to seconds for the response header.
203
205
 
204
206
  Enable or disable setting `Cache-Control` response header.
205
207
 
206
208
  ### cacheImmutable
207
209
 
208
210
  Type: `Boolean`
209
- Default: `undefined`
211
+ Default: `false`
210
212
 
211
213
  Enable or disable setting `Cache-Control: public, max-age=31536000, immutable` response header for immutable assets (i.e. asset with a hash like `image.a4c12bde.jpg`).
214
+
212
215
  Immutable assets are assets that have their hash in the file name therefore they can be cached, because if you change their contents the file name will be changed.
213
- Take preference over the `cacheControl` option if the asset was defined as immutable.
216
+
217
+ When omitted, immutable assets fall back to the `cacheControl` option.
218
+
219
+ Set `cacheImmutable: true` to opt into the immutable cache header for hashed assets.
220
+ This takes precedence over the `cacheControl` option only when the asset was defined as immutable and `cacheImmutable` is `true`.
214
221
 
215
222
  ### publicPath
216
223
 
@@ -249,7 +256,7 @@ This option also accepts a `Function` value, which can be used to filter which f
249
256
  The function follows the same premise as [`Array#filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) in which a return value of `false` _will not_ write the file, and a return value of `true` _will_ write the file to disk. eg.
250
257
 
251
258
  ```js
252
- const { rspack } = require("@rspack/core");
259
+ import { rspack } from "@rspack/core";
253
260
 
254
261
  const configuration = {
255
262
  /* Rspack configuration */
@@ -269,16 +276,16 @@ Default: [memfs](https://github.com/streamich/memfs)
269
276
  Set the default file system which will be used by Rspack as primary destination of generated files.
270
277
  This option isn't affected by the [writeToDisk](#writeToDisk) option.
271
278
 
272
- This can be done simply by using `path.join`:
279
+ This can be done simply by using `node:path`'s `join`:
273
280
 
274
281
  ```js
275
- const path = require("node:path");
276
- const mkdirp = require("mkdirp");
277
- const myOutputFileSystem = require("my-fs");
278
- const { rspack } = require("@rspack/core");
282
+ import { join } from "node:path";
283
+ import { rspack } from "@rspack/core";
284
+ import mkdirp from "mkdirp";
285
+ import myOutputFileSystem from "my-fs";
279
286
 
280
- myOutputFileSystem.join = path.join.bind(path); // no need to bind
281
- myOutputFileSystem.mkdirp = mkdirp.bind(mkdirp); // no need to bind
287
+ myOutputFileSystem.join = join;
288
+ myOutputFileSystem.mkdirp = mkdirp;
282
289
 
283
290
  const compiler = rspack({
284
291
  /* Rspack configuration */
@@ -292,7 +299,7 @@ devMiddleware(compiler, { outputFileSystem: myOutputFileSystem });
292
299
  Allows to set up a callback to change the response data.
293
300
 
294
301
  ```js
295
- const { rspack } = require("@rspack/core");
302
+ import { rspack } from "@rspack/core";
296
303
 
297
304
  const configuration = {
298
305
  /* Rspack configuration */
@@ -328,9 +335,9 @@ Required: `No`
328
335
  A function executed once the middleware has stopped watching.
329
336
 
330
337
  ```js
331
- const { devMiddleware } = require("@rspack/dev-middleware");
332
- const express = require("express");
333
- const { rspack } = require("@rspack/core");
338
+ import { rspack } from "@rspack/core";
339
+ import { devMiddleware } from "@rspack/dev-middleware";
340
+ import express from "express";
334
341
 
335
342
  const compiler = rspack({
336
343
  /* Rspack configuration */
@@ -338,8 +345,7 @@ const compiler = rspack({
338
345
 
339
346
  const instance = devMiddleware(compiler);
340
347
 
341
- // eslint-disable-next-line new-cap
342
- const app = new express();
348
+ const app = express();
343
349
 
344
350
  app.use(instance);
345
351
 
@@ -363,9 +369,9 @@ Required: `No`
363
369
  A function executed once the middleware has invalidated.
364
370
 
365
371
  ```js
366
- const { devMiddleware } = require("@rspack/dev-middleware");
367
- const express = require("express");
368
- const { rspack } = require("@rspack/core");
372
+ import { rspack } from "@rspack/core";
373
+ import { devMiddleware } from "@rspack/dev-middleware";
374
+ import express from "express";
369
375
 
370
376
  const compiler = rspack({
371
377
  /* Rspack configuration */
@@ -373,8 +379,7 @@ const compiler = rspack({
373
379
 
374
380
  const instance = devMiddleware(compiler);
375
381
 
376
- // eslint-disable-next-line new-cap
377
- const app = new express();
382
+ const app = express();
378
383
 
379
384
  app.use(instance);
380
385
 
@@ -403,9 +408,9 @@ A function executed when the bundle becomes valid.
403
408
  If the bundle is valid at the time of calling, the callback is executed immediately.
404
409
 
405
410
  ```js
406
- const { devMiddleware } = require("@rspack/dev-middleware");
407
- const express = require("express");
408
- const { rspack } = require("@rspack/core");
411
+ import { rspack } from "@rspack/core";
412
+ import { devMiddleware } from "@rspack/dev-middleware";
413
+ import express from "express";
409
414
 
410
415
  const compiler = rspack({
411
416
  /* Rspack configuration */
@@ -413,8 +418,7 @@ const compiler = rspack({
413
418
 
414
419
  const instance = devMiddleware(compiler);
415
420
 
416
- // eslint-disable-next-line new-cap
417
- const app = new express();
421
+ const app = express();
418
422
 
419
423
  app.use(instance);
420
424
 
@@ -437,9 +441,9 @@ Required: `Yes`
437
441
  URL for the requested file.
438
442
 
439
443
  ```js
440
- const { devMiddleware } = require("@rspack/dev-middleware");
441
- const express = require("express");
442
- const { rspack } = require("@rspack/core");
444
+ import { rspack } from "@rspack/core";
445
+ import { devMiddleware } from "@rspack/dev-middleware";
446
+ import express from "express";
443
447
 
444
448
  const compiler = rspack({
445
449
  /* Rspack configuration */
@@ -447,15 +451,26 @@ const compiler = rspack({
447
451
 
448
452
  const instance = devMiddleware(compiler);
449
453
 
450
- // eslint-disable-next-line new-cap
451
- const app = new express();
454
+ const app = express();
452
455
 
453
456
  app.use(instance);
454
457
 
455
458
  instance.waitUntilValid(() => {
456
- const filename = instance.getFilenameFromUrl("/bundle.js");
459
+ let resolved;
460
+
461
+ try {
462
+ resolved = instance.getFilenameFromUrl("/bundle.js");
463
+ } catch (error) {
464
+ console.error(error);
465
+ return;
466
+ }
457
467
 
458
- console.log(`Filename is ${filename}`);
468
+ if (!resolved) {
469
+ console.log("Not found");
470
+ return;
471
+ }
472
+
473
+ console.log(`Filename is ${resolved.filename}`);
459
474
  });
460
475
  ```
461
476
 
@@ -468,9 +483,9 @@ Since `output.publicPath` and `output.filename`/`output.chunkFilename` can be dy
468
483
  But there is a solution to avoid it - mount the middleware to a non-root route, for example:
469
484
 
470
485
  ```js
471
- const { devMiddleware } = require("@rspack/dev-middleware");
472
- const express = require("express");
473
- const { rspack } = require("@rspack/core");
486
+ import { rspack } from "@rspack/core";
487
+ import { devMiddleware } from "@rspack/dev-middleware";
488
+ import express from "express";
474
489
 
475
490
  const compiler = rspack({
476
491
  // Rspack options
@@ -509,17 +524,17 @@ process is finished with server-side rendering enabled._
509
524
  Example Implementation:
510
525
 
511
526
  ```js
512
- const { devMiddleware } = require("@rspack/dev-middleware");
513
- const express = require("express");
514
- const isObject = require("is-object");
515
- const { rspack } = require("@rspack/core");
527
+ import { join } from "node:path";
528
+ import { rspack } from "@rspack/core";
529
+ import { devMiddleware } from "@rspack/dev-middleware";
530
+ import express from "express";
531
+ import isObject from "is-object";
516
532
 
517
533
  const compiler = rspack({
518
534
  /* Rspack configuration */
519
535
  });
520
536
 
521
- // eslint-disable-next-line new-cap
522
- const app = new express();
537
+ const app = express();
523
538
 
524
539
  // This function makes server rendering of asset references consistent with different Rspack chunk/entry configurations
525
540
  function normalizeAssets(assets) {
@@ -547,16 +562,16 @@ app.use((req, res) => {
547
562
  <title>My App</title>
548
563
  <style>
549
564
  ${normalizeAssets(assetsByChunkName.main)
550
- .filter((path) => path.endsWith(".css"))
551
- .map((path) => outputFileSystem.readFileSync(path.join(outputPath, path)))
565
+ .filter((asset) => asset.endsWith(".css"))
566
+ .map((asset) => outputFileSystem.readFileSync(join(outputPath, asset)))
552
567
  .join("\n")}
553
568
  </style>
554
569
  </head>
555
570
  <body>
556
571
  <div id="root"></div>
557
572
  ${normalizeAssets(assetsByChunkName.main)
558
- .filter((path) => path.endsWith(".js"))
559
- .map((path) => `<script src="${path}"></script>`)
573
+ .filter((asset) => asset.endsWith(".js"))
574
+ .map((asset) => `<script src="${asset}"></script>`)
560
575
  .join("\n")}
561
576
  </body>
562
577
  </html>
@@ -568,14 +583,16 @@ app.use((req, res) => {
568
583
 
569
584
  Examples of use with other servers will follow here.
570
585
 
571
- ### Connect
586
+ ### connect-next
587
+
588
+ [connect-next](https://github.com/rstackjs/connect-next) is an actively maintained fork of Connect.
572
589
 
573
590
  ```js
574
- const http = require("node:http");
575
- const { devMiddleware } = require("@rspack/dev-middleware");
576
- const connect = require("connect");
577
- const { rspack } = require("@rspack/core");
578
- const rspackConfig = require("./rspack.config.js");
591
+ import { createServer } from "node:http";
592
+ import { rspack } from "@rspack/core";
593
+ import { devMiddleware } from "@rspack/dev-middleware";
594
+ import { connect } from "connect-next";
595
+ import rspackConfig from "./rspack.config.js";
579
596
 
580
597
  const compiler = rspack(rspackConfig);
581
598
  const devMiddlewareOptions = {
@@ -585,18 +602,18 @@ const app = connect();
585
602
 
586
603
  app.use(devMiddleware(compiler, devMiddlewareOptions));
587
604
 
588
- http.createServer(app).listen(3000);
605
+ createServer(app).listen(3000);
589
606
  ```
590
607
 
591
608
  ### Router
592
609
 
593
610
  ```js
594
- const http = require("node:http");
595
- const { devMiddleware } = require("@rspack/dev-middleware");
596
- const finalhandler = require("finalhandler");
597
- const Router = require("router");
598
- const { rspack } = require("@rspack/core");
599
- const rspackConfig = require("./rspack.config.js");
611
+ import { createServer } from "node:http";
612
+ import { rspack } from "@rspack/core";
613
+ import { devMiddleware } from "@rspack/dev-middleware";
614
+ import finalhandler from "finalhandler";
615
+ import Router from "router";
616
+ import rspackConfig from "./rspack.config.js";
600
617
 
601
618
  const compiler = rspack(rspackConfig);
602
619
  const devMiddlewareOptions = {
@@ -608,7 +625,7 @@ const router = Router();
608
625
 
609
626
  router.use(devMiddleware(compiler, devMiddlewareOptions));
610
627
 
611
- const server = http.createServer((req, res) => {
628
+ const server = createServer((req, res) => {
612
629
  router(req, res, finalhandler(req, res));
613
630
  });
614
631
 
@@ -618,10 +635,10 @@ server.listen(3000);
618
635
  ### Express
619
636
 
620
637
  ```js
621
- const { devMiddleware } = require("@rspack/dev-middleware");
622
- const express = require("express");
623
- const { rspack } = require("@rspack/core");
624
- const rspackConfig = require("./rspack.config.js");
638
+ import { rspack } from "@rspack/core";
639
+ import { devMiddleware } from "@rspack/dev-middleware";
640
+ import express from "express";
641
+ import rspackConfig from "./rspack.config.js";
625
642
 
626
643
  const compiler = rspack(rspackConfig);
627
644
  const devMiddlewareOptions = {
@@ -637,10 +654,10 @@ app.listen(3000, () => console.log("Example app listening on port 3000!"));
637
654
  ### Koa
638
655
 
639
656
  ```js
640
- const { devMiddleware } = require("@rspack/dev-middleware");
641
- const Koa = require("koa");
642
- const { rspack } = require("@rspack/core");
643
- const rspackConfig = require("./rspack.simple.config");
657
+ import { rspack } from "@rspack/core";
658
+ import { devMiddleware } from "@rspack/dev-middleware";
659
+ import Koa from "koa";
660
+ import rspackConfig from "./rspack.simple.config.js";
644
661
 
645
662
  const compiler = rspack(rspackConfig);
646
663
  const devMiddlewareOptions = {
@@ -648,7 +665,7 @@ const devMiddlewareOptions = {
648
665
  };
649
666
  const app = new Koa();
650
667
 
651
- app.use(middleware.koaWrapper(compiler, devMiddlewareOptions));
668
+ app.use(devMiddleware.koaWrapper(compiler, devMiddlewareOptions));
652
669
 
653
670
  app.listen(3000);
654
671
  ```
@@ -656,10 +673,10 @@ app.listen(3000);
656
673
  ### Hapi
657
674
 
658
675
  ```js
659
- const Hapi = require("@hapi/hapi");
660
- const { devMiddleware } = require("@rspack/dev-middleware");
661
- const { rspack } = require("@rspack/core");
662
- const rspackConfig = require("./rspack.config.js");
676
+ import Hapi from "@hapi/hapi";
677
+ import { rspack } from "@rspack/core";
678
+ import { devMiddleware } from "@rspack/dev-middleware";
679
+ import rspackConfig from "./rspack.config.js";
663
680
 
664
681
  const compiler = rspack(rspackConfig);
665
682
  const devMiddlewareOptions = {};
@@ -688,10 +705,10 @@ process.on("unhandledRejection", (err) => {
688
705
  ### Hono
689
706
 
690
707
  ```js
708
+ import { rspack } from "@rspack/core";
709
+ import { devMiddleware } from "@rspack/dev-middleware";
691
710
  import { serve } from "@hono/node-server";
692
- import devMiddleware from "@rspack/dev-middleware";
693
711
  import { Hono } from "hono";
694
- import { rspack } from "@rspack/core";
695
712
  import rspackConfig from "./rspack.config.js";
696
713
 
697
714
  const compiler = rspack(rspackConfig);