misc(v2): misc fixes

This commit is contained in:
Yangshun Tay 2019-03-04 22:39:07 -08:00
parent 95b0cb942f
commit d64581f2b4
9 changed files with 27 additions and 34 deletions

View file

@ -30,6 +30,7 @@ module.exports = {
'react/jsx-closing-bracket-location': OFF, // Conflicts with Prettier. 'react/jsx-closing-bracket-location': OFF, // Conflicts with Prettier.
'react/jsx-filename-extension': OFF, 'react/jsx-filename-extension': OFF,
'react/jsx-one-expression-per-line': OFF, 'react/jsx-one-expression-per-line': OFF,
'react/no-array-index-key': OFF, // Sometimes its ok, e.g. non-changing data.
'react/prop-types': OFF, 'react/prop-types': OFF,
'react/destructuring-assignment': OFF, // Too many lines. 'react/destructuring-assignment': OFF, // Too many lines.
'import/no-unresolved': WARNING, // Because it couldn't resolve webpack alias. 'import/no-unresolved': WARNING, // Because it couldn't resolve webpack alias.

View file

@ -47,7 +47,7 @@ module.exports = async function build(siteDir) {
let serverConfig = createServerConfig(props).toConfig(); let serverConfig = createServerConfig(props).toConfig();
let clientConfig = createClientConfig(props).toConfig(); let clientConfig = createClientConfig(props).toConfig();
// apply user webpack config // Apply user webpack config.
const { const {
siteConfig: {configureWebpack}, siteConfig: {configureWebpack},
} = props; } = props;
@ -61,7 +61,7 @@ module.exports = async function build(siteDir) {
// Build the server bundles (render the static HTML and pick client bundle) // Build the server bundles (render the static HTML and pick client bundle)
await compile(serverConfig); await compile(serverConfig);
// copy static files // Copy static files.
const {outDir} = props; const {outDir} = props;
const staticDir = path.resolve(siteDir, 'static'); const staticDir = path.resolve(siteDir, 'static');
const staticFiles = await globby(['**'], { const staticFiles = await globby(['**'], {
@ -75,7 +75,7 @@ module.exports = async function build(siteDir) {
}), }),
); );
// generate sitemap // Generate sitemap.
const sitemap = await createSitemap(props); const sitemap = await createSitemap(props);
const sitemapPath = path.join(outDir, 'sitemap.xml'); const sitemapPath = path.join(outDir, 'sitemap.xml');
await fs.writeFile(sitemapPath, sitemap); await fs.writeFile(sitemapPath, sitemap);

View file

@ -49,8 +49,8 @@ module.exports = async function start(siteDir, cliOptions = {}) {
const docsRelativeDir = props.siteConfig.customDocsPath; const docsRelativeDir = props.siteConfig.customDocsPath;
const fsWatcher = chokidar.watch( const fsWatcher = chokidar.watch(
[ [
// TODO: Watch plugin paths (e.g. blog)
`../${docsRelativeDir}/**/*.md`, `../${docsRelativeDir}/**/*.md`,
'blog/**/*.md',
loadConfig.configFileName, loadConfig.configFileName,
'sidebars.json', 'sidebars.json',
], ],
@ -59,11 +59,9 @@ module.exports = async function start(siteDir, cliOptions = {}) {
ignoreInitial: true, ignoreInitial: true,
}, },
); );
fsWatcher.on('add', reload); ['add', 'change', 'unlink', 'addDir', 'unlinkDir'].forEach(event =>
fsWatcher.on('change', reload); fsWatcher.on(event, reload),
fsWatcher.on('unlink', reload); );
fsWatcher.on('addDir', reload);
fsWatcher.on('unlinkDir', reload);
} }
const port = await getPort(cliOptions.port); const port = await getPort(cliOptions.port);

View file

@ -13,7 +13,7 @@ import App from './App';
import preload from './preload'; import preload from './preload';
import routes from '@generated/routes'; // eslint-disable-line import routes from '@generated/routes'; // eslint-disable-line
// Client side render (e.g: running in browser) to become single-page application (SPA) // Client-side render (e.g: running in browser) to become single-page application (SPA).
if (typeof window !== 'undefined' && typeof document !== 'undefined') { if (typeof window !== 'undefined' && typeof document !== 'undefined') {
preload(routes, window.location.pathname).then(() => { preload(routes, window.location.pathname).then(() => {
ReactDOM.render( ReactDOM.render(

View file

@ -50,8 +50,7 @@ export default function render(locals) {
const cssFiles = assets.filter(value => value.match(/\.css$/)); const cssFiles = assets.filter(value => value.match(/\.css$/));
const {baseUrl} = locals; const {baseUrl} = locals;
const html = ` return `<!DOCTYPE html>
<!DOCTYPE html>
<html${htmlAttributes ? ` ${htmlAttributes}` : ''}> <html${htmlAttributes ? ` ${htmlAttributes}` : ''}>
<head> <head>
${metaHtml} ${metaHtml}
@ -75,6 +74,5 @@ export default function render(locals) {
</body> </body>
</html> </html>
`; `;
return html;
}); });
} }

View file

@ -29,21 +29,17 @@ module.exports = async function createSitemap({
); );
} }
const urls = []; const urls = allMetadatas.map(metadata => ({
url: metadata.permalink,
changefreq: 'weekly',
priority: 0.5,
}));
allMetadatas.forEach(metadata => { const generatedSitemap = sitemap.createSitemap({
urls.push({
url: metadata.permalink,
changefreq: 'weekly',
priority: 0.5,
});
});
const sm = sitemap.createSitemap({
hostname: siteUrl, hostname: siteUrl,
cacheTime: 600 * 1000, // 600 sec - cache purge period cacheTime: 600 * 1000, // 600 sec - cache purge period
urls, urls,
}); });
return sm.toString(); return generatedSitemap.toString();
}; };

View file

@ -34,8 +34,8 @@ function BlogPage(props) {
</li> </li>
))} ))}
</ul> </ul>
{BlogPosts.map(BlogPost => ( {BlogPosts.map((BlogPost, index) => (
<BlogPost /> <BlogPost key={index} />
))} ))}
</div> </div>
</Layout> </Layout>

View file

@ -146,7 +146,7 @@ module.exports = function createBaseConfig(props, isServer) {
}).test(CSS_MODULE_REGEX); }).test(CSS_MODULE_REGEX);
// mini-css-extract plugin // mini-css-extract plugin
config.plugin('extract-css').use(CSSExtractPlugin, [ config.plugin('extractCSS').use(CSSExtractPlugin, [
{ {
filename: isProd ? '[name].[chunkhash].css' : '[name].css', filename: isProd ? '[name].[chunkhash].css' : '[name].css',
chunkFilename: isProd ? '[id].[chunkhash].css' : '[id].css', chunkFilename: isProd ? '[id].[chunkhash].css' : '[id].css',

View file

@ -5,11 +5,12 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
const cleanWebpackPlugin = require('clean-webpack-plugin');
const path = require('path'); const path = require('path');
const webpackNiceLog = require('webpack-nicelog'); const webpackNiceLog = require('webpack-nicelog');
const {StatsWriterPlugin} = require('webpack-stats-plugin'); const {StatsWriterPlugin} = require('webpack-stats-plugin');
const {ReactLoadablePlugin} = require('react-loadable/webpack'); const {ReactLoadablePlugin} = require('react-loadable/webpack');
const cleanWebpackPlugin = require('clean-webpack-plugin');
const createBaseConfig = require('./base'); const createBaseConfig = require('./base');
const {applyChainWebpack} = require('./utils'); const {applyChainWebpack} = require('./utils');
@ -17,18 +18,17 @@ module.exports = function createClientConfig(props) {
const config = createBaseConfig(props); const config = createBaseConfig(props);
config.entry('main').add(path.resolve(__dirname, '../core/clientEntry.js')); config.entry('main').add(path.resolve(__dirname, '../core/clientEntry.js'));
// Remove/clean build folders before building bundles.
const {outDir} = props; const {outDir} = props;
// Remove/clean build folders before building bundles.
config config
.plugin('clean') .plugin('clean')
.use(cleanWebpackPlugin, [outDir, {verbose: false, allowExternal: true}]); .use(cleanWebpackPlugin, [outDir, {verbose: false, allowExternal: true}]);
// Write webpack stats object so we can pickup correct client bundle path in server.
// write webpack stats object so we can pickup correct client bundle path in server.
config config
.plugin('client-stats') .plugin('clientStats')
.use(StatsWriterPlugin, [{filename: 'client.stats.json'}]); .use(StatsWriterPlugin, [{filename: 'client.stats.json'}]);
config config
.plugin('react-loadable-stats') .plugin('reactLoadableStats')
.use(ReactLoadablePlugin, [ .use(ReactLoadablePlugin, [
{filename: path.join(outDir, 'react-loadable.json')}, {filename: path.join(outDir, 'react-loadable.json')},
]); ]);
@ -39,7 +39,7 @@ module.exports = function createClientConfig(props) {
.plugin('niceLog') .plugin('niceLog')
.use(webpackNiceLog, [{name: 'Client', skipBuildTime: isProd}]); .use(webpackNiceLog, [{name: 'Client', skipBuildTime: isProd}]);
// user extended webpack-chain config // User-extended webpack-chain config.
applyChainWebpack(props.siteConfig.chainWebpack, config, false); applyChainWebpack(props.siteConfig.chainWebpack, config, false);
return config; return config;