mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 16:47:26 +02:00
refactor: code
This commit is contained in:
parent
a9c3d50a68
commit
44122cd202
4 changed files with 37 additions and 48 deletions
|
@ -1,4 +1,3 @@
|
||||||
const webpackNiceLog = require('webpack-nicelog');
|
|
||||||
const webpack = require('webpack');
|
const webpack = require('webpack');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
|
@ -11,14 +10,13 @@ function compile(config) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
webpack(config, (err, stats) => {
|
webpack(config, (err, stats) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err);
|
reject(err);
|
||||||
}
|
}
|
||||||
if (stats.hasErrors()) {
|
if (stats.hasErrors()) {
|
||||||
stats.toJson().errors.forEach(e => {
|
stats.toJson().errors.forEach(e => {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
});
|
});
|
||||||
reject(new Error(`Failed to compile with errors.`));
|
reject(new Error(`Failed to compile with errors.`));
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
if (stats.hasWarnings()) {
|
if (stats.hasWarnings()) {
|
||||||
stats.toJson().warnings.forEach(warning => {
|
stats.toJson().warnings.forEach(warning => {
|
||||||
|
@ -26,7 +24,6 @@ function compile(config) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
resolve(stats.toJson({modules: false}));
|
resolve(stats.toJson({modules: false}));
|
||||||
return true;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -38,23 +35,14 @@ module.exports = async function build(siteDir, cliOptions = {}) {
|
||||||
|
|
||||||
const props = await load(siteDir);
|
const props = await load(siteDir);
|
||||||
|
|
||||||
// resolve webpack config
|
|
||||||
let config = createProdConfig(props);
|
|
||||||
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
|
|
||||||
{
|
|
||||||
name: 'Production'
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
|
|
||||||
// create compiler from generated webpack config
|
// create compiler from generated webpack config
|
||||||
config = config.toConfig();
|
const config = createProdConfig(props).toConfig();
|
||||||
|
|
||||||
// compile!
|
// compile!
|
||||||
await compile(config);
|
await compile(config);
|
||||||
|
|
||||||
const {outDir} = props;
|
|
||||||
|
|
||||||
// copy static files
|
// copy static files
|
||||||
|
const {outDir} = props;
|
||||||
const staticDir = path.resolve(siteDir, 'static');
|
const staticDir = path.resolve(siteDir, 'static');
|
||||||
const staticFiles = await globby(['**'], {
|
const staticFiles = await globby(['**'], {
|
||||||
cwd: staticDir
|
cwd: staticDir
|
||||||
|
|
|
@ -10,8 +10,6 @@ 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 webpackNiceLog = require('webpack-nicelog');
|
|
||||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
||||||
const load = require('../load');
|
const load = require('../load');
|
||||||
const createDevConfig = require('../webpack/dev');
|
const createDevConfig = require('../webpack/dev');
|
||||||
|
|
||||||
|
@ -25,6 +23,7 @@ module.exports = async function start(siteDir, cliOptions = {}) {
|
||||||
// Process all related files as a prop
|
// Process all related files as a prop
|
||||||
const props = await load(siteDir);
|
const props = await load(siteDir);
|
||||||
|
|
||||||
|
// Reload files processing
|
||||||
if (!cliOptions.noWatch) {
|
if (!cliOptions.noWatch) {
|
||||||
const reload = () => {
|
const reload = () => {
|
||||||
load(siteDir).catch(err => {
|
load(siteDir).catch(err => {
|
||||||
|
@ -49,43 +48,17 @@ module.exports = async function start(siteDir, cliOptions = {}) {
|
||||||
const port = await getPort(cliOptions.port);
|
const port = await getPort(cliOptions.port);
|
||||||
const {baseUrl} = props;
|
const {baseUrl} = props;
|
||||||
|
|
||||||
// resolve webpack config
|
|
||||||
let config = createDevConfig(props);
|
|
||||||
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
|
|
||||||
{
|
|
||||||
name: 'Munseo',
|
|
||||||
onDone: () => {
|
|
||||||
console.log(
|
|
||||||
`\n${chalk.blue('Development server available at ')}${chalk.cyan(
|
|
||||||
`http://localhost:${port}${baseUrl}`
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
config.plugin('html-webpack-plugin').use(HtmlWebpackPlugin, [
|
|
||||||
{
|
|
||||||
inject: false,
|
|
||||||
hash: true,
|
|
||||||
template: path.resolve(__dirname, '../core/devTemplate.ejs'),
|
|
||||||
filename: 'index.html',
|
|
||||||
title: props.siteConfig.title
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
|
|
||||||
// create compiler from generated webpack config
|
// create compiler from generated webpack config
|
||||||
config = config.toConfig();
|
const config = createDevConfig(props).toConfig();
|
||||||
const compiler = webpack(config);
|
const compiler = webpack(config);
|
||||||
|
|
||||||
// webpack-serve
|
// webpack-serve
|
||||||
const nonExistentDir = path.resolve(__dirname, 'non-existent');
|
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
await serve(
|
await serve(
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
content: [nonExistentDir],
|
|
||||||
compiler,
|
compiler,
|
||||||
open: false,
|
open: true,
|
||||||
devMiddleware: {
|
devMiddleware: {
|
||||||
logLevel: 'silent'
|
logLevel: 'silent'
|
||||||
},
|
},
|
||||||
|
@ -96,11 +69,16 @@ module.exports = async function start(siteDir, cliOptions = {}) {
|
||||||
logLevel: 'error',
|
logLevel: 'error',
|
||||||
port,
|
port,
|
||||||
add: app => {
|
add: app => {
|
||||||
|
// serve static files
|
||||||
const staticDir = path.resolve(siteDir, 'static');
|
const staticDir = path.resolve(siteDir, 'static');
|
||||||
if (fs.existsSync(staticDir)) {
|
if (fs.existsSync(staticDir)) {
|
||||||
app.use(mount(baseUrl, serveStatic(staticDir)));
|
app.use(mount(baseUrl, serveStatic(staticDir)));
|
||||||
}
|
}
|
||||||
app.use(range); // enable range request https://tools.ietf.org/html/rfc7233
|
|
||||||
|
// enable HTTP range requests
|
||||||
|
app.use(range);
|
||||||
|
|
||||||
|
// rewrite request to `/` since this is a SPA
|
||||||
app.use(
|
app.use(
|
||||||
convert(
|
convert(
|
||||||
history({
|
history({
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||||
|
const webpackNiceLog = require('webpack-nicelog');
|
||||||
const createBaseConfig = require('./base');
|
const createBaseConfig = require('./base');
|
||||||
|
|
||||||
module.exports = function createDevConfig(props) {
|
module.exports = function createDevConfig(props) {
|
||||||
|
@ -6,5 +8,21 @@ module.exports = function createDevConfig(props) {
|
||||||
|
|
||||||
config.entry('main').add(path.resolve(__dirname, '../core/devEntry.js'));
|
config.entry('main').add(path.resolve(__dirname, '../core/devEntry.js'));
|
||||||
|
|
||||||
|
const {siteConfig} = props;
|
||||||
|
config.plugin('html-webpack-plugin').use(HtmlWebpackPlugin, [
|
||||||
|
{
|
||||||
|
inject: false,
|
||||||
|
hash: true,
|
||||||
|
template: path.resolve(__dirname, '../core/devTemplate.ejs'),
|
||||||
|
filename: 'index.html',
|
||||||
|
title: siteConfig.title
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
|
||||||
|
{
|
||||||
|
name: 'Development'
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const staticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin');
|
const staticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin');
|
||||||
|
const webpackNiceLog = require('webpack-nicelog');
|
||||||
const createBaseConfig = require('./base');
|
const createBaseConfig = require('./base');
|
||||||
|
|
||||||
module.exports = function createProdConfig(props) {
|
module.exports = function createProdConfig(props) {
|
||||||
|
@ -13,9 +14,8 @@ module.exports = function createProdConfig(props) {
|
||||||
|
|
||||||
const {siteConfig, docsData, pagesData} = props;
|
const {siteConfig, docsData, pagesData} = props;
|
||||||
|
|
||||||
// Find all available paths
|
// Find all available paths to be rendered
|
||||||
const paths = [...docsData, ...pagesData].map(data => data.path);
|
const paths = [...docsData, ...pagesData].map(data => data.path);
|
||||||
|
|
||||||
config.plugin('StaticSiteGenerator').use(staticSiteGeneratorPlugin, [
|
config.plugin('StaticSiteGenerator').use(staticSiteGeneratorPlugin, [
|
||||||
{
|
{
|
||||||
entry: 'main',
|
entry: 'main',
|
||||||
|
@ -27,6 +27,11 @@ module.exports = function createProdConfig(props) {
|
||||||
paths
|
paths
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
|
||||||
|
{
|
||||||
|
name: 'Production'
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue