mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-08 22:03:01 +02:00
refactor: dev
This commit is contained in:
parent
d9a86a54c1
commit
9070fb50ab
2 changed files with 117 additions and 119 deletions
216
lib/dev.js
216
lib/dev.js
|
@ -1,109 +1,107 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
const webpack = require('webpack');
|
const webpack = require('webpack');
|
||||||
const chokidar = require('chokidar');
|
const chokidar = require('chokidar');
|
||||||
const convert = require('koa-connect');
|
const convert = require('koa-connect');
|
||||||
const range = require('koa-range');
|
const range = require('koa-range');
|
||||||
const mount = require('koa-mount');
|
const mount = require('koa-mount');
|
||||||
const serveStatic = require('koa-static');
|
const serveStatic = require('koa-static');
|
||||||
const history = require('connect-history-api-fallback');
|
const history = require('connect-history-api-fallback');
|
||||||
const portfinder = require('portfinder');
|
const portfinder = require('portfinder');
|
||||||
const serve = require('webpack-serve');
|
const serve = require('webpack-serve');
|
||||||
const serveWaitpage = require('webpack-serve-waitpage');
|
const serveWaitpage = require('webpack-serve-waitpage');
|
||||||
const webpackNiceLog = require('webpack-nicelog');
|
const webpackNiceLog = require('webpack-nicelog');
|
||||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||||
const load = require('./loader');
|
const load = require('./loader');
|
||||||
const createDevConfig = require('./webpack/dev');
|
const createDevConfig = require('./webpack/dev');
|
||||||
|
|
||||||
async function getPort(port) {
|
async function getPort(port) {
|
||||||
portfinder.basePort = parseInt(port, 10) || 8080;
|
portfinder.basePort = parseInt(port, 10) || 8080;
|
||||||
return await portfinder.getPortPromise();
|
return await portfinder.getPortPromise();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = async function dev(sourceDir, cliOptions = {}) {
|
module.exports = async function dev(sourceDir, cliOptions = {}) {
|
||||||
// load site props from preprocessed files in source directory
|
// load site props from preprocessed files in source directory
|
||||||
const props = await load(sourceDir);
|
const props = await load(sourceDir);
|
||||||
|
|
||||||
// Reload for any add/change/remove of file
|
// Reload for any add/change/remove of file
|
||||||
const reload = () => {
|
const reload = () => {
|
||||||
load(sourceDir).catch(err => {
|
load(sourceDir).catch(err => {
|
||||||
console.error(chalk.red(err.stack));
|
console.error(chalk.red(err.stack));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const fsWatcher = chokidar.watch(['**/*.md', 'config.js'], {
|
const fsWatcher = chokidar.watch(['**/*.md', 'config.js'], {
|
||||||
cwd: sourceDir,
|
cwd: sourceDir,
|
||||||
ignoreInitial: true
|
ignoreInitial: true
|
||||||
});
|
});
|
||||||
fsWatcher.on('add', reload);
|
fsWatcher.on('add', reload);
|
||||||
fsWatcher.on('change', reload);
|
fsWatcher.on('change', reload);
|
||||||
fsWatcher.on('unlink', reload);
|
fsWatcher.on('unlink', reload);
|
||||||
fsWatcher.on('addDir', reload);
|
fsWatcher.on('addDir', reload);
|
||||||
fsWatcher.on('unlinkDir', reload);
|
fsWatcher.on('unlinkDir', reload);
|
||||||
|
|
||||||
// resolve webpack config
|
const port = await getPort(cliOptions.port);
|
||||||
let config = createDevConfig(props);
|
const {publicPath} = props;
|
||||||
|
|
||||||
const port = await getPort(cliOptions.port);
|
// resolve webpack config
|
||||||
const {publicPath} = props;
|
let config = createDevConfig(props);
|
||||||
|
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
|
||||||
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
|
{
|
||||||
{
|
onDone: () => {
|
||||||
onDone: () => {
|
console.log(
|
||||||
console.log(
|
`\n${chalk.blue('Development server available at ')}${chalk.cyan(
|
||||||
`\n${chalk.blue('Development server available at ')}${chalk.cyan(
|
`http://localhost:${port}${publicPath}`
|
||||||
`http://localhost:${port}${publicPath}`
|
)}`
|
||||||
)}`
|
);
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
}
|
]);
|
||||||
]);
|
config.plugin('html-webpack-plugin').use(HtmlWebpackPlugin, [
|
||||||
|
{
|
||||||
config.plugin('html-webpack-plugin').use(HtmlWebpackPlugin, [
|
inject: false,
|
||||||
{
|
hash: true,
|
||||||
inject: false,
|
template: path.resolve(__dirname, 'core/index.html'),
|
||||||
hash: true,
|
filename: 'index.html'
|
||||||
template: path.resolve(__dirname, 'core/index.html'),
|
}
|
||||||
filename: 'index.html'
|
]);
|
||||||
}
|
|
||||||
]);
|
// create compiler from generated webpack config
|
||||||
|
config = config.toConfig();
|
||||||
// create compiler from generated webpack config
|
const compiler = webpack(config);
|
||||||
config = config.toConfig();
|
|
||||||
const compiler = webpack(config);
|
// webpack-serve
|
||||||
|
const nonExistentDir = path.resolve(__dirname, 'non-existent');
|
||||||
// webpack-serve
|
await serve(
|
||||||
const nonExistentDir = path.resolve(__dirname, 'non-existent');
|
{},
|
||||||
await serve(
|
{
|
||||||
{},
|
content: [nonExistentDir],
|
||||||
{
|
compiler,
|
||||||
content: [nonExistentDir],
|
open: true,
|
||||||
compiler,
|
devMiddleware: {
|
||||||
open: true,
|
logLevel: 'silent'
|
||||||
devMiddleware: {
|
},
|
||||||
logLevel: 'silent'
|
hotClient: {
|
||||||
},
|
port: port + 1,
|
||||||
hotClient: {
|
logLevel: 'error'
|
||||||
port: port + 1,
|
},
|
||||||
logLevel: 'error'
|
logLevel: 'error',
|
||||||
},
|
port,
|
||||||
logLevel: 'error',
|
add: (app, middleware, options) => {
|
||||||
port,
|
const staticDir = path.resolve(sourceDir, 'public');
|
||||||
add: (app, middleware, options) => {
|
if (fs.existsSync(staticDir)) {
|
||||||
const staticDir = path.resolve(sourceDir, 'public');
|
app.use(mount(publicPath, serveStatic(staticDir)));
|
||||||
if (fs.existsSync(staticDir)) {
|
}
|
||||||
app.use(mount(publicPath, serveStatic(staticDir)));
|
app.use(serveWaitpage(options, {theme: 'dark'})); // https://github.com/elisherer/webpack-serve-waitpage#middleware-options
|
||||||
}
|
app.use(range); // enable range request https://tools.ietf.org/html/rfc7233
|
||||||
app.use(serveWaitpage(options, {theme: 'dark'})); // https://github.com/elisherer/webpack-serve-waitpage#middleware-options
|
app.use(
|
||||||
app.use(range); // enable range request https://tools.ietf.org/html/rfc7233
|
convert(
|
||||||
app.use(
|
history({
|
||||||
convert(
|
rewrites: [{from: /\.html$/, to: '/'}]
|
||||||
history({
|
})
|
||||||
rewrites: [{from: /\.html$/, to: '/'}]
|
)
|
||||||
})
|
);
|
||||||
)
|
}
|
||||||
);
|
}
|
||||||
}
|
);
|
||||||
}
|
};
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const createBaseConfig = require('./base');
|
const createBaseConfig = require('./base');
|
||||||
|
|
||||||
module.exports = function createDevConfig(props) {
|
module.exports = function createDevConfig(props) {
|
||||||
const config = createBaseConfig(props);
|
const config = createBaseConfig(props);
|
||||||
|
|
||||||
config.entry('main').add(path.resolve(__dirname, '../core/index.js'));
|
config.entry('main').add(path.resolve(__dirname, '../core/index.js'));
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue