Skip to content

Commit

Permalink
fix(bundler-webpack): partially fix ssr dependencies issue
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Dec 10, 2022
1 parent b4afa32 commit 83e7b6c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
"type": "node-terminal",
"request": "launch",
"command": "pnpm docs:build"
},
{
"name": "docs:dev (webpack)",
"type": "node-terminal",
"request": "launch",
"command": "DOCS_BUNDLER=webpack pnpm docs:dev"
},
{
"name": "docs:build (webpack)",
"type": "node-terminal",
"request": "launch",
"command": "DOCS_BUNDLER=webpack pnpm docs:build"
}
]
}
9 changes: 2 additions & 7 deletions packages/bundler-webpack/src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,8 @@ export const build = async (
const clientManifest: ClientManifest = await fs.readJson(clientManifestPath)

// resolve client files meta
const {
allFilesMeta,
initialFilesMeta,
asyncFilesMeta,
moduleFilesMetaMap,
} = resolveClientManifestMeta(clientManifest)
const { initialFilesMeta, asyncFilesMeta, moduleFilesMetaMap } =
resolveClientManifestMeta(clientManifest)

// load the compiled server bundle
const serverEntryPath = app.dir.temp('.server/app.cjs')
Expand All @@ -107,7 +103,6 @@ export const build = async (
vueRouter,
renderToString,
ssrTemplate,
allFilesMeta,
initialFilesMeta,
asyncFilesMeta,
moduleFilesMetaMap,
Expand Down
2 changes: 0 additions & 2 deletions packages/bundler-webpack/src/build/renderPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const renderPage = async ({
vueRouter,
renderToString,
ssrTemplate,
allFilesMeta,
initialFilesMeta,
asyncFilesMeta,
moduleFilesMetaMap,
Expand All @@ -42,7 +41,6 @@ export const renderPage = async ({
vueRouter: Router
renderToString: (input: VueApp, context: SSRContext) => Promise<string>
ssrTemplate: string
allFilesMeta: FileMeta[]
initialFilesMeta: FileMeta[]
asyncFilesMeta: FileMeta[]
moduleFilesMetaMap: ModuleFilesMetaMap
Expand Down
8 changes: 3 additions & 5 deletions packages/bundler-webpack/src/build/ssr/createClientPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ export const createClientPlugin = (
'vuepress-client-plugin',
async (compilation) => {
// get webpack stats object
const stats: StatsToJsonOutput = compilation
.getStats()
.toJson() as unknown as StatsToJsonOutput

const {
assets = [],
modules = [],
entrypoints = {},
chunks = [],
} = stats
}: StatsToJsonOutput = compilation
.getStats()
.toJson() as unknown as StatsToJsonOutput

// get all files
const allFiles = assets.map((a) => a.name)
Expand Down
12 changes: 8 additions & 4 deletions packages/bundler-webpack/src/build/ssr/vuepressLoader.cts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ module.exports = function vuepressLoader(source: string): string {
// get `request` from loader context
const { request } = this as any

if (!request.endsWith('.vue')) return source

// add `request` to `ssrContext._registeredComponents` to handle SSR dependencies
// notice that this could only handle those sfc that cannot use inline template
// see https://github.com/vuejs/vue-loader/blob/1b1a195612f885a8dec3f371edf1cb8b35d341e4/src/index.ts#L167-L183
return source.replace(
'script.ssrRender = ssrRender',
`\
/import { ssrRender } from (.*)\n/,
`import { ssrRender as _ssrRender } from $1
import { ssrContextKey } from 'vue'
script.ssrRender = (...args) => {
const ssrRender = (...args) => {
const ssrContext = args[2].appContext.provides[ssrContextKey]
ssrContext._registeredComponents.add(${JSON.stringify(request)})
return ssrRender(...args)
return _ssrRender(...args)
}
`
)
Expand Down

0 comments on commit 83e7b6c

Please sign in to comment.