-
Notifications
You must be signed in to change notification settings - Fork 922
/
Copy pathhandleModuleVue.ts
69 lines (62 loc) · 1.71 KB
/
handleModuleVue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { createRequire } from 'node:module'
import type { App } from '@vuepress/core'
import type { VueLoaderOptions } from 'vue-loader'
import { VueLoaderPlugin } from 'vue-loader'
import type Config from 'webpack-5-chain'
import type { VuepressMarkdownLoaderOptions } from '../loaders/vuepressMarkdownLoader'
import type { WebpackBundlerOptions } from '../types.js'
const require = createRequire(import.meta.url)
/**
* Set webpack module to handle vue files
*/
export const handleModuleVue = ({
app,
options,
config,
isBuild,
isServer,
}: {
app: App
options: WebpackBundlerOptions
config: Config
isBuild: boolean
isServer: boolean
}): void => {
const handleVue = ({
lang,
test,
}: {
lang: 'md' | 'vue'
test: RegExp
}): void => {
const rule = config.module.rule(lang).test(test)
// use internal vuepress-ssr-loader to handle SSR dependencies
if (isBuild) {
rule
.use('vuepress-ssr-loader')
.loader(require.resolve('#vuepress-ssr-loader'))
.end()
}
// use official vue-loader
rule
.use('vue-loader')
.loader(require.resolve('vue-loader'))
.options({
...options.vue,
isServerBuild: isServer,
} satisfies VueLoaderOptions)
.end()
// use internal vuepress-markdown-loader to handle markdown files
if (lang === 'md') {
rule
.use('vuepress-markdown-loader')
.loader(require.resolve('#vuepress-markdown-loader'))
.options({ app } satisfies VuepressMarkdownLoaderOptions)
.end()
}
}
handleVue({ lang: 'md', test: /\.md$/ })
handleVue({ lang: 'vue', test: /\.vue$/ })
// use vue-loader plugin
config.plugin('vue-loader').use(VueLoaderPlugin)
}