-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49931fa
commit 78b2401
Showing
16 changed files
with
505 additions
and
70 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 |
---|---|---|
|
@@ -4,4 +4,7 @@ compiled | |
doc_build | ||
|
||
# ignore all JS/TS files, use Biome | ||
**/*[.js,.ts,.jsx,.tsx] | ||
**/*.js | ||
**/*.ts | ||
**/*.jsx | ||
**/*.tsx |
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,6 @@ | ||
{ | ||
"name": "dts-bundle-test", | ||
"version": "1.0.0", | ||
"private": true, | ||
"type": "module" | ||
} |
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 { defineConfig } from '@rslib/core'; | ||
import { | ||
generateBundleCjsConfig, | ||
generateBundleEsmConfig, | ||
} from '../../../scripts/shared'; | ||
|
||
export default defineConfig({ | ||
lib: [ | ||
generateBundleEsmConfig(__dirname, { | ||
dts: { | ||
bundle: true, | ||
}, | ||
}), | ||
generateBundleCjsConfig(__dirname, { | ||
dts: false, | ||
}), | ||
], | ||
source: { | ||
entry: { | ||
main: './src/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './utils/numbers'; | ||
export * from './utils/strings'; | ||
export * from './sum'; |
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,5 @@ | ||
import { num1, num2, num3 } from './utils/numbers'; | ||
import { str1, str2, str3 } from './utils/strings'; | ||
|
||
export const numSum = num1 + num2 + num3; | ||
export const strSum = str1 + str2 + str3; |
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,3 @@ | ||
export const num1 = 1; | ||
export const num2 = 2; | ||
export const num3 = 3; |
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,3 @@ | ||
export const str1 = 'str1'; | ||
export const str2 = 'str2'; | ||
export const str3 = 'str3'; |
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,7 @@ | ||
{ | ||
"extends": "@rslib/tsconfig/base", | ||
"compilerOptions": { | ||
"baseUrl": "./" | ||
}, | ||
"include": ["src"] | ||
} |
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 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,43 @@ | ||
import { join } from 'node:path'; | ||
import { | ||
Extractor, | ||
ExtractorConfig, | ||
type ExtractorResult, | ||
} from '@microsoft/api-extractor'; | ||
import { logger } from '@rsbuild/core'; | ||
import { ensureTempDeclarationDir } from './utils'; | ||
|
||
export function bundleDts(outDir: string, tsconfigPath = 'tsconfig.json') { | ||
const cwd = process.cwd(); | ||
const internalConfig = { | ||
// TODO: use source.entry.main | ||
mainEntryPointFilePath: join(ensureTempDeclarationDir(), 'index.d.ts'), | ||
// TODO: use !externals | ||
// bundledPackages: [], | ||
dtsRollup: { | ||
enabled: true, | ||
untrimmedFilePath: join(cwd, outDir, 'index.d.ts'), | ||
}, | ||
compiler: { | ||
tsconfigFilePath: join(cwd, tsconfigPath), | ||
}, | ||
projectFolder: cwd, | ||
}; | ||
|
||
const extractorConfig = ExtractorConfig.prepare({ | ||
configObject: internalConfig, | ||
configObjectFullPath: undefined, | ||
packageJsonFullPath: join(cwd, 'package.json'), | ||
}); | ||
|
||
const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, { | ||
localBuild: true, | ||
showVerboseMessages: true, | ||
}); | ||
|
||
if (!extractorResult.succeeded) { | ||
throw new Error('API Extractor error'); | ||
} | ||
|
||
logger.info('API Extractor rollup succeeded\n'); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { join } from 'node:path'; | ||
import { logger } from '@rsbuild/core'; | ||
import type { pluginDtsOptions } from 'src'; | ||
import * as ts from 'typescript'; | ||
import { ensureTempDeclarationDir, loadTsconfig } from './utils'; | ||
|
||
export function emitDts(options: pluginDtsOptions): { | ||
outDir: string; | ||
} { | ||
const { tsconfigPath, distPath, bundle } = options; | ||
const cwd = process.cwd(); | ||
const configPath = tsconfigPath ? join(cwd, tsconfigPath) : cwd; | ||
const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath); | ||
|
||
const getDeclarationDir = (bundle: boolean, distPath?: string) => { | ||
if (bundle) { | ||
return ensureTempDeclarationDir(); | ||
} | ||
|
||
return distPath ? distPath : rawCompilerOptions.declarationDir; | ||
}; | ||
|
||
const outDir = distPath ? distPath : rawCompilerOptions.declarationDir; | ||
|
||
const compilerOptions = { | ||
...rawCompilerOptions, | ||
noEmit: false, | ||
declaration: true, | ||
declarationDir: getDeclarationDir(bundle, distPath), | ||
emitDeclarationOnly: true, | ||
}; | ||
|
||
const host: ts.CompilerHost = ts.createCompilerHost(compilerOptions); | ||
|
||
const program: ts.Program = ts.createProgram( | ||
fileNames, | ||
compilerOptions, | ||
host, | ||
); | ||
|
||
const emitResult = program.emit(); | ||
|
||
const allDiagnostics = ts | ||
.getPreEmitDiagnostics(program) | ||
.concat(emitResult.diagnostics); | ||
|
||
const diagnosticMessages: string[] = []; | ||
|
||
for (const diagnostic of allDiagnostics) { | ||
if (diagnostic.file) { | ||
const { line, character } = ts.getLineAndCharacterOfPosition( | ||
diagnostic.file, | ||
diagnostic.start!, | ||
); | ||
const message = ts.flattenDiagnosticMessageText( | ||
diagnostic.messageText, | ||
'\n', | ||
); | ||
diagnosticMessages.push( | ||
`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`, | ||
); | ||
} else { | ||
const message = ts.flattenDiagnosticMessageText( | ||
diagnostic.messageText, | ||
'\n', | ||
); | ||
diagnosticMessages.push(message); | ||
} | ||
} | ||
|
||
if (diagnosticMessages.length) { | ||
logger.error( | ||
`Failed to emit declaration files.\n${diagnosticMessages.join('\n')}\n`, | ||
); | ||
throw new Error('TypeScript compilation failed'); | ||
} | ||
|
||
logger.info('TypeScript compilation succeeded\n'); | ||
|
||
return { | ||
outDir: outDir || './dist', | ||
}; | ||
} |
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
Oops, something went wrong.