-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bundler-webpack): sync types of webpack-dev-server 4 (close #208)
- Loading branch information
Showing
4 changed files
with
63 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 41 additions & 48 deletions
89
packages/@vuepress/bundler-webpack/src/dev/createDevServerConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,53 @@ | ||
import { sep } from 'path' | ||
import type { WebpackOptionsNormalized } from 'webpack' | ||
import * as WebpackDevServer from 'webpack-dev-server' | ||
import { App } from '@vuepress/core' | ||
import { path } from '@vuepress/utils' | ||
import type { WebpackBundlerOptions } from '../types' | ||
import type { WebpackDevServer } from '../types.webpack-dev-server' | ||
import { trailingSlashMiddleware } from './trailingSlashMiddleware' | ||
|
||
export const createDevServerConfig = ( | ||
app: App, | ||
options: WebpackBundlerOptions | ||
): WebpackDevServer.Configuration => { | ||
// TODO: add types for webpack-dev-server 4 | ||
const serverConfig: WebpackOptionsNormalized['devServer'] = { | ||
compress: true, | ||
devMiddleware: { | ||
publicPath: app.options.base, | ||
writeToDisk: false, | ||
stats: app.env.isDebug ? 'normal' : 'errors-warnings', | ||
}, | ||
firewall: false, | ||
headers: { | ||
'access-control-allow-origin': '*', | ||
}, | ||
historyApiFallback: { | ||
disableDotRule: true, | ||
rewrites: [{ from: /./, to: path.join(app.options.base, 'index.html') }], | ||
}, | ||
host: app.options.host, | ||
hot: true, | ||
onAfterSetupMiddleware: (expressApp, server) => { | ||
// plugin hook: afterDevServer | ||
options.afterDevServer?.(expressApp, server) | ||
}, | ||
onBeforeSetupMiddleware: (expressApp, server) => { | ||
// use trailing slash middleware to support vuepress routing in dev-server | ||
// it will be handled by most of the deployment platforms | ||
expressApp.use(trailingSlashMiddleware) | ||
): WebpackOptionsNormalized['devServer'] => ({ | ||
compress: true, | ||
devMiddleware: { | ||
publicPath: app.options.base, | ||
writeToDisk: false, | ||
stats: app.env.isDebug ? 'normal' : 'errors-warnings', | ||
}, | ||
firewall: false, | ||
headers: { | ||
'access-control-allow-origin': '*', | ||
}, | ||
historyApiFallback: { | ||
disableDotRule: true, | ||
rewrites: [{ from: /./, to: path.join(app.options.base, 'index.html') }], | ||
}, | ||
host: app.options.host, | ||
hot: true, | ||
onAfterSetupMiddleware: (server: WebpackDevServer) => { | ||
options.afterDevServer?.(server) | ||
}, | ||
onBeforeSetupMiddleware: (server: WebpackDevServer) => { | ||
// use trailing slash middleware to support vuepress routing in dev-server | ||
// it will be handled by most of the deployment platforms | ||
server.use(trailingSlashMiddleware) | ||
|
||
// plugin hook: beforeDevServer | ||
options.beforeDevServer?.(expressApp, server) | ||
}, | ||
open: app.options.open, | ||
port: app.options.port, | ||
static: { | ||
// `static.directory` will fail on Windows if we do not replace / with \ | ||
directory: app.dir.public().replace('/', sep), | ||
publicPath: app.options.base, | ||
watch: { | ||
ignoreInitial: true, | ||
ignored: [ | ||
// Do not watch node_modules | ||
'node_modules', | ||
], | ||
}, | ||
options.beforeDevServer?.(server) | ||
}, | ||
open: app.options.open, | ||
port: app.options.port, | ||
static: { | ||
// `static.directory` will fail on Windows if we do not replace / with \ | ||
directory: app.dir.public().replace('/', sep), | ||
publicPath: app.options.base, | ||
watch: { | ||
ignoreInitial: true, | ||
ignored: [ | ||
// Do not watch node_modules | ||
'node_modules', | ||
], | ||
}, | ||
} | ||
|
||
return serverConfig | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/@vuepress/bundler-webpack/src/types.webpack-dev-server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { FSWatcher } from 'chokidar' | ||
import type { Application } from 'express' | ||
import type { Compiler, WebpackOptionsNormalized } from 'webpack' | ||
|
||
export interface WebpackDevServer { | ||
app: Application | ||
compiler: Compiler | ||
// WebpackLogger | ||
logger: ReturnType<Compiler['getInfrastructureLogger']> | ||
// DevServer | ||
options: WebpackOptionsNormalized['devServer'] | ||
sockets: any[] | ||
staticWatchers: FSWatcher[] | ||
webSocketProxies: any[] | ||
webSocketHeartbeatInterval: number | ||
|
||
use: Application['use'] | ||
} |