diff --git a/packages/bundler-webpack/package.json b/packages/bundler-webpack/package.json index 02aa8ce2e2..28d32a8f12 100644 --- a/packages/bundler-webpack/package.json +++ b/packages/bundler-webpack/package.json @@ -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", diff --git a/packages/bundler-webpack/src/build/createClientConfig.ts b/packages/bundler-webpack/src/build/createClientConfig.ts index e61f048c77..7530be9e07 100644 --- a/packages/bundler-webpack/src/build/createClientConfig.ts +++ b/packages/bundler-webpack/src/build/createClientConfig.ts @@ -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 diff --git a/packages/bundler-webpack/src/build/createServerConfig.ts b/packages/bundler-webpack/src/build/createServerConfig.ts index 0db803831e..d21b75b034 100644 --- a/packages/bundler-webpack/src/build/createServerConfig.ts +++ b/packages/bundler-webpack/src/build/createServerConfig.ts @@ -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 diff --git a/packages/bundler-webpack/src/build/renderPage.ts b/packages/bundler-webpack/src/build/renderPage.ts index 009841dffb..6d3c888dd9 100644 --- a/packages/bundler-webpack/src/build/renderPage.ts +++ b/packages/bundler-webpack/src/build/renderPage.ts @@ -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 */ diff --git a/packages/bundler-webpack/src/build/ssr/vuepressLoader.cts b/packages/bundler-webpack/src/build/ssr/vuepressSsrLoader.cts similarity index 74% rename from packages/bundler-webpack/src/build/ssr/vuepressLoader.cts rename to packages/bundler-webpack/src/build/ssr/vuepressSsrLoader.cts index 0ee48004a7..4217e1cea7 100644 --- a/packages/bundler-webpack/src/build/ssr/vuepressLoader.cts +++ b/packages/bundler-webpack/src/build/ssr/vuepressSsrLoader.cts @@ -1,3 +1,5 @@ +import type { LoaderDefinitionFunction } from 'webpack' + /** * A webpack loader to handle SSR dependencies * @@ -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 @@ -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 diff --git a/packages/bundler-webpack/tsup.config.ts b/packages/bundler-webpack/tsup.config.ts index 5213017426..fbd20132f9 100644 --- a/packages/bundler-webpack/tsup.config.ts +++ b/packages/bundler-webpack/tsup.config.ts @@ -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'], },