mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 00:27:21 +02:00
fix(v2): map loaded modules to bundles (#1056)
* fix(v2): map loaded modules to bundles * nits
This commit is contained in:
parent
2ce6ca5854
commit
5d60739f7c
3 changed files with 37 additions and 16 deletions
|
@ -2,20 +2,26 @@ import React from 'react';
|
||||||
import {StaticRouter} from 'react-router-dom';
|
import {StaticRouter} from 'react-router-dom';
|
||||||
import ReactDOMServer from 'react-dom/server';
|
import ReactDOMServer from 'react-dom/server';
|
||||||
import Helmet from 'react-helmet';
|
import Helmet from 'react-helmet';
|
||||||
|
import {getBundles} from 'react-loadable/webpack';
|
||||||
|
import Loadable from 'react-loadable';
|
||||||
|
|
||||||
import App from './App';
|
import reactLoadableStats from '@build/react-loadable.json'; //eslint-disable-line
|
||||||
import preload from './preload';
|
|
||||||
import routes from '@generated/routes'; // eslint-disable-line
|
|
||||||
import webpackClientStats from '@build/client.stats.json'; //eslint-disable-line
|
import webpackClientStats from '@build/client.stats.json'; //eslint-disable-line
|
||||||
|
import routes from '@generated/routes'; // eslint-disable-line
|
||||||
|
import preload from './preload';
|
||||||
|
import App from './App';
|
||||||
|
|
||||||
// Renderer for static-site-generator-webpack-plugin (async rendering via promises)
|
// Renderer for static-site-generator-webpack-plugin (async rendering via promises)
|
||||||
export default function render(locals) {
|
export default function render(locals) {
|
||||||
return preload(routes, locals.path).then(() => {
|
return preload(routes, locals.path).then(() => {
|
||||||
|
const modules = [];
|
||||||
const context = {};
|
const context = {};
|
||||||
const appHtml = ReactDOMServer.renderToString(
|
const appHtml = ReactDOMServer.renderToString(
|
||||||
|
<Loadable.Capture report={moduleName => modules.push(moduleName)}>
|
||||||
<StaticRouter location={locals.path} context={context}>
|
<StaticRouter location={locals.path} context={context}>
|
||||||
<App />
|
<App />
|
||||||
</StaticRouter>,
|
</StaticRouter>
|
||||||
|
</Loadable.Capture>,
|
||||||
);
|
);
|
||||||
|
|
||||||
const helmet = Helmet.renderStatic();
|
const helmet = Helmet.renderStatic();
|
||||||
|
@ -28,7 +34,11 @@ export default function render(locals) {
|
||||||
];
|
];
|
||||||
const metaHtml = metaStrings.filter(Boolean).join('\n ');
|
const metaHtml = metaStrings.filter(Boolean).join('\n ');
|
||||||
|
|
||||||
const assets = webpackClientStats.assetsByChunkName.main;
|
const bundles = getBundles(reactLoadableStats, modules);
|
||||||
|
const assets = [
|
||||||
|
...webpackClientStats.assetsByChunkName.main,
|
||||||
|
...bundles.map(b => b.file),
|
||||||
|
];
|
||||||
const jsFiles = assets.filter(value => value.match(/\.js$/));
|
const jsFiles = assets.filter(value => value.match(/\.js$/));
|
||||||
const cssFiles = assets.filter(value => value.match(/\.css$/));
|
const cssFiles = assets.filter(value => value.match(/\.css$/));
|
||||||
const {baseUrl} = locals;
|
const {baseUrl} = locals;
|
||||||
|
@ -40,17 +50,21 @@ export default function render(locals) {
|
||||||
${metaHtml}
|
${metaHtml}
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
${cssFiles.map(
|
${cssFiles
|
||||||
|
.map(
|
||||||
cssFile =>
|
cssFile =>
|
||||||
`<link rel="stylesheet" type="text/css" href="${baseUrl}${cssFile}" />`,
|
`<link rel="stylesheet" type="text/css" href="${baseUrl}${cssFile}" />`,
|
||||||
)}
|
)
|
||||||
|
.join('\n')}
|
||||||
</head>
|
</head>
|
||||||
<body${bodyAttributes ? ` ${bodyAttributes}` : ''}>
|
<body${bodyAttributes ? ` ${bodyAttributes}` : ''}>
|
||||||
<div id="app">${appHtml}</div>
|
<div id="app">${appHtml}</div>
|
||||||
${jsFiles.map(
|
${jsFiles
|
||||||
|
.map(
|
||||||
jsFile =>
|
jsFile =>
|
||||||
`<script type="text/javascript" src="${baseUrl}${jsFile}"></script>`,
|
`<script type="text/javascript" src="${baseUrl}${jsFile}"></script>`,
|
||||||
)}
|
)
|
||||||
|
.join('\n')}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`;
|
`;
|
||||||
|
|
|
@ -60,6 +60,7 @@ module.exports = function createBaseConfig(props, isServer) {
|
||||||
presets: ['@babel/env', '@babel/react'],
|
presets: ['@babel/env', '@babel/react'],
|
||||||
plugins: [
|
plugins: [
|
||||||
isServer ? 'dynamic-import-node' : '@babel/syntax-dynamic-import',
|
isServer ? 'dynamic-import-node' : '@babel/syntax-dynamic-import',
|
||||||
|
'react-loadable/babel',
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
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 cleanWebpackPlugin = require('clean-webpack-plugin');
|
const cleanWebpackPlugin = require('clean-webpack-plugin');
|
||||||
const createBaseConfig = require('./base');
|
const createBaseConfig = require('./base');
|
||||||
const {applyChainWebpack} = require('./utils');
|
const {applyChainWebpack} = require('./utils');
|
||||||
|
@ -17,8 +18,13 @@ module.exports = function createClientConfig(props) {
|
||||||
|
|
||||||
// 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('stats')
|
.plugin('client-stats')
|
||||||
.use(StatsWriterPlugin, [{filename: 'client.stats.json'}]);
|
.use(StatsWriterPlugin, [{filename: 'client.stats.json'}]);
|
||||||
|
config
|
||||||
|
.plugin('react-loadable-stats')
|
||||||
|
.use(ReactLoadablePlugin, [
|
||||||
|
{filename: path.join(outDir, 'react-loadable.json')},
|
||||||
|
]);
|
||||||
|
|
||||||
// show compilation progress bar and build time
|
// show compilation progress bar and build time
|
||||||
const isProd = process.env.NODE_ENV === 'production';
|
const isProd = process.env.NODE_ENV === 'production';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue