mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 00:57:53 +02:00
feat: add build command
This commit is contained in:
parent
b079e48976
commit
7a69cff30c
1 changed files with 55 additions and 1 deletions
|
@ -1,6 +1,60 @@
|
||||||
|
const webpackNiceLog = require('webpack-nicelog');
|
||||||
|
const webpack = require('webpack');
|
||||||
|
const path = require('path');
|
||||||
|
const chalk = require('chalk');
|
||||||
|
const load = require('../load');
|
||||||
|
const createProdConfig = require('../webpack/prod');
|
||||||
|
|
||||||
|
function compile(config) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
webpack(config, (err, stats) => {
|
||||||
|
if (err) {
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
if (stats.hasErrors()) {
|
||||||
|
stats.toJson().errors.forEach(e => {
|
||||||
|
console.error(e);
|
||||||
|
});
|
||||||
|
reject(new Error(`Failed to compile with errors.`));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (stats.hasWarnings()) {
|
||||||
|
stats.toJson().warnings.forEach(warning => {
|
||||||
|
console.warn(warning);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
resolve(stats.toJson({modules: false}));
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = async function build(siteDir, cliOptions = {}) {
|
module.exports = async function build(siteDir, cliOptions = {}) {
|
||||||
process.env.NODE_ENV = 'production';
|
process.env.NODE_ENV = 'production';
|
||||||
console.log('Build command invoked ...');
|
console.log('Build command invoked ...');
|
||||||
console.log(siteDir);
|
|
||||||
console.log(cliOptions);
|
console.log(cliOptions);
|
||||||
|
|
||||||
|
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
|
||||||
|
config = config.toConfig();
|
||||||
|
|
||||||
|
// compile!
|
||||||
|
await compile(config);
|
||||||
|
|
||||||
|
const {outDir} = props;
|
||||||
|
const relativeDir = path.relative(process.cwd(), outDir);
|
||||||
|
console.log(
|
||||||
|
`\n${chalk.green('Success!')} Generated static files in ${chalk.cyan(
|
||||||
|
relativeDir
|
||||||
|
)}.\n`
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue