-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(types): simplify type exports (#10243)
Co-authored-by: 翠 / green <green@sapphi.red>
- Loading branch information
1 parent
931d69b
commit 291174d
Showing
40 changed files
with
386 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
dist | ||
playground-temp | ||
temp | ||
packages/vite/client/types.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
*.local | ||
*.log | ||
/.vscode/ | ||
/packages/vite/client/types.d.ts | ||
/packages/vite/LICENSE | ||
dist | ||
dist-ssr | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { dirname, resolve } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import colors from 'picocolors' | ||
import { rewriteImports } from './util' | ||
|
||
const dir = dirname(fileURLToPath(import.meta.url)) | ||
const nodeDts = resolve(dir, '../dist/node/index.d.ts') | ||
|
||
// rewrite `types/*` import to relative import | ||
rewriteImports(nodeDts, (importPath) => { | ||
if (importPath.startsWith('types/')) { | ||
return '../../' + importPath | ||
} | ||
}) | ||
|
||
console.log(colors.green(colors.bold(`patched types/* imports`))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { dirname, relative, resolve } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import colors from 'picocolors' | ||
import { rewriteImports, slash } from './util' | ||
|
||
const dir = dirname(fileURLToPath(import.meta.url)) | ||
const tempDir = resolve(dir, '../temp/node') | ||
const depTypesDir = resolve(dir, '../src/types') | ||
|
||
// walk through the temp dts dir, find all import/export of, deps-types/* | ||
// and rewrite them into relative imports - so that api-extractor actually | ||
// includes them in the rolled-up final d.ts file. | ||
rewriteImports(tempDir, (importPath, currentFile) => { | ||
if (importPath.startsWith('dep-types/')) { | ||
const absoluteTypePath = resolve( | ||
depTypesDir, | ||
importPath.slice('dep-types/'.length) | ||
) | ||
return slash(relative(dirname(currentFile), absoluteTypePath)) | ||
} | ||
}) | ||
|
||
console.log(colors.green(colors.bold(`patched deps-types/* imports`))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs' | ||
import { resolve } from 'node:path' | ||
import type { ParseResult } from '@babel/parser' | ||
import { parse } from '@babel/parser' | ||
import type { File } from '@babel/types' | ||
import colors from 'picocolors' | ||
import MagicString from 'magic-string' | ||
|
||
export function rewriteImports( | ||
fileOrDir: string, | ||
rewrite: (importPath: string, currentFile: string) => string | void | ||
): void { | ||
walkDir(fileOrDir, (file) => { | ||
rewriteFileImports(file, (importPath) => { | ||
return rewrite(importPath, file) | ||
}) | ||
}) | ||
} | ||
|
||
export function slash(p: string): string { | ||
return p.replace(/\\/g, '/') | ||
} | ||
|
||
function walkDir(dir: string, handleFile: (file: string) => void): void { | ||
if (statSync(dir).isDirectory()) { | ||
const files = readdirSync(dir) | ||
for (const file of files) { | ||
const resolved = resolve(dir, file) | ||
walkDir(resolved, handleFile) | ||
} | ||
} else { | ||
handleFile(dir) | ||
} | ||
} | ||
|
||
function rewriteFileImports( | ||
file: string, | ||
rewrite: (importPath: string) => string | void | ||
): void { | ||
const content = readFileSync(file, 'utf-8') | ||
const str = new MagicString(content) | ||
let ast: ParseResult<File> | ||
try { | ||
ast = parse(content, { | ||
sourceType: 'module', | ||
plugins: ['typescript', 'classProperties'] | ||
}) | ||
} catch (e) { | ||
console.log(colors.red(`failed to parse ${file}`)) | ||
throw e | ||
} | ||
for (const statement of ast.program.body) { | ||
if ( | ||
statement.type === 'ImportDeclaration' || | ||
statement.type === 'ExportNamedDeclaration' || | ||
statement.type === 'ExportAllDeclaration' | ||
) { | ||
const source = statement.source | ||
if (source?.value) { | ||
const newImportPath = rewrite(source.value) | ||
if (newImportPath) { | ||
str.overwrite( | ||
source.start!, | ||
source.end!, | ||
JSON.stringify(newImportPath) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
writeFileSync(file, str.toString()) | ||
} |
Oops, something went wrong.