-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbundle-declarations-for-chunk.ts
29 lines (23 loc) · 1.22 KB
/
bundle-declarations-for-chunk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import {SourceDescription, SourceMap} from "rollup";
import {D_TS_EXTENSION, D_TS_MAP_EXTENSION, SOURCE_MAP_COMMENT, SOURCE_MAP_COMMENT_REGEXP} from "../../../constant/constant";
import {declarationBundler} from "../../transformer/declaration-bundler/declaration-bundler";
import {DeclarationBundlerOptions} from "../../transformer/declaration-bundler/declaration-bundler-options";
export interface BundleDeclarationsForChunkOptions extends DeclarationBundlerOptions {}
export interface BundleDeclarationsForChunkResult extends SourceDescription {}
export function bundleDeclarationsForChunk(options: BundleDeclarationsForChunkOptions): BundleDeclarationsForChunkResult {
let code = "";
let map: SourceMap | undefined;
const emitOutput = options.host.emit(undefined, true, declarationBundler(options));
for (const {name, text} of emitOutput.outputFiles) {
if (name.endsWith(D_TS_MAP_EXTENSION)) {
map = JSON.parse(text) as SourceMap;
map.file = options.declarationPaths.fileName;
} else if (name.endsWith(D_TS_EXTENSION)) {
code += text.replace(SOURCE_MAP_COMMENT_REGEXP, `${SOURCE_MAP_COMMENT}=${options.declarationMapPaths.fileName}`);
}
}
return {
code,
...(map == null ? {} : {map: JSON.stringify(map)})
};
}