fix(build): [ENG-12] exclude declaration sources from library artifacts

This commit is contained in:
Harvey Zhao committed 2026-09-13 13:37:00 +08:00
1 parent 38355ae4cf
commit 4a299eb2d4
50 files changed
+242 -2157

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
// eslint-disable-next-line test/no-import-node-test -- Exercise actual Vite library output on disk.
import test from 'node:test'
import { build } from 'vite'
import { getViteBuildConfig } from '../scripts/utils.js'
test('Library builds never copy declaration sources or unrelated public resources into dist', async () => {
const parent = fs.realpathSync('refactor/.cache')
const root = fs.mkdtempSync(path.join(parent, 'library-build-'))
try {
fs.mkdirSync(path.join(root, 'src'))
fs.mkdirSync(path.join(root, 'public'))
fs.writeFileSync(path.join(root, 'src/index.ts'), 'export default function answer() { return 42 }\n')
fs.writeFileSync(path.join(root, 'public/internal.ts'), 'export type PrivateImplementation = { secret: number }\n')
fs.writeFileSync(path.join(root, 'public/unrelated.txt'), 'must not ship\n')
for (const [format, fileName, target] of [['umd', 'index.js', 'es2020'], ['umd', 'index.legacy.js', 'es2015'], ['es', 'index.mjs', 'es2020']]) {
const config = getViteBuildConfig({ entry: path.join(root, 'src/index.ts'), outDir: path.join(root, 'dist'), name: 'Fixture', format, fileName, target })
await build({ root, ...config })
assert(!fs.existsSync(path.join(root, 'dist/internal.ts')), 'Private declaration source was copied')
assert(!fs.existsSync(path.join(root, 'dist/unrelated.txt')), 'Unrelated public asset was copied')
}
assert.deepEqual(fs.readdirSync(path.join(root, 'dist')).sort(), ['index.js', 'index.legacy.js', 'index.mjs'])
}
finally {
assert.equal(path.dirname(fs.realpathSync(root)), parent)
assert(path.basename(root).startsWith('library-build-'))
fs.rmSync(root, { recursive: true, force: true })
}
})
+3
View File
@@ -16,6 +16,9 @@ test('Package checks reject missing files, missing wildcard exports and internal
assert.throws(() => checkFiles(manifest, [...files, 'package/tsconfig.json']), /tsconfig/)
for (const source of ['src/internal.ts', 'public/artplayer.ts', 'node_modules/private/index.js'])
assert.throws(() => checkFiles(manifest, [...files, `package/${source}`]), /Source\/dependencies/)
for (const source of ['dist/internal.ts', 'dist/internal.cts', 'dist/internal.mts'])
assert.throws(() => checkFiles(manifest, [...files, `package/${source}`]), /Authored TypeScript/)
checkFiles(manifest, [...files, 'package/types/entry.d.cts', 'package/types/entry.d.mts'])
assert.throws(() => checkFiles(manifest, files, ['package/dist/old.js']), /Historical/)
})