|
1 | 1 | import { sep } from 'path'
|
2 | 2 | import type { WebpackOptionsNormalized } from 'webpack'
|
3 |
| -import * as WebpackDevServer from 'webpack-dev-server' |
4 | 3 | import { App } from '@vuepress/core'
|
5 | 4 | import { path } from '@vuepress/utils'
|
6 | 5 | import type { WebpackBundlerOptions } from '../types'
|
| 6 | +import type { WebpackDevServer } from '../types.webpack-dev-server' |
7 | 7 | import { trailingSlashMiddleware } from './trailingSlashMiddleware'
|
8 | 8 |
|
9 | 9 | export const createDevServerConfig = (
|
10 | 10 | app: App,
|
11 | 11 | options: WebpackBundlerOptions
|
12 |
| -): WebpackDevServer.Configuration => { |
13 |
| - // TODO: add types for webpack-dev-server 4 |
14 |
| - const serverConfig: WebpackOptionsNormalized['devServer'] = { |
15 |
| - compress: true, |
16 |
| - devMiddleware: { |
17 |
| - publicPath: app.options.base, |
18 |
| - writeToDisk: false, |
19 |
| - stats: app.env.isDebug ? 'normal' : 'errors-warnings', |
20 |
| - }, |
21 |
| - firewall: false, |
22 |
| - headers: { |
23 |
| - 'access-control-allow-origin': '*', |
24 |
| - }, |
25 |
| - historyApiFallback: { |
26 |
| - disableDotRule: true, |
27 |
| - rewrites: [{ from: /./, to: path.join(app.options.base, 'index.html') }], |
28 |
| - }, |
29 |
| - host: app.options.host, |
30 |
| - hot: true, |
31 |
| - onAfterSetupMiddleware: (expressApp, server) => { |
32 |
| - // plugin hook: afterDevServer |
33 |
| - options.afterDevServer?.(expressApp, server) |
34 |
| - }, |
35 |
| - onBeforeSetupMiddleware: (expressApp, server) => { |
36 |
| - // use trailing slash middleware to support vuepress routing in dev-server |
37 |
| - // it will be handled by most of the deployment platforms |
38 |
| - expressApp.use(trailingSlashMiddleware) |
| 12 | +): WebpackOptionsNormalized['devServer'] => ({ |
| 13 | + compress: true, |
| 14 | + devMiddleware: { |
| 15 | + publicPath: app.options.base, |
| 16 | + writeToDisk: false, |
| 17 | + stats: app.env.isDebug ? 'normal' : 'errors-warnings', |
| 18 | + }, |
| 19 | + firewall: false, |
| 20 | + headers: { |
| 21 | + 'access-control-allow-origin': '*', |
| 22 | + }, |
| 23 | + historyApiFallback: { |
| 24 | + disableDotRule: true, |
| 25 | + rewrites: [{ from: /./, to: path.join(app.options.base, 'index.html') }], |
| 26 | + }, |
| 27 | + host: app.options.host, |
| 28 | + hot: true, |
| 29 | + onAfterSetupMiddleware: (server: WebpackDevServer) => { |
| 30 | + options.afterDevServer?.(server) |
| 31 | + }, |
| 32 | + onBeforeSetupMiddleware: (server: WebpackDevServer) => { |
| 33 | + // use trailing slash middleware to support vuepress routing in dev-server |
| 34 | + // it will be handled by most of the deployment platforms |
| 35 | + server.use(trailingSlashMiddleware) |
39 | 36 |
|
40 |
| - // plugin hook: beforeDevServer |
41 |
| - options.beforeDevServer?.(expressApp, server) |
42 |
| - }, |
43 |
| - open: app.options.open, |
44 |
| - port: app.options.port, |
45 |
| - static: { |
46 |
| - // `static.directory` will fail on Windows if we do not replace / with \ |
47 |
| - directory: app.dir.public().replace('/', sep), |
48 |
| - publicPath: app.options.base, |
49 |
| - watch: { |
50 |
| - ignoreInitial: true, |
51 |
| - ignored: [ |
52 |
| - // Do not watch node_modules |
53 |
| - 'node_modules', |
54 |
| - ], |
55 |
| - }, |
| 37 | + options.beforeDevServer?.(server) |
| 38 | + }, |
| 39 | + open: app.options.open, |
| 40 | + port: app.options.port, |
| 41 | + static: { |
| 42 | + // `static.directory` will fail on Windows if we do not replace / with \ |
| 43 | + directory: app.dir.public().replace('/', sep), |
| 44 | + publicPath: app.options.base, |
| 45 | + watch: { |
| 46 | + ignoreInitial: true, |
| 47 | + ignored: [ |
| 48 | + // Do not watch node_modules |
| 49 | + 'node_modules', |
| 50 | + ], |
56 | 51 | },
|
57 |
| - } |
58 |
| - |
59 |
| - return serverConfig |
60 |
| -} |
| 52 | + }, |
| 53 | +}) |
0 commit comments