forked from vuejs/vitepress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
116 lines (103 loc) · 2.82 KB
/
rollup.config.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as fs from 'node:fs/promises'
import { builtinModules, createRequire } from 'node:module'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { type RollupOptions, defineConfig } from 'rollup'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import esbuild from 'rollup-plugin-esbuild'
import json from '@rollup/plugin-json'
import replace from '@rollup/plugin-replace'
import alias from '@rollup/plugin-alias'
import dts from 'rollup-plugin-dts'
import { globSync } from 'tinyglobby'
const ROOT = fileURLToPath(import.meta.url)
const r = (p: string) => resolve(ROOT, '..', p)
const require = createRequire(import.meta.url)
const pkg = require(r('package.json'))
const DEV = !!process.env.DEV
const PROD = !DEV
const external = [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
...builtinModules.flatMap((m) =>
m.includes('punycode') ? [] : [m, `node:${m}`]
)
]
const plugins = [
alias({ entries: { 'readable-stream': 'stream' } }),
replace({
// polyfill broken browser check from bundled deps
'navigator.userAgentData': 'undefined',
'navigator.userAgent': 'undefined',
preventAssignment: true
}),
commonjs(),
nodeResolve({ preferBuiltins: false }),
esbuild({ target: 'node18' }),
json()
]
const esmBuild: RollupOptions = {
input: [
r('src/node/index.ts'),
r('src/node/cli.ts'),
...globSync(r('src/node/worker_*.ts'))
],
output: {
format: 'esm',
entryFileNames: `[name].js`,
chunkFileNames: 'chunk-[hash].js',
dir: r('dist/node'),
sourcemap: DEV
},
external,
plugins,
onwarn(warning, warn) {
if (warning.code !== 'EVAL') warn(warning)
}
}
const typesExternal = [
...external,
/\/vitepress\/(?!(dist|node_modules)\/).*\.d\.ts$/,
'source-map-js',
'fast-glob'
]
const dtsNode = dts({
respectExternal: true,
tsconfig: r('src/node/tsconfig.json')
})
const originalResolveId = dtsNode.resolveId
dtsNode.resolveId = async function (source, importer) {
const res = await (originalResolveId as Function).call(this, source, importer)
if (res?.id) res.id = await fs.realpath(res.id)
return res
}
const nodeTypes: RollupOptions = {
input: r('src/node/index.ts'),
output: {
format: 'esm',
file: r('dist/node/index.d.ts')
},
external: typesExternal,
plugins: [dtsNode]
}
const clientTypes: RollupOptions = {
input: r('dist/client-types/index.d.ts'),
output: {
format: 'esm',
file: r('dist/client/index.d.ts')
},
external: typesExternal,
plugins: [
dts({ respectExternal: true }),
{
name: 'cleanup',
async closeBundle() {
if (PROD) {
await fs.rm(r('dist/client-types'), { recursive: true })
}
}
}
]
}
export default defineConfig([esmBuild, nodeTypes, clientTypes])