Skip to content

Commit

Permalink
build: use rollup-plugin-dts for dts build
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 3, 2023
1 parent 6213b73 commit e145fe3
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -8,3 +8,5 @@ TODOs.md
*.log
.idea
.eslintcache
dts-build/packages
*.tsbuildinfo
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"dev": "node scripts/dev.mjs",
"build": "node scripts/build.mjs",
"build-dts": "tsc -p tsconfig.build.json && rollup -c rollup.dts.config.mjs",
"size": "run-s size-global size-baseline",
"size-global": "node scripts/build.mjs vue runtime-dom -f global -p",
"size-baseline": "node scripts/build.mjs runtime-dom runtime-core reactivity shared -f esm-bundler && cd packages/size-check && vite build && node brotli",
Expand All @@ -15,8 +16,8 @@
"test": "vitest",
"test-unit": "vitest -c vitest.unit.config.ts",
"test-e2e": "node scripts/build.mjs vue -f global -d && vitest -c vitest.e2e.config.ts",
"test-dts": "node scripts/build.mjs shared reactivity runtime-core runtime-dom -dt -f esm-bundler && npm run test-dts-only",
"test-dts-only": "tsc -p ./test-dts/tsconfig.json && tsc -p ./test-dts/tsconfig.build.json",
"test-dts": "run-s build-dts test-dts-only",
"test-dts-only": "tsc -p ./test-dts/tsconfig.build.json",
"test-coverage": "vitest -c vitest.unit.config.ts --coverage",
"release": "node scripts/release.mjs",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
Expand Down Expand Up @@ -57,7 +58,7 @@
"@babel/parser": "^7.20.15",
"@babel/types": "^7.20.7",
"@esbuild-plugins/node-modules-polyfill": "^0.1.4",
"@microsoft/api-extractor": "~7.20.0",
"@microsoft/api-extractor": "~7.34.2",
"@rollup/plugin-alias": "^4.0.3",
"@rollup/plugin-commonjs": "^23.0.2",
"@rollup/plugin-json": "^5.0.1",
Expand Down Expand Up @@ -92,6 +93,7 @@
"pug": "^3.0.1",
"puppeteer": "^19.2.2",
"rollup": "~3.10.0",
"rollup-plugin-dts": "^5.1.1",
"rollup-plugin-esbuild": "^5.0.0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0",
Expand Down
98 changes: 57 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions rollup.dts.config.mjs
@@ -0,0 +1,53 @@
// @ts-check
import { existsSync, readdirSync, readFileSync } from 'fs'
import dts from 'rollup-plugin-dts'

if (!existsSync('temp/packages')) {
console.warn(
'no temp dts files found. run `tsc -p tsconfig.build.json` first.'
)
process.exit(1)
}

export default readdirSync('temp/packages').map(pkg => {
return {
input: `./temp/packages/${pkg}/src/index.d.ts`,
output: {
file: `packages/${pkg}/dist/${pkg}.d.ts`,
format: 'es'
},
plugins: [dts(), patchTypes(pkg)],
onwarn(warning, warn) {
// during dts rollup, everything is externalized by default
if (
warning.code === 'UNRESOLVED_IMPORT' &&
!warning.exporter.startsWith('.')
) {
return
}
warn(warning)
}
}
})

/**
* @returns {import('rollup').Plugin}
*/
function patchTypes(pkg) {
return {
name: 'patch-types',
renderChunk(code) {
// 1. TODO remove entries marked with @private
// 2. append pkg specific types
const additionalTypeDir = `packages/${pkg}/types`
if (existsSync(additionalTypeDir)) {
code +=
'\n' +
readdirSync(additionalTypeDir)
.map(file => readFileSync(`${additionalTypeDir}/${file}`, 'utf-8'))
.join('\n')
}
return code
}
}
}
21 changes: 18 additions & 3 deletions scripts/aliases.mjs
@@ -1,11 +1,14 @@
// @ts-check
// these aliases are shared between vitest and rollup
import { readdirSync } from 'node:fs'
import { readdirSync, statSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

const resolveEntryForPkg = p =>
path.resolve(fileURLToPath(import.meta.url), `../../packages/${p}/src/index.ts`)
path.resolve(
fileURLToPath(import.meta.url),
`../../packages/${p}/src/index.ts`
)

const dirs = readdirSync(new URL('../packages', import.meta.url))

Expand All @@ -15,9 +18,21 @@ const entries = {
'vue/server-renderer': resolveEntryForPkg('server-renderer'),
'@vue/compat': resolveEntryForPkg('vue-compat')
}

const nonSrcPackages = [
'sfc-playground',
'size-check',
'template-explorer'
]

for (const dir of dirs) {
const key = `@vue/${dir}`
if (dir !== 'vue' && !(key in entries)) {
if (
dir !== 'vue' &&
!nonSrcPackages.includes(dir) &&
!(key in entries) &&
statSync(new URL(`../packages/${dir}`, import.meta.url)).isDirectory()
) {
entries[key] = resolveEntryForPkg(dir)
}
}
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.build.json
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true
},
"exclude": [
"packages/*/__tests__",
"packages/runtime-test",
"packages/template-explorer",
"packages/sfc-playground",
"packages/size-check",
"test-dts"
]
}
2 changes: 1 addition & 1 deletion tsconfig.json
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist",
"outDir": "temp",
"sourceMap": false,
"target": "es2016",
"newLine": "LF",
Expand Down

0 comments on commit e145fe3

Please sign in to comment.