mirror of https://github.com/vuejs/vitepress
parent
b28667b6b0
commit
266c754de4
File diff suppressed because it is too large
Load Diff
@ -1,98 +0,0 @@
|
||||
import alias from '@rollup/plugin-alias'
|
||||
import commonjs from '@rollup/plugin-commonjs'
|
||||
import json from '@rollup/plugin-json'
|
||||
import { nodeResolve } from '@rollup/plugin-node-resolve'
|
||||
import replace from '@rollup/plugin-replace'
|
||||
import * as fs from 'node:fs/promises'
|
||||
import { builtinModules, createRequire } from 'node:module'
|
||||
import { type RollupOptions, defineConfig } from 'rollup'
|
||||
import dts from 'rollup-plugin-dts'
|
||||
import esbuild from 'rollup-plugin-esbuild'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
const pkg = require('./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: 'node20' }),
|
||||
json()
|
||||
]
|
||||
|
||||
const esmBuild: RollupOptions = {
|
||||
input: ['src/node/index.ts', 'src/node/cli.ts'],
|
||||
output: {
|
||||
format: 'esm',
|
||||
entryFileNames: `[name].js`,
|
||||
chunkFileNames: 'chunk-[hash].js',
|
||||
dir: 'dist/node',
|
||||
sourcemap: DEV
|
||||
},
|
||||
external,
|
||||
plugins,
|
||||
onwarn(warning, warn) {
|
||||
if (warning.code !== 'EVAL') warn(warning)
|
||||
}
|
||||
}
|
||||
|
||||
const typesExternal = [
|
||||
...external,
|
||||
/\/vitepress\/(?!(dist|node_modules|vitepress)\/).*\.d\.ts$/,
|
||||
/^markdown-it(?:\/|$)/
|
||||
]
|
||||
|
||||
const dtsNode = dts({
|
||||
respectExternal: true,
|
||||
tsconfig: 'src/node/tsconfig.json',
|
||||
compilerOptions: { preserveSymlinks: false }
|
||||
})
|
||||
|
||||
const nodeTypes: RollupOptions = {
|
||||
input: 'src/node/index.ts',
|
||||
output: {
|
||||
format: 'esm',
|
||||
file: 'dist/node/index.d.ts'
|
||||
},
|
||||
external: typesExternal,
|
||||
plugins: [dtsNode]
|
||||
}
|
||||
|
||||
const clientTypes: RollupOptions = {
|
||||
input: 'dist/client-types/index.d.ts',
|
||||
output: {
|
||||
format: 'esm',
|
||||
file: 'dist/client/index.d.ts'
|
||||
},
|
||||
external: typesExternal,
|
||||
plugins: [
|
||||
dts({ respectExternal: true }),
|
||||
{
|
||||
name: 'cleanup',
|
||||
async closeBundle() {
|
||||
if (PROD) {
|
||||
await fs.rm('dist/client-types', { recursive: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export default defineConfig([esmBuild, nodeTypes, clientTypes])
|
||||
@ -1,11 +0,0 @@
|
||||
import { copy } from 'fs-extra'
|
||||
import { globSync } from 'tinyglobby'
|
||||
|
||||
function toDest(file) {
|
||||
return file.replace(/^src\//, 'dist/')
|
||||
}
|
||||
|
||||
globSync(['src/client/**']).forEach((file) => {
|
||||
if (/(\.ts|tsconfig\.json)$/.test(file)) return
|
||||
copy(file, toDest(file))
|
||||
})
|
||||
@ -1,9 +0,0 @@
|
||||
import { copy } from 'fs-extra'
|
||||
import { globSync } from 'tinyglobby'
|
||||
|
||||
globSync(['src/shared/**/*.ts']).forEach(async (file) => {
|
||||
await Promise.all([
|
||||
copy(file, file.replace(/^src\/shared\//, 'src/node/')),
|
||||
copy(file, file.replace(/^src\/shared\//, 'src/client/'))
|
||||
])
|
||||
})
|
||||
@ -1,36 +0,0 @@
|
||||
import { watch } from 'chokidar'
|
||||
import { copy, remove } from 'fs-extra'
|
||||
import { normalizePath } from 'vite'
|
||||
|
||||
function toClientAndNode(method, file) {
|
||||
file = normalizePath(file)
|
||||
if (method === 'copy') {
|
||||
copy(file, file.replace(/^src\/shared\//, 'src/node/'))
|
||||
copy(file, file.replace(/^src\/shared\//, 'src/client/'))
|
||||
} else if (method === 'remove') {
|
||||
remove(file.replace(/^src\/shared\//, 'src/node/'))
|
||||
remove(file.replace(/^src\/shared\//, 'src/client/'))
|
||||
}
|
||||
}
|
||||
|
||||
function toDist(file) {
|
||||
return normalizePath(file).replace(/^src\//, 'dist/')
|
||||
}
|
||||
|
||||
// copy shared files to the client and node directory whenever they change.
|
||||
watch('src/shared', {
|
||||
ignored: (path, stats) => stats?.isFile() && !path.endsWith('.ts')
|
||||
})
|
||||
.on('change', (file) => toClientAndNode('copy', file))
|
||||
.on('add', (file) => toClientAndNode('copy', file))
|
||||
.on('unlink', (file) => toClientAndNode('remove', file))
|
||||
|
||||
// copy non ts files, such as an html or css, to the dist directory whenever
|
||||
// they change.
|
||||
watch('src/client', {
|
||||
ignored: (path, stats) =>
|
||||
stats?.isFile() && (path.endsWith('.ts') || path.endsWith('tsconfig.json'))
|
||||
})
|
||||
.on('change', (file) => copy(file, toDist(file)))
|
||||
.on('add', (file) => copy(file, toDist(file)))
|
||||
.on('unlink', (file) => remove(toDist(file)))
|
||||
@ -1,10 +1,8 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"outDir": "../../dist/node",
|
||||
"types": ["node"],
|
||||
"sourceMap": true
|
||||
"types": ["node"]
|
||||
},
|
||||
"include": ["."]
|
||||
}
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"lib": ["esnext", "dom", "dom.iterable"]
|
||||
"types": []
|
||||
},
|
||||
"include": ["."]
|
||||
}
|
||||
|
||||
@ -0,0 +1,138 @@
|
||||
import { watch, type ChokidarOptions } from 'chokidar'
|
||||
import fs from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { glob } from 'tinyglobby'
|
||||
import { defineConfig, type UserConfig } from 'tsdown'
|
||||
|
||||
const DEV = process.argv.includes('--watch') || process.argv.includes('-w')
|
||||
|
||||
const clientBase = path.resolve(import.meta.dirname, 'src/client')
|
||||
const nodeBase = path.resolve(import.meta.dirname, 'src/node')
|
||||
const sharedBase = path.resolve(import.meta.dirname, 'src/shared')
|
||||
|
||||
const distBase = path.resolve(import.meta.dirname, 'dist')
|
||||
const distClientBase = path.resolve(import.meta.dirname, 'dist/client')
|
||||
|
||||
const typesExternal = [
|
||||
/\/vitepress\/(?!(?:dist|node_modules|vitepress|src)\/).*\.d\.ts$/,
|
||||
/^markdown-it(?:$|\/)/
|
||||
]
|
||||
|
||||
await copyAndWatch()
|
||||
|
||||
const common = {
|
||||
checks: { eval: false, pluginTimings: false },
|
||||
clean: false,
|
||||
dts: { resolver: 'tsc' },
|
||||
failOnWarn: !DEV,
|
||||
fixedExtension: false,
|
||||
inlineOnly: false,
|
||||
logLevel: 'warn',
|
||||
minify: { codegen: false, compress: true, mangle: false },
|
||||
nodeProtocol: true,
|
||||
sourcemap: DEV,
|
||||
external: (id, parentId) =>
|
||||
(parentId?.endsWith('.d.ts') && typesExternal.some((re) => re.test(id))) ||
|
||||
undefined
|
||||
} satisfies UserConfig
|
||||
|
||||
export default defineConfig([
|
||||
{
|
||||
...common,
|
||||
name: 'node',
|
||||
entry: ['src/node/index.ts', 'src/node/cli.ts'],
|
||||
outDir: 'dist/node',
|
||||
platform: 'node',
|
||||
target: 'node20',
|
||||
tsconfig: 'src/node/tsconfig.json'
|
||||
},
|
||||
{
|
||||
...common,
|
||||
name: 'client',
|
||||
entry: ['src/client/**/*.ts'],
|
||||
outDir: 'dist/client',
|
||||
platform: 'neutral',
|
||||
target: ['node20', 'chrome107', 'firefox104', 'safari16'],
|
||||
tsconfig: 'src/client/tsconfig.json',
|
||||
external: (id, parentId) =>
|
||||
id === '@localSearchIndex' ||
|
||||
id === '@siteData' ||
|
||||
id === '@theme/index' ||
|
||||
shouldCopyFromClient(id) ||
|
||||
common.external(id, parentId)
|
||||
}
|
||||
])
|
||||
|
||||
// #region Watchers
|
||||
|
||||
async function copyAndWatch() {
|
||||
await fs.rm(distBase, { recursive: true, force: true, maxRetries: 10 })
|
||||
await Promise.all([
|
||||
...(await glob('**/*', { cwd: sharedBase })).map(cpShared),
|
||||
...(await glob('**/*', { cwd: clientBase })).map(cpClient)
|
||||
])
|
||||
|
||||
if (!DEV) return
|
||||
|
||||
const watcherOpts: ChokidarOptions = {
|
||||
ignoreInitial: true,
|
||||
awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 50 }
|
||||
}
|
||||
|
||||
const sharedWatcher = watch('.', {
|
||||
cwd: sharedBase,
|
||||
ignored: (val, stats) => !!stats?.isFile() && !shouldCopyFromShared(val),
|
||||
...watcherOpts
|
||||
})
|
||||
.on('add', cpShared)
|
||||
.on('change', cpShared)
|
||||
|
||||
const assetWatcher = watch('.', {
|
||||
cwd: clientBase,
|
||||
ignored: (val, stats) => !!stats?.isFile() && !shouldCopyFromClient(val),
|
||||
...watcherOpts
|
||||
})
|
||||
.on('add', cpClient)
|
||||
.on('change', cpClient)
|
||||
.on('unlink', rmClient)
|
||||
|
||||
const closeAll = async () => {
|
||||
await Promise.allSettled([sharedWatcher.close(), assetWatcher.close()])
|
||||
}
|
||||
|
||||
process.once('SIGINT', () => closeAll().finally(() => process.exit(0)))
|
||||
process.once('SIGTERM', () => closeAll().finally(() => process.exit(0)))
|
||||
process.once('exit', () => closeAll())
|
||||
}
|
||||
|
||||
async function cpShared(rel: string) {
|
||||
if (!shouldCopyFromShared(rel)) return
|
||||
const src = path.join(sharedBase, rel)
|
||||
const destClient = path.join(clientBase, rel)
|
||||
const destNode = path.join(nodeBase, rel)
|
||||
await fs.copyFile(src, destClient)
|
||||
await fs.copyFile(src, destNode)
|
||||
}
|
||||
|
||||
async function cpClient(rel: string) {
|
||||
if (!shouldCopyFromClient(rel)) return
|
||||
const src = path.join(clientBase, rel)
|
||||
const dest = path.join(distClientBase, rel)
|
||||
await fs.mkdir(path.dirname(dest), { recursive: true })
|
||||
await fs.copyFile(src, dest)
|
||||
}
|
||||
|
||||
async function rmClient(rel: string) {
|
||||
const dest = path.join(distClientBase, rel)
|
||||
await fs.rm(dest, { force: true })
|
||||
}
|
||||
|
||||
function shouldCopyFromShared(id: string) {
|
||||
return id.endsWith('.ts')
|
||||
}
|
||||
|
||||
function shouldCopyFromClient(id: string) {
|
||||
return id.endsWith('.css') || id.endsWith('.vue') || id.endsWith('.woff2')
|
||||
}
|
||||
|
||||
// #endregion
|
||||
Loading…
Reference in new issue