-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sunsetting webpack-serve #1195
Sunsetting webpack-serve #1195
Changes from all commits
25094ba
adf54be
958c036
737ade0
ea0840f
2a4342a
9c997c0
3b1a73f
97d8df9
f15d31a
8df8328
fc627f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
'use strict' | ||
|
||
module.exports = async function dev (sourceDir, cliOptions = {}) { | ||
module.exports = async (sourceDir, cliOptions = {}, ctx) => { | ||
const { server, host, port } = await prepareServer(sourceDir, cliOptions, ctx) | ||
server.listen(port, host, err => { | ||
if (err) { | ||
console.log(err) | ||
} | ||
}) | ||
} | ||
|
||
module.exports.prepare = prepareServer | ||
|
||
async function prepareServer (sourceDir, cliOptions = {}, context) { | ||
const WebpackDevServer = require('webpack-dev-server') | ||
const { path } = require('@vuepress/shared-utils') | ||
const webpack = require('webpack') | ||
const chokidar = require('chokidar') | ||
const serve = require('webpack-serve') | ||
const convert = require('koa-connect') | ||
const mount = require('koa-mount') | ||
const range = require('koa-range') | ||
const serveStatic = require('koa-static') | ||
const history = require('connect-history-api-fallback') | ||
|
||
const prepare = require('./prepare/index') | ||
const { chalk, fs, logger } = require('@vuepress/shared-utils') | ||
const { chalk, logger } = require('@vuepress/shared-utils') | ||
const HeadPlugin = require('./webpack/HeadPlugin') | ||
const DevLogPlugin = require('./webpack/DevLogPlugin') | ||
const createClientConfig = require('./webpack/createClientConfig') | ||
const { applyUserWebpackConfig } = require('./util/index') | ||
const { frontmatterEmitter } = require('@vuepress/markdown-loader') | ||
|
||
logger.wait('Extracting site metadata...') | ||
const ctx = await prepare(sourceDir, cliOptions, false /* isProd */) | ||
const ctx = context || await prepare(sourceDir, cliOptions, false /* isProd */) | ||
|
||
// setup watchers to update options and dynamically generated files | ||
const update = (reason) => { | ||
|
@@ -105,52 +110,52 @@ module.exports = async function dev (sourceDir, cliOptions = {}) { | |
config = applyUserWebpackConfig(userConfig, config, false /* isServer */) | ||
} | ||
|
||
const serverConfig = Object.assign({ | ||
disableHostCheck: true, | ||
compress: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅Used to bnable gzip compression for everything served. |
||
clientLogLevel: 'error', | ||
hot: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅Enable webpack's Hot Module Replacement feature. |
||
quiet: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
❌This option shouldn't be set to |
||
headers: { | ||
'access-control-allow-origin': '*' | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅This option was set to enable CQRS. |
||
publicPath: ctx.base, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ |
||
watchOptions: { | ||
ignored: /node_modules/ | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅Excluded such a huge folder. |
||
historyApiFallback: { | ||
rewrites: [ | ||
{ from: /\.html$/, to: '/' } | ||
] | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅Follow the past configuration. |
||
overlay: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅Do not need to show a full-screen overlay in the browser when there are compiler errors or warnings |
||
host, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ |
||
contentBase: path.resolve(sourceDir, '.vuepress/public'), | ||
before (app, server) { | ||
ctx.pluginAPI.options.beforeDevServer.syncApply(app, server) | ||
}, | ||
after (app, server) { | ||
ctx.pluginAPI.options.afterDevServer.syncApply(app, server) | ||
} | ||
}, ctx.siteConfig.devServer || {}) | ||
|
||
WebpackDevServer.addDevServerEntrypoints(config, serverConfig) | ||
|
||
const compiler = webpack(config) | ||
const server = new WebpackDevServer(compiler, serverConfig) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌We should create a Node.js API for e.g. custom a command to export current VuePress site to a PDF. const { dev } = require('vuepresss')
module.exports = {
extendCli (cli) {
cli
.command('export [targetDir]', 'export current vuepress site to a PDF')
.action(async (dir = '.') => {
const { port, host, server } = await dev.prepare()
server.listen(port, host, err => {
if (err) {
console.log(err)
}
})
// do somthing, such as launch browser to export PDF.
await generatePDF()
server.close()
})
}
} |
||
|
||
const nonExistentDir = path.resolve(__dirname, 'non-existent') | ||
await serve({ | ||
// avoid project cwd from being served. Otherwise if the user has index.html | ||
// in cwd it would break the server | ||
content: [nonExistentDir], | ||
compiler, | ||
return { | ||
server, | ||
host, | ||
dev: { logLevel: 'warn' }, | ||
hot: { | ||
port: port + 1, | ||
logLevel: 'error' | ||
}, | ||
logLevel: 'error', | ||
port, | ||
open: cliOptions.open, | ||
add: app => { | ||
// apply plugin options to extend dev server. | ||
ctx.pluginAPI.options.enhanceDevServer.syncApply(app) | ||
|
||
const userPublic = path.resolve(sourceDir, '.vuepress/public') | ||
|
||
// enable range request | ||
app.use(range) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅Express enable range request in development. so removing it will not break the fix of #555. |
||
|
||
// respect base when serving static files... | ||
if (fs.existsSync(userPublic)) { | ||
app.use(mount(ctx.base, serveStatic(userPublic))) | ||
} | ||
|
||
app.use(convert(history({ | ||
rewrites: [ | ||
{ from: /\.html$/, to: '/' } | ||
] | ||
}))) | ||
} | ||
}) | ||
ctx | ||
} | ||
} | ||
|
||
function resolveHost (host) { | ||
// webpack-serve hot updates doesn't work properly over 0.0.0.0 on Windows, | ||
// but localhost does not allow visiting over network :/ | ||
const defaultHost = process.platform === 'win32' ? 'localhost' : '0.0.0.0' | ||
const defaultHost = 'localhost' | ||
host = host || defaultHost | ||
const displayHost = host === defaultHost && process.platform !== 'win32' | ||
const displayHost = host === defaultHost | ||
? 'localhost' | ||
: host | ||
return { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('@vuepress/core') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅Used to bypasses host checking.