feat: add koa-static to serve static files

This commit is contained in:
endiliey 2018-08-01 16:06:52 +08:00
parent c5dfddaf61
commit 0c69604fb2
4 changed files with 29 additions and 6 deletions

View file

@ -5,6 +5,7 @@ const webpack = require('webpack');
const chokidar = require('chokidar');
const convert = require('koa-connect');
const range = require('koa-range');
const serveStatic = require('koa-static');
const history = require('connect-history-api-fallback');
const portfinder = require('portfinder');
const serve = require('webpack-serve');
@ -71,14 +72,15 @@ module.exports = async function dev(sourceDir, cliOptions = {}) {
const compiler = webpack(config);
// webpack-serve
const nonExistentDir = path.resolve(__dirname, 'non-existent')
await serve(
{},
{
content: [nonExistentDir],
compiler,
open: false, // don't open browser automatically
open: true,
devMiddleware: {
logLevel: 'silent',
publicPath
logLevel: 'silent'
},
hotClient: {
port: port + 1,
@ -87,6 +89,10 @@ module.exports = async function dev(sourceDir, cliOptions = {}) {
logLevel: 'error',
port,
add: app => {
const staticDir = path.resolve(sourceDir, 'public');
if (fs.existsSync(staticDir)) {
app.use(mount(publicPath, serveStatic(staticDir)));
}
app.use(range); // enable range request https://tools.ietf.org/html/rfc7233
app.use(
convert(