feat: code split & use react helmet

This commit is contained in:
endiliey 2018-08-23 21:46:14 +08:00
parent bf1e30dc52
commit 406106b67e
19 changed files with 241 additions and 146 deletions

View file

@ -4,7 +4,8 @@ const chalk = require('chalk');
const fs = require('fs-extra');
const globby = require('globby');
const load = require('../load');
const createProdConfig = require('../webpack/prod');
const createServerConfig = require('../webpack/server');
const createClientConfig = require('../webpack/client');
function compile(config) {
return new Promise((resolve, reject) => {
@ -35,11 +36,14 @@ module.exports = async function build(siteDir, cliOptions = {}) {
const props = await load(siteDir);
// create compiler from generated webpack config
const config = createProdConfig(props).toConfig();
const serverConfig = createServerConfig(props).toConfig();
const clientConfig = createClientConfig(props).toConfig();
// compile!
await compile(config);
// we build the client bundles first
await compile(clientConfig);
// then we build the server bundles (render the static HTML and pick client bundle)
await compile(serverConfig);
// copy static files
const {outDir} = props;

View file

@ -10,8 +10,9 @@ const serveStatic = require('koa-static');
const history = require('connect-history-api-fallback');
const portfinder = require('portfinder');
const serve = require('webpack-serve');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const load = require('../load');
const createDevConfig = require('../webpack/dev');
const createClientConfig = require('../webpack/client');
async function getPort(reqPort) {
portfinder.basePort = parseInt(reqPort, 10) || 3000;
@ -52,7 +53,18 @@ module.exports = async function start(siteDir, cliOptions = {}) {
const {baseUrl} = props;
// create compiler from generated webpack config
const config = createDevConfig(props).toConfig();
let config = createClientConfig(props);
const {siteConfig} = props;
config.plugin('html-webpack-plugin').use(HtmlWebpackPlugin, [
{
hash: true,
template: path.resolve(__dirname, '../core/devTemplate.ejs'),
filename: 'index.html',
title: siteConfig.title
}
]);
config = config.toConfig();
const compiler = webpack(config);
// webpack-serve