Skip to content

Commit

Permalink
refactor(bundler-webpack): rename vuepress-ssr-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Sep 9, 2024
1 parent a5294d5 commit 240e7fe
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/bundler-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "meteorlxy",
"type": "module",
"imports": {
"#vuepress-loader": "./dist/vuepress-loader.cjs"
"#vuepress-ssr-loader": "./dist/vuepress-ssr-loader.cjs"
},
"exports": {
".": "./dist/index.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/bundler-webpack/src/build/createClientConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export const createClientConfig = async (
isBuild: true,
})

// use internal vuepress-loader to handle SSR dependencies
// use internal vuepress-ssr-loader to handle SSR dependencies
config.module
.rule('vue')
.test(/\.vue$/)
.use('vuepress-loader')
.use('vuepress-ssr-loader')
.before('vue-loader')
.loader(require.resolve('#vuepress-loader'))
.loader(require.resolve('#vuepress-ssr-loader'))
.end()

// vuepress client plugin, handle client assets info for ssr
Expand Down
9 changes: 6 additions & 3 deletions packages/bundler-webpack/src/build/createServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ export const createServerConfig = async (
// do not need to minimize server bundle
config.optimization.minimize(false)

// use internal vuepress-loader to handle SSR dependencies
// use internal vuepress-ssr-loader to handle SSR dependencies
config.module
.rule('vue')
.test(/\.vue$/)
.use('vuepress-loader')
.use('vuepress-ssr-loader')
.before('vue-loader')
.loader(require.resolve('#vuepress-loader'))
.loader(require.resolve('#vuepress-ssr-loader'))
.options({
app,
})
.end()

return config
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-webpack/src/build/renderPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { FileMeta, ModuleFilesMetaMap } from './types.js'

interface PageRenderContext extends SSRContext, VuepressSSRContext {
/**
* Injected by vuepress-loader
* Injected by vuepress-ssr-loader
*
* Store the module request of components that used by current page
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { LoaderDefinitionFunction } from 'webpack'

/**
* A webpack loader to handle SSR dependencies
*
Expand All @@ -8,11 +10,8 @@
* to ensure that the module `request` in client and
* server bundle are the same
*/
module.exports = function vuepressLoader(source: string): string {
// get `request` from loader context
const { request } = this as any

if (!request.endsWith('.vue')) return source
const vuepressSsrLoader: LoaderDefinitionFunction = function (source) {
if (!this.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
Expand All @@ -23,9 +22,11 @@ module.exports = function vuepressLoader(source: string): string {
import { ssrContextKey } from 'vue'
const ssrRender = (...args) => {
const ssrContext = args[2].appContext.provides[ssrContextKey]
ssrContext._registeredComponents.add(${JSON.stringify(request)})
ssrContext._registeredComponents.add(${JSON.stringify(this.request)})
return _ssrRender(...args)
}
`,
)
}

module.exports = vuepressSsrLoader
2 changes: 1 addition & 1 deletion packages/bundler-webpack/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig([
{
...shared,
entry: {
'vuepress-loader': './src/build/ssr/vuepressLoader.cts',
'vuepress-ssr-loader': './src/build/ssr/vuepressSsrLoader.cts',
},
format: ['cjs'],
},
Expand Down

0 comments on commit 240e7fe

Please sign in to comment.