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" "templates"
], ],
"scripts": { "scripts": {
"build": "tsdown" "build": "tsdown --config-loader unrun"
}, },
"dependencies": { "dependencies": {
"@clack/prompts": "catalog:prod", "@clack/prompts": "catalog:prod",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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