Skip to content

Commit 169ab1b

Browse files
committed
refactor(bundler-webpack): split cjs loader entry
1 parent 8a90ce9 commit 169ab1b

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed
Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,3 @@
1-
import type { LoaderDefinitionFunction } from 'webpack'
2-
3-
/**
4-
* A webpack loader to handle SSR dependencies
5-
*
6-
* This loader will only take effect in server bundle
7-
* because we only replace `ssrRender` code
8-
*
9-
* But we still need to use this loader in client,
10-
* to ensure that the module `request` in client and
11-
* server bundle are the same
12-
*/
13-
const vuepressSsrLoader: LoaderDefinitionFunction = function (source) {
14-
if (!this.request.endsWith('.vue')) return source
15-
16-
// add `request` to `ssrContext._registeredComponents` to handle SSR dependencies
17-
// notice that this could only handle those sfc that cannot use inline template
18-
// see https://github.com/vuejs/vue-loader/blob/1b1a195612f885a8dec3f371edf1cb8b35d341e4/src/index.ts#L167-L183
19-
return source.replace(
20-
/import { ssrRender } from (.*)\n/,
21-
`import { ssrRender as _ssrRender } from $1
22-
import { ssrContextKey } from 'vue'
23-
const ssrRender = (...args) => {
24-
const ssrContext = args[2].appContext.provides[ssrContextKey]
25-
ssrContext._registeredComponents.add(${JSON.stringify(this.request)})
26-
return _ssrRender(...args)
27-
}
28-
`,
29-
)
30-
}
1+
const { vuepressSsrLoader } = require('./vuepressSsrLoader.js')
312

323
module.exports = vuepressSsrLoader
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { LoaderDefinitionFunction } from 'webpack'
2+
3+
/**
4+
* A webpack loader to handle SSR dependencies
5+
*
6+
* This loader will only take effect in server bundle
7+
* because we only replace `ssrRender` code
8+
*
9+
* But we still need to use this loader in client,
10+
* to ensure that the module `request` in client and
11+
* server bundle are the same
12+
*/
13+
export const vuepressSsrLoader: LoaderDefinitionFunction =
14+
function vuepressSsrLoader(source) {
15+
// add `request` to `ssrContext._registeredComponents` to handle SSR dependencies
16+
// notice that this could only handle those sfc that cannot use inline template
17+
// see https://github.com/vuejs/vue-loader/blob/1b1a195612f885a8dec3f371edf1cb8b35d341e4/src/index.ts#L167-L183
18+
return source.replace(
19+
/import { ssrRender } from (.*)\n/,
20+
`import { ssrRender as _ssrRender } from $1
21+
import { ssrContextKey } from 'vue'
22+
const ssrRender = (...args) => {
23+
const ssrContext = args[2].appContext.provides[ssrContextKey]
24+
ssrContext._registeredComponents.add(${JSON.stringify(this.request)})
25+
return _ssrRender(...args)
26+
}
27+
`,
28+
)
29+
}

0 commit comments

Comments
 (0)