|
5 | 5 |
|
6 | 6 | # @rspack/dev-server |
7 | 7 |
|
8 | | -Development server for rspack. |
| 8 | +Use Rspack with a development server that provides live reloading. This should be used for development only. |
9 | 9 |
|
10 | | -## Documentation |
| 10 | +> `@rspack/dev-server` is based on `webpack-dev-server@5` |
11 | 11 |
|
12 | | -See [https://rspack.dev](https://rspack.dev) for details. |
| 12 | +## Installation |
| 13 | + |
| 14 | +First of all, install `@rspack/dev-server` and `@rspack/core` by your favorite package manager: |
| 15 | + |
| 16 | +```bash |
| 17 | +# npm |
| 18 | +$ npm install @rspack/dev-server @rspack/core --save-dev |
| 19 | + |
| 20 | +# yarn |
| 21 | +$ yarn add @rspack/dev-server @rspack/core --dev |
| 22 | + |
| 23 | +# pnpm |
| 24 | +$ pnpm add @rspack/dev-server @rspack/core --save-dev |
| 25 | + |
| 26 | +# bun |
| 27 | +$ bun add @rspack/dev-server @rspack/core -D |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +There are two recommended ways to use `@rspack/dev-server`: |
| 33 | + |
| 34 | +### With the CLI |
| 35 | + |
| 36 | +The easiest way to use it is with the [`@rspack/cli`](https://www.npmjs.com/package/@rspack/cli). |
| 37 | + |
| 38 | +You can install it in your project by: |
| 39 | + |
| 40 | +```bash |
| 41 | +# npm |
| 42 | +$ npm install @rspack/cli --save-dev |
| 43 | + |
| 44 | +# yarn |
| 45 | +$ yarn add @rspack/cli --dev |
| 46 | + |
| 47 | +# pnpm |
| 48 | +$ pnpm add @rspack/cli --save-dev |
| 49 | + |
| 50 | +# bun |
| 51 | +$ bun add @rspack/cli -D |
| 52 | +``` |
| 53 | + |
| 54 | +And then start the development server by: |
| 55 | + |
| 56 | +```bash |
| 57 | +# with rspack.config.js |
| 58 | +$ rspack serve |
| 59 | + |
| 60 | +# with custom config file |
| 61 | +$ rspack serve -c ./your.config.js |
| 62 | +``` |
| 63 | + |
| 64 | +> See [CLI](https://rspack.dev/api/cli) for more details. |
| 65 | +
|
| 66 | +While starting the development server, you can specify the configuration by the `devServer` field of your Rspack config file: |
| 67 | + |
| 68 | +```js |
| 69 | +// rspack.config.js |
| 70 | +module.exports = { |
| 71 | + // ... |
| 72 | + devServer: { |
| 73 | + // the configuration of the development server |
| 74 | + port: 8080 |
| 75 | + }, |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +> See [DevServer](https://rspack.dev/config/dev-server) for all configuration options. |
| 80 | +
|
| 81 | +### With the API |
| 82 | + |
| 83 | +While it's recommended to run `@rspack/dev-server` via the CLI, you may also choose to start a server via the API. |
| 84 | + |
| 85 | +```js |
| 86 | +import { RspackDevServer } from "@rspack/dev-server"; |
| 87 | +import rspack from "@rspack/core"; |
| 88 | +import rspackConfig from './rspack.config.js'; |
| 89 | + |
| 90 | +const compiler = rspack(rspackConfig); |
| 91 | +const devServerOptions = { |
| 92 | + ...rspackConfig.devServer, |
| 93 | + // override |
| 94 | + port: 8888 |
| 95 | +}; |
| 96 | + |
| 97 | +const server = new RspackDevServer(devServerOptions, compiler); |
| 98 | + |
| 99 | +server.startCallback(() => { |
| 100 | + console.log('Successfully started server on http://localhost:8888'); |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +> Cause `@rspack/dev-server` is based on `webpack-dev-server@5`, you can see the [webpack-dev-server API](https://webpack.js.org/api/webpack-dev-server/) for more methods of the server instance. |
| 105 | +
|
| 106 | +## Credits |
| 107 | + |
| 108 | +Thanks to the [webpack-dev-server](https://github.com/webpack/webpack-dev-server) project created by [@sokra](https://github.com/sokra) |
13 | 109 |
|
14 | 110 | ## License |
15 | 111 |
|
16 | | -Rspack is [MIT licensed](https://github.com/web-infra-dev/rspack/blob/main/LICENSE). |
| 112 | +[MIT licensed](https://github.com/web-infra-dev/rspack-dev-server/blob/main/LICENSE). |
0 commit comments