Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): simplify type exports #10243

Merged
merged 11 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: patch dep-types only
  • Loading branch information
bluwy committed Sep 25, 2022
commit c7320f491a21683866a1e519783549558214fff1
4 changes: 2 additions & 2 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@
"strip-literal": "^0.4.2",
"tsconfck": "^2.0.1",
"tslib": "^2.4.0",
"dep-types": "link:./src/dep-types",
"types": "link:./src/types",
"dep-types": "link:./src/types",
"types": "link:./types",
"ufo": "^0.8.5",
"ws": "^8.9.0"
},
Expand Down
41 changes: 16 additions & 25 deletions packages/vite/scripts/patchTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ import { dirname, relative, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import type { ParseResult } from '@babel/parser'
import { parse } from '@babel/parser'
import type { File, StringLiteral } from '@babel/types'
import type { File } from '@babel/types'
import colors from 'picocolors'
import MagicString from 'magic-string'

const dir = dirname(fileURLToPath(import.meta.url))
const tempDir = resolve(dir, '../temp/node')
const typesDir = resolve(dir, '../src/types')
const depTypesDir = resolve(dir, '../src/dep-types')
const depTypesDir = resolve(dir, '../src/types')

// walk through the temp dts dir, find all import/export of types/*, deps-types/*
// and rewrite them into relative imports - so that api-extractor actually
// includes them in the rolled-up final d.ts file.
walkDir(tempDir)
console.log(colors.green(colors.bold(`patched types/*, deps-types/* imports`)))
console.log(colors.green(colors.bold(`patched deps-types/* imports`)))

function slash(p: string): string {
return p.replace(/\\/g, '/')
Expand Down Expand Up @@ -55,29 +54,21 @@ function rewriteFile(file: string): void {
statement.type === 'ExportAllDeclaration'
) {
const source = statement.source
if (source?.value.startsWith('types/')) {
rewriteSource(str, source, file, typesDir, 'types')
} else if (source?.value.startsWith('dep-types/')) {
rewriteSource(str, source, file, depTypesDir, 'dep-types')
if (source?.value.startsWith('dep-types/')) {
const absoluteTypePath = resolve(
depTypesDir,
source.value.slice('dep-types/'.length)
)
const relativeTypePath = slash(
relative(dirname(file), absoluteTypePath)
)
str.overwrite(
source.start!,
source.end!,
JSON.stringify(relativeTypePath)
)
}
}
}
writeFileSync(file, str.toString())
}

function rewriteSource(
str: MagicString,
source: StringLiteral,
rewritingFile: string,
typesDir: string,
typesDirName: string
) {
const absoluteTypePath = resolve(
typesDir,
source.value.slice(typesDirName.length + 1)
)
const relativeTypePath = slash(
relative(dirname(rewritingFile), absoluteTypePath)
)
str.overwrite(source.start!, source.end!, JSON.stringify(relativeTypePath))
}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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