feat: prototype blog post generation in dev server

This commit is contained in:
endiliey 2018-07-30 01:35:35 +08:00
parent 8cbd23d690
commit 7ecd4c9bef
9 changed files with 112 additions and 31 deletions

View file

@ -3,11 +3,22 @@ const fs = require('fs-extra');
const chalk = require('chalk');
const webpack = require('webpack');
const chokidar = require('chokidar');
const convert = require('koa-connect')
const range = require('koa-range')
const history = require('connect-history-api-fallback')
const portfinder = require('portfinder')
const serve = require('webpack-serve');
const webpackNiceLog = require('webpack-nicelog');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const load = require('./loader');
const createDevConfig = require('./webpack/dev');
async function getPort (port) {
portfinder.basePort = parseInt(port) || 8080
port = await portfinder.getPortPromise()
return port
}
module.exports = async function dev(sourceDir, cliOptions = {}) {
// load site props from preprocessed files in source directory
const props = await load(sourceDir);
@ -18,7 +29,7 @@ module.exports = async function dev(sourceDir, cliOptions = {}) {
console.error(chalk.red(err.stack));
});
};
const fsWatcher = chokidar.watch(['**/*.md'], {
const fsWatcher = chokidar.watch(['**/*.md', '.blogi/config.js'], {
cwd: sourceDir,
ignoreInitial: true
});
@ -31,7 +42,7 @@ module.exports = async function dev(sourceDir, cliOptions = {}) {
// resolve webpack config
let config = createDevConfig(props);
const port = cliOptions.port || 8080;
const port = await getPort(cliOptions.port);
const {publicPath} = props;
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
@ -46,6 +57,15 @@ module.exports = async function dev(sourceDir, cliOptions = {}) {
}
]);
config.plugin('html-webpack-plugin').use(HtmlWebpackPlugin, [
{
inject: false,
hash: true,
template: path.resolve(__dirname, 'core/index.html'),
filename: 'index.html'
}
]);
// create compiler from generated webpack config
config = config.toConfig();
const compiler = webpack(config);
@ -65,7 +85,15 @@ module.exports = async function dev(sourceDir, cliOptions = {}) {
logLevel: 'error'
},
logLevel: 'error',
port
port,
add: app => {
app.use(range) // enable range request https://tools.ietf.org/html/rfc7233
app.use(convert(history({
rewrites: [
{ from: /\.html$/, to: '/' }
]
})))
}
}
);
};