-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
/** | ||
* 服务端打包配置 | ||
*/ | ||
const { merge } = require('webpack-merge') | ||
const nodeExternals = require('webpack-node-externals') | ||
const baseConfig = require('./webpack.base.config.js') | ||
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin') | ||
const { merge } = require("webpack-merge"); | ||
const nodeExternals = require("webpack-node-externals"); | ||
const baseConfig = require("./webpack.base.config.js"); | ||
const VueSSRServerPlugin = require("vue-server-renderer/server-plugin"); | ||
|
||
module.exports = merge(baseConfig, { | ||
// 将 entry 指向应用程序的 server entry 文件 | ||
entry: './src/entry-server.js', | ||
entry: "./src/entry-server.js", | ||
|
||
// 这允许 webpack 以 Node 适用方式处理模块加载 | ||
// 并且还会在编译 Vue 组件时, | ||
// 告知 `vue-loader` 输送面向服务器代码(server-oriented code)。 | ||
target: 'node', | ||
target: "node", | ||
|
||
output: { | ||
filename: 'server-bundle.js', | ||
filename: "server-bundle.js", | ||
// 此处告知 server bundle 使用 Node 风格导出模块(Node-style exports) | ||
libraryTarget: 'commonjs2' | ||
libraryTarget: "commonjs2", | ||
}, | ||
|
||
// 不打包 node_modules 第三方包,而是保留 require 方式直接加载 | ||
externals: [nodeExternals({ | ||
// 白名单中的资源依然正常打包 | ||
allowlist: [/\.css$/] | ||
})], | ||
externals: [ | ||
nodeExternals({ | ||
// 白名单中的资源依然正常打包 | ||
allowlist: [/\.css$/], | ||
}), | ||
], | ||
|
||
plugins: [ | ||
// 这是将服务器的整个输出构建为单个 JSON 文件的插件。 | ||
// 默认文件名为 `vue-ssr-server-bundle.json` | ||
new VueSSRServerPlugin() | ||
] | ||
}) | ||
new VueSSRServerPlugin(), | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
/** | ||
* 客户端入口 | ||
*/ | ||
import { createApp } from './app' | ||
import { createApp } from "./app"; | ||
|
||
// 客户端特定引导逻辑…… | ||
|
||
const { app, router, store } = createApp() | ||
const { app, router, store } = createApp(); | ||
|
||
if (window.__INITIAL_STATE__) { | ||
store.replaceState(window.__INITIAL_STATE__) | ||
store.replaceState(window.__INITIAL_STATE__); | ||
} | ||
|
||
router.onReady(() => { | ||
app.$mount('#app') | ||
}) | ||
app.$mount("#app"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
// entry-server.js | ||
import { createApp } from './app' | ||
import { createApp } from "./app"; | ||
|
||
export default async context => { | ||
export default async (context) => { | ||
// 因为有可能会是异步路由钩子函数或组件,所以我们将返回一个 Promise, | ||
// 以便服务器能够等待所有的内容在渲染前, | ||
// 就已经准备就绪。 | ||
const { app, router, store } = createApp() | ||
// 以便服务器能够等待所有的内容在渲染前, | ||
// 就已经准备就绪。 | ||
const { app, router, store } = createApp(); | ||
|
||
const meta = app.$meta() | ||
const meta = app.$meta(); | ||
|
||
// 设置服务器端 router 的位置 | ||
router.push(context.url) | ||
router.push(context.url); | ||
|
||
context.meta = meta | ||
context.meta = meta; | ||
|
||
// 等到 router 将可能的异步组件和钩子函数解析完 | ||
await new Promise(router.onReady.bind(router)) | ||
await new Promise(router.onReady.bind(router)); | ||
|
||
context.rendered = () => { | ||
// Renderer 会把 context.state 数据对象内联到页面模板中 | ||
// 最终发送给客户端的页面中会包含一段脚本:window.__INITIAL_STATE__ = context.state | ||
// 客户端就要把页面中的 window.__INITIAL_STATE__ 拿出来填充到客户端 store 容器中 | ||
context.state = store.state | ||
} | ||
context.state = store.state; | ||
}; | ||
|
||
return app | ||
} | ||
return app; | ||
}; |