docusaurus/lib/dev.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-07-28 08:25:38 -04:00
const path = require('path');
2018-07-29 02:54:34 -04:00
const fs = require('fs-extra');
2018-07-28 08:25:38 -04:00
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')
2018-07-28 08:25:38 -04:00
const serve = require('webpack-serve');
2018-07-29 07:03:57 -04:00
const webpackNiceLog = require('webpack-nicelog');
const HtmlWebpackPlugin = require('html-webpack-plugin');
2018-07-28 08:25:38 -04:00
const load = require('./loader');
const createDevConfig = require('./webpack/dev');
async function getPort (port) {
portfinder.basePort = parseInt(port) || 8080
port = await portfinder.getPortPromise()
return port
}
2018-07-28 08:25:38 -04:00
module.exports = async function dev(sourceDir, cliOptions = {}) {
// load site props from preprocessed files in source directory
const props = await load(sourceDir);
2018-07-28 13:07:08 -04:00
// Reload for any add/change/remove of file
2018-07-28 08:25:38 -04:00
const reload = () => {
2018-07-28 13:07:08 -04:00
load(sourceDir).catch(err => {
console.error(chalk.red(err.stack));
});
2018-07-28 08:25:38 -04:00
};
const fsWatcher = chokidar.watch(['**/*.md', '.blogi/config.js'], {
2018-07-28 08:25:38 -04:00
cwd: sourceDir,
ignoreInitial: true
});
fsWatcher.on('add', reload);
fsWatcher.on('change', reload);
fsWatcher.on('unlink', reload);
fsWatcher.on('addDir', reload);
fsWatcher.on('unlinkDir', reload);
// resolve webpack config
let config = createDevConfig(props);
const port = await getPort(cliOptions.port);
2018-07-28 13:14:32 -04:00
const {publicPath} = props;
2018-07-28 13:07:08 -04:00
2018-07-29 07:03:57 -04:00
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
2018-07-28 13:14:32 -04:00
{
2018-07-29 07:03:57 -04:00
onDone: () => {
console.log(
`\n${chalk.blue('Development server available at ')}${chalk.cyan(
`http://localhost:${port}${publicPath}`
)}`
);
}
2018-07-28 13:14:32 -04:00
}
]);
2018-07-28 13:07:08 -04:00
config.plugin('html-webpack-plugin').use(HtmlWebpackPlugin, [
{
inject: false,
hash: true,
template: path.resolve(__dirname, 'core/index.html'),
filename: 'index.html'
}
]);
2018-07-28 08:25:38 -04:00
// create compiler from generated webpack config
config = config.toConfig();
const compiler = webpack(config);
// webpack-serve
await serve(
{},
{
compiler,
2018-07-29 02:54:34 -04:00
open: false, // don't open browser automatically
2018-07-28 13:07:08 -04:00
devMiddleware: {
2018-07-29 07:03:57 -04:00
logLevel: 'silent',
2018-07-28 13:14:32 -04:00
publicPath
2018-07-28 13:07:08 -04:00
},
2018-07-28 08:25:38 -04:00
hotClient: {
port: port + 1,
logLevel: 'error'
},
logLevel: 'error',
port,
add: app => {
app.use(range) // enable range request https://tools.ietf.org/html/rfc7233
app.use(convert(history({
rewrites: [
{ from: /\.html$/, to: '/' }
]
})))
}
2018-07-28 08:25:38 -04:00
}
);
};