build: improve tsdown bundle config

This commit is contained in:
pengzhanbo 2026-04-02 20:59:23 +08:00
parent cbc5c55891
commit 184d1aee76
13 changed files with 54 additions and 77 deletions

View File

@ -27,7 +27,7 @@
"templates"
],
"scripts": {
"build": "tsdown"
"build": "tsdown --config-loader unrun"
},
"dependencies": {
"@clack/prompts": "catalog:prod",

View File

@ -30,7 +30,7 @@
"build": "pnpm run tsdown && pnpm run copy",
"clean": "rimraf --glob ./lib",
"copy": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png,woff2}\" lib",
"tsdown": "tsdown"
"tsdown": "tsdown --config-loader unrun"
},
"peerDependencies": {
"vuepress": "catalog:vuepress"

View File

@ -1,7 +1,5 @@
import { defineConfig } from 'tsdown'
import { argv } from '../../scripts/tsdown-args.mjs'
/** @import {Options} from 'tsdown' */
import { defineConfig, type UserConfig } from 'tsdown'
import { argv } from '../../scripts/tsdown-args'
const clientExternal = [
/.*\.vue$/,
@ -9,15 +7,13 @@ const clientExternal = [
]
export default defineConfig(() => {
/** @type {Options} */
const DEFAULT_OPTIONS = {
const DEFAULT_OPTIONS: UserConfig = {
dts: true,
sourcemap: false,
format: 'esm',
fixedExtension: false,
}
/** @type {Options[]} */
const options = []
const options: UserConfig[] = []
if (argv.node) {
options.push({
@ -36,7 +32,7 @@ export default defineConfig(() => {
entry: ['./src/client/config.ts'],
outDir: './lib/client',
dts: false,
external: clientExternal,
deps: { neverBundle: clientExternal },
},
])
}

View File

@ -36,8 +36,8 @@
"clean": "rimraf --glob ./lib",
"copy": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png}\" lib",
"copy:watch": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png}\" lib -w",
"tsdown": "tsdown",
"tsdown:watch": "tsdown --watch -- -c"
"tsdown": "tsdown --config-loader unrun",
"tsdown:watch": "tsdown --config-loader unrun --watch -- -c"
},
"peerDependencies": {
"artplayer": "catalog:peer",

View File

@ -1,7 +1,5 @@
import { defineConfig } from 'tsdown'
import { argv } from '../../scripts/tsdown-args.mjs'
/** @import {Options} from 'tsdown' */
import { defineConfig, type UserConfig } from 'tsdown'
import { argv } from '../../scripts/tsdown-args'
const config = [
{ dir: 'composables', files: ['codeRepl.ts', 'pdf.ts', 'rustRepl.ts', 'size.ts', 'audio.ts', 'demo.ts', 'mark.ts', 'decrypt.ts'] },
@ -18,8 +16,7 @@ const clientExternal = [
]
export default defineConfig((cli) => {
/** @type {Options} */
const DEFAULT_OPTIONS = {
const DEFAULT_OPTIONS: UserConfig = {
dts: true,
sourcemap: false,
format: 'esm',
@ -27,8 +24,7 @@ export default defineConfig((cli) => {
fixedExtension: false,
}
/** @type {Options[]} */
const options = []
const options: UserConfig[] = []
// shared
options.push({
@ -43,7 +39,7 @@ export default defineConfig((cli) => {
entry: ['./src/node/index.ts'],
outDir: './lib/node',
target: 'node20.19.0',
external: ['markdown-it', /^@?vuepress/],
deps: { neverBundle: ['markdown-it', /^@?vuepress/] },
})
}
@ -52,7 +48,7 @@ export default defineConfig((cli) => {
...DEFAULT_OPTIONS,
entry: files.map(file => `./src/client/${dir}/${file}`),
outDir: `./lib/client/${dir}`,
external: clientExternal,
deps: { neverBundle: clientExternal },
})))
}
return options

View File

@ -34,7 +34,7 @@
"build": "pnpm run tsdown && pnpm run copy",
"clean": "rimraf --glob ./lib",
"copy": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png}\" lib",
"tsdown": "tsdown"
"tsdown": "tsdown --config-loader unrun"
},
"peerDependencies": {
"vuepress": "catalog:vuepress"

View File

@ -1,7 +1,5 @@
import { defineConfig } from 'tsdown'
import { argv } from '../../scripts/tsdown-args.mjs'
/** @import {Options} from 'tsdown' */
import { defineConfig, type UserConfig } from 'tsdown'
import { argv } from '../../scripts/tsdown-args'
const sharedExternal = [
/.*\/shared\/index\.js$/,
@ -15,16 +13,14 @@ const clientExternal = [
]
export default defineConfig(() => {
/** @type {Options} */
const DEFAULT_OPTIONS = {
const DEFAULT_OPTIONS: UserConfig = {
dts: true,
sourcemap: false,
format: 'esm',
fixedExtension: false,
}
/** @type {Options[]} */
const options = []
const options: UserConfig[] = []
// shared
options.push({
@ -38,7 +34,7 @@ export default defineConfig(() => {
...DEFAULT_OPTIONS,
entry: ['./src/node/index.ts'],
outDir: './lib/node',
external: sharedExternal,
deps: { neverBundle: sharedExternal },
target: 'node20.19.0',
})
}
@ -50,21 +46,21 @@ export default defineConfig(() => {
...DEFAULT_OPTIONS,
entry: ['./src/client/utils/index.ts'],
outDir: './lib/client/utils',
external: clientExternal,
deps: { neverBundle: clientExternal },
},
// client/composables/index.js
{
...DEFAULT_OPTIONS,
entry: ['./src/client/composables/index.ts'],
outDir: './lib/client/composables',
external: clientExternal,
deps: { neverBundle: clientExternal },
},
// client/config.js
{
...DEFAULT_OPTIONS,
entry: ['./src/client/config.ts'],
outDir: './lib/client',
external: clientExternal,
deps: { neverBundle: clientExternal },
dts: false,
},
// client/index.js
@ -72,10 +68,10 @@ export default defineConfig(() => {
...DEFAULT_OPTIONS,
entry: ['./src/client/index.ts'],
outDir: './lib/client',
external: [
deps: { neverBundle: [
...clientExternal,
'./composables/index.js',
],
] },
},
])
}

View File

@ -33,9 +33,7 @@ async function npmMirrorSync() {
resolve()
})
req.on('error', (error) => {
reject(error)
})
req.on('error', reject)
req.end()
})

View File

@ -1,16 +1,10 @@
import process from 'node:process'
import minimist from 'minimist'
// interface ArgvOptions {
// client: boolean
// node: boolean
// }
/**
* @typedef {object} ArgvOptions
* @property {boolean} client -
* @property {boolean} node - node
*/
interface ArgvOptions {
client: boolean
node: boolean
}
const rawArgv = process.argv.slice(2)
const tsupArgv = rawArgv.includes('--') ? rawArgv.slice(rawArgv.indexOf('--') + 1) : []
@ -30,8 +24,7 @@ const parsed = tsupArgv.length
all: true,
}
/** @type {ArgvOptions} */
export const argv = {
export const argv: ArgvOptions = {
client: parsed.client || parsed.all,
node: parsed.node || parsed.all,
}

View File

@ -62,8 +62,8 @@
"clean": "rimraf --glob ./lib",
"copy": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png,woff2}\" lib",
"copy:watch": "cpx \"src/**/*.{d.ts,vue,css,scss,jpg,png,woff2}\" lib -w",
"tsdown": "tsdown",
"tsdown:watch": "tsdown --watch -- -c"
"tsdown": "tsdown --config-loader unrun",
"tsdown:watch": "tsdown --config-loader unrun --watch -- -c"
},
"peerDependencies": {
"@iconify/json": ">=2",

View File

@ -1,8 +1,8 @@
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import { defineConfig } from 'tsdown'
import { argv } from '../scripts/tsdown-args.mjs'
import { defineConfig, type UserConfig } from 'tsdown'
import { argv } from '../scripts/tsdown-args'
/** @import {Options} from 'tsdown' */
@ -24,25 +24,22 @@ const featuresComposables = fs.readdirSync(
)
export default defineConfig((cli) => {
/** @type {Options} */
const DEFAULT_OPTIONS = {
const DEFAULT_OPTIONS: UserConfig = {
dts: true,
sourcemap: false,
watch: cli.watch,
format: 'esm',
silent: !!cli.watch,
clean: !cli.watch,
fixedExtension: false,
}
/** @type {Options[]} */
const options = []
const options: UserConfig[] = []
// shared
options.push({
...DEFAULT_OPTIONS,
entry: ['./src/shared/index.ts'],
outDir: './lib/shared',
external: ['sax'],
deps: { neverBundle: ['sax'] },
})
if (argv.node) {
@ -50,20 +47,20 @@ export default defineConfig((cli) => {
...DEFAULT_OPTIONS,
entry: ['./src/node/index.ts'],
outDir: './lib/node',
external: [...sharedExternal, '@pinyin-pro/data/complete'],
deps: { neverBundle: [...sharedExternal, '@pinyin-pro/data/complete'] },
target: 'node20.19.0',
watch: false,
})
}
if (argv.client) {
options.push(...[
options.push(
// client/utils/index.js
{
...DEFAULT_OPTIONS,
entry: ['./src/client/utils/index.ts'],
outDir: './lib/client/utils',
platform: 'browser',
external: clientExternal,
deps: { neverBundle: clientExternal },
},
// client/composables/index.js
{
@ -71,10 +68,10 @@ export default defineConfig((cli) => {
entry: ['./src/client/composables/index.ts'],
outDir: './lib/client/composables',
platform: 'browser',
external: [
deps: { neverBundle: [
...clientExternal,
'../utils/index.js',
],
] },
},
// client/config.js
{
@ -83,11 +80,11 @@ export default defineConfig((cli) => {
outDir: './lib/client',
dts: false,
platform: 'browser',
external: [
deps: { neverBundle: [
...clientExternal,
'./composables/index.js',
'./utils/index.js',
],
] },
},
// client/index.js
{
@ -95,26 +92,26 @@ export default defineConfig((cli) => {
entry: ['./src/client/index.ts'],
outDir: './lib/client',
platform: 'browser',
external: [
deps: { neverBundle: [
...clientExternal,
'./composables/index.js',
'./utils/index.js',
'./config.js',
],
] },
},
...featuresComposables.map(file => ({
...DEFAULT_OPTIONS,
entry: [`./src/client/features/composables/${file}`],
outDir: `./lib/client/features/composables/`,
platform: 'browser',
external: [
deps: { neverBundle: [
...clientExternal,
'../../composables/index.js',
'../../utils/index.js',
...featuresComposables.map(file => `./${file.replace('.ts', '.js')}`),
],
})),
])
] },
} as UserConfig)),
)
}
return options
})

View File

@ -14,7 +14,8 @@
"theme/**/*",
"docs/.vuepress/**/*",
"cli/**/*",
"scripts/**/*"
"scripts/**/*",
"**/tsdown.config.ts"
],
"exclude": [
"**/node_modules/**",