Skip to content

Commit

Permalink
fix:修改案例
Browse files Browse the repository at this point in the history
  • Loading branch information
SunLxy committed Apr 6, 2022
1 parent 6bf643f commit 5c3e15d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 54 deletions.
2 changes: 1 addition & 1 deletion example/basic-routes-rematch-new/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logs = console.log; // eslint-disable-line

const server = http.createServer(app);
let currentApp = app;
const PORT = process.env.PORT || 3000;
const PORT = parseInt(process.env.PORT || 3000) + 1;
const HOST = process.env.HOST || 'localhost';
server.listen(PORT, (error) => {
if (error) {
Expand Down
18 changes: 3 additions & 15 deletions example/basic-routes-rematch-new/src/serverIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@ import { Provider } from 'react-redux';
import store from "./models"
import App from './routes';

// require 方式 打包报错
const assetsMainifest = new Function(`return ${FS.readFileSync(`${OUTPUT_PUBLIC_PATH}/asset-client-manifest.json`, "utf-8")}`)()

const assets = {}
const assetsMainifest = new Function(`return ${FS.readFileSync(`${OUTPUT_PUBLIC_PATH}/asset-client-manifest.json`, "utf-8")}`)()

if (assetsMainifest && assetsMainifest["entrypoints"]) {
Object.values(assetsMainifest.entrypoints).forEach((item) => {
if (/.css$/.test(item)) {
assets.css = item
}
if (/.js$/.test(item)) {
assets.js = item
}
})
}

const appDirectory = FS.realpathSync(process.cwd());
const resolveApp = (relativePath) => Path.resolve(appDirectory, relativePath);
Expand Down Expand Up @@ -61,11 +49,11 @@ server
<meta charset="utf-8" />
<title>Welcome to KKT</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${assets.css ? `<link rel="stylesheet" href="${assets.css}">` : ''}
${assetsMainifest.client.css ? `<link rel="stylesheet" href="${assetsMainifest.client.css}">` : ''}
</head>
<body>
<div id="root">${markup}</div>
${assets.js ? `<script src="${assets.js}" defer crossorigin></script>` : ""}
${assetsMainifest.client.js ? `<script src="${assetsMainifest.client.js}" defer crossorigin></script>` : ""}
</body>
</html>`);
}
Expand Down
12 changes: 6 additions & 6 deletions example/basic-routes/src/client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
// import App from './routes';
// const renderMethod = !!module.hot ? ReactDOM.render : ReactDOM.hydrate
import { BrowserRouter } from "react-router-dom"
import App from './routes';

ReactDOM.render(<div>222</div>, document.getElementById('root'));
ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, document.getElementById('root'));

// if (module.hot) {
// module.hot.accept();
// }
if (module.hot) {
module.hot.accept();
}
2 changes: 1 addition & 1 deletion example/basic-routes/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logs = console.log; // eslint-disable-line

const server = http.createServer(app);
let currentApp = app;
const PORT = process.env.PORT || 3000;
const PORT = parseInt(process.env.PORT || 3000) + 1;
const HOST = process.env.HOST || 'localhost';
server.listen(PORT, (error) => {
if (error) {
Expand Down
20 changes: 2 additions & 18 deletions example/basic-routes/src/serverIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,12 @@ import FS from 'fs';

import App from './routes';

// require 方式 打包报错
const assetsMainifest = new Function(`return ${FS.readFileSync(`${OUTPUT_PUBLIC_PATH}/asset-client-manifest.json`, "utf-8")}`)()

const assets = {}

if (assetsMainifest && assetsMainifest["entrypoints"]) {
Object.values(assetsMainifest.entrypoints).forEach((item) => {
if (/.css$/.test(item)) {
assets.css = item
}
if (/.js$/.test(item)) {
assets.js = item
}
})
}

const appDirectory = FS.realpathSync(process.cwd());
const resolveApp = (relativePath) => Path.resolve(appDirectory, relativePath);
const server = express();
// server.use(express.static(resolveApp("dist")))



const render = (props = {}) => {
const html = renderToString(
Expand Down Expand Up @@ -60,11 +44,11 @@ server
<meta charset="utf-8" />
<title>Welcome to KKT</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${assets.css ? `<link rel="stylesheet" href="${assets.css}">` : ''}
${assetsMainifest.client.css ? `<link rel="stylesheet" href="${assetsMainifest.client.css}">` : ''}
</head>
<body>
<div id="root">${markup}</div>
${assets.js ? `<script src="${assets.js}" defer crossorigin></script>` : ""}
${assetsMainifest.client.js ? `<script src="${assetsMainifest.client.js}" defer crossorigin></script>` : ""}
</body>
</html>`);
}
Expand Down
3 changes: 2 additions & 1 deletion example/basic-ts/src/serverIndex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import App from './app/App';

import Path from 'path';
import FS from 'fs';
// require 方式 打包报错

// @ts-ignore
const assetsMainifest = new Function(`return ${FS.readFileSync(`${OUTPUT_PUBLIC_PATH}/asset-client-manifest.json`, "utf-8")}`)()


const appDirectory = FS.realpathSync(process.cwd());
const resolveApp = (relativePath: string) => Path.resolve(appDirectory, relativePath);
const server = express();
Expand Down
20 changes: 10 additions & 10 deletions example/react-router-rematch-old/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ server.listen(PORT, (error) => {
logs('🚀 started!', `PORT: http://localhost:${PORT}`);
});

// if (module.hot) {
// logs('✅ Server-side HMR Enabled!');
// module.hot.accept('./serverIndex', () => {
// logs('🔁 HMR Reloading `./serverIndex`...');
// server.removeListener('request', currentApp);
// const newApp = require('./serverIndex').default; // eslint-disable-line
// server.on('request', newApp);
// currentApp = newApp;
// });
// }
if (module.hot) {
logs('✅ Server-side HMR Enabled!');
module.hot.accept('./serverIndex', () => {
logs('🔁 HMR Reloading `./serverIndex`...');
server.removeListener('request', currentApp);
const newApp = require('./serverIndex').default; // eslint-disable-line
server.on('request', newApp);
currentApp = newApp;
});
}
3 changes: 1 addition & 2 deletions example/react-router-rematch-old/src/serverIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import Path from 'path';
import FS from 'fs';
import { createStore } from './store';

const assetPath = `${OUTPUT_PUBLIC_PATH}/asset-client-manifest.json`
const assetsMainifest = new Function(`return ${FS.readFileSync(`${OUTPUT_PUBLIC_PATH}/asset-client-manifest.json`, "utf-8")}`)()

const assetsMainifest = new Function(`return ${FS.readFileSync(`${assetPath}`, "utf-8")}`)()

const appDirectory = FS.realpathSync(process.cwd());
const resolveApp = (relativePath) => Path.resolve(appDirectory, relativePath);
Expand Down

0 comments on commit 5c3e15d

Please sign in to comment.