Skip to content

Commit

Permalink
refactor(bundler-webpack): split cjs loader entry
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Sep 10, 2024
1 parent 8a90ce9 commit 169ab1b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
31 changes: 1 addition & 30 deletions packages/bundler-webpack/src/loaders/vuepressSsrLoader.cts
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
import type { LoaderDefinitionFunction } from 'webpack'

/**
* A webpack loader to handle SSR dependencies
*
* This loader will only take effect in server bundle
* because we only replace `ssrRender` code
*
* But we still need to use this loader in client,
* to ensure that the module `request` in client and
* server bundle are the same
*/
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
// see https://github.com/vuejs/vue-loader/blob/1b1a195612f885a8dec3f371edf1cb8b35d341e4/src/index.ts#L167-L183
return source.replace(
/import { ssrRender } from (.*)\n/,
`import { ssrRender as _ssrRender } from $1
import { ssrContextKey } from 'vue'
const ssrRender = (...args) => {
const ssrContext = args[2].appContext.provides[ssrContextKey]
ssrContext._registeredComponents.add(${JSON.stringify(this.request)})
return _ssrRender(...args)
}
`,
)
}
const { vuepressSsrLoader } = require('./vuepressSsrLoader.js')

module.exports = vuepressSsrLoader
29 changes: 29 additions & 0 deletions packages/bundler-webpack/src/loaders/vuepressSsrLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { LoaderDefinitionFunction } from 'webpack'

/**
* A webpack loader to handle SSR dependencies
*
* This loader will only take effect in server bundle
* because we only replace `ssrRender` code
*
* But we still need to use this loader in client,
* to ensure that the module `request` in client and
* server bundle are the same
*/
export const vuepressSsrLoader: LoaderDefinitionFunction =
function vuepressSsrLoader(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(
/import { ssrRender } from (.*)\n/,
`import { ssrRender as _ssrRender } from $1
import { ssrContextKey } from 'vue'
const ssrRender = (...args) => {
const ssrContext = args[2].appContext.provides[ssrContextKey]
ssrContext._registeredComponents.add(${JSON.stringify(this.request)})
return _ssrRender(...args)
}
`,
)
}

0 comments on commit 169ab1b

Please sign in to comment.