diff --git a/lib/commands/build.js b/lib/commands/build.js index 62d8f226d4..7f017ca84e 100644 --- a/lib/commands/build.js +++ b/lib/commands/build.js @@ -1,4 +1,3 @@ -const webpackNiceLog = require('webpack-nicelog'); const webpack = require('webpack'); const path = require('path'); const chalk = require('chalk'); @@ -11,14 +10,13 @@ function compile(config) { return new Promise((resolve, reject) => { webpack(config, (err, stats) => { if (err) { - return reject(err); + 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 => { @@ -26,7 +24,6 @@ function compile(config) { }); } resolve(stats.toJson({modules: false})); - return true; }); }); } @@ -38,23 +35,14 @@ module.exports = async function build(siteDir, 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(); + const config = createProdConfig(props).toConfig(); // compile! await compile(config); - const {outDir} = props; - // copy static files + const {outDir} = props; const staticDir = path.resolve(siteDir, 'static'); const staticFiles = await globby(['**'], { cwd: staticDir diff --git a/lib/commands/start.js b/lib/commands/start.js index a150ad276d..cc373b79b4 100644 --- a/lib/commands/start.js +++ b/lib/commands/start.js @@ -10,8 +10,6 @@ const serveStatic = require('koa-static'); 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('../load'); const createDevConfig = require('../webpack/dev'); @@ -25,6 +23,7 @@ module.exports = async function start(siteDir, cliOptions = {}) { // Process all related files as a prop const props = await load(siteDir); + // Reload files processing if (!cliOptions.noWatch) { const reload = () => { load(siteDir).catch(err => { @@ -49,43 +48,17 @@ module.exports = async function start(siteDir, cliOptions = {}) { const port = await getPort(cliOptions.port); 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 - config = config.toConfig(); + const config = createDevConfig(props).toConfig(); const compiler = webpack(config); // webpack-serve - const nonExistentDir = path.resolve(__dirname, 'non-existent'); setTimeout(async () => { await serve( {}, { - content: [nonExistentDir], compiler, - open: false, + open: true, devMiddleware: { logLevel: 'silent' }, @@ -96,11 +69,16 @@ module.exports = async function start(siteDir, cliOptions = {}) { logLevel: 'error', port, add: app => { + // serve static files const staticDir = path.resolve(siteDir, 'static'); if (fs.existsSync(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( convert( history({ diff --git a/lib/webpack/dev.js b/lib/webpack/dev.js index 97e42404b0..0956603f76 100644 --- a/lib/webpack/dev.js +++ b/lib/webpack/dev.js @@ -1,4 +1,6 @@ const path = require('path'); +const HtmlWebpackPlugin = require('html-webpack-plugin'); +const webpackNiceLog = require('webpack-nicelog'); const createBaseConfig = require('./base'); module.exports = function createDevConfig(props) { @@ -6,5 +8,21 @@ module.exports = function createDevConfig(props) { 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; }; diff --git a/lib/webpack/prod.js b/lib/webpack/prod.js index d284f706d1..ee22a138bf 100644 --- a/lib/webpack/prod.js +++ b/lib/webpack/prod.js @@ -1,5 +1,6 @@ const path = require('path'); const staticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin'); +const webpackNiceLog = require('webpack-nicelog'); const createBaseConfig = require('./base'); module.exports = function createProdConfig(props) { @@ -13,9 +14,8 @@ module.exports = function createProdConfig(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); - config.plugin('StaticSiteGenerator').use(staticSiteGeneratorPlugin, [ { entry: 'main', @@ -27,6 +27,11 @@ module.exports = function createProdConfig(props) { paths } ]); + config.plugin('WebpackNiceLog').use(webpackNiceLog, [ + { + name: 'Production' + } + ]); return config; };