Skip to content

Commit 16fd947

Browse files
committed
Reorganized code to keep createBundle and updateBundle in factory.ts
1 parent 5501892 commit 16fd947

File tree

3 files changed

+78
-77
lines changed

3 files changed

+78
-77
lines changed

src/compiler/emitter.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,70 @@ namespace ts {
88
const delimiters = createDelimiterMap();
99
const brackets = createBracketsMap();
1010

11+
/*@internal*/
12+
/**
13+
* Iterates over the source files that are expected to have an emit output.
14+
*
15+
* @param host An EmitHost.
16+
* @param action The action to execute.
17+
* @param sourceFilesOrTargetSourceFile
18+
* If an array, the full list of source files to emit.
19+
* Else, calls `getSourceFilesToEmit` with the (optional) target source file to determine the list of source files to emit.
20+
*/
21+
export function forEachEmittedFile(
22+
host: EmitHost, action: (emitFileNames: EmitFileNames, sourceFileOrBundle: SourceFile | Bundle, emitOnlyDtsFiles: boolean) => void,
23+
sourceFilesOrTargetSourceFile?: SourceFile[] | SourceFile,
24+
emitOnlyDtsFiles?: boolean) {
25+
26+
const sourceFiles = isArray(sourceFilesOrTargetSourceFile) ? sourceFilesOrTargetSourceFile : getSourceFilesToEmit(host, sourceFilesOrTargetSourceFile);
27+
const options = host.getCompilerOptions();
28+
if (options.outFile || options.out) {
29+
if (sourceFiles.length) {
30+
const jsFilePath = options.outFile || options.out;
31+
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
32+
const declarationFilePath = options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : "";
33+
action({ jsFilePath, sourceMapFilePath, declarationFilePath }, createBundle(sourceFiles), emitOnlyDtsFiles);
34+
}
35+
}
36+
else {
37+
for (const sourceFile of sourceFiles) {
38+
const jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, getOutputExtension(sourceFile, options));
39+
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
40+
const declarationFilePath = !isSourceFileJavaScript(sourceFile) && (emitOnlyDtsFiles || options.declaration) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined;
41+
action({ jsFilePath, sourceMapFilePath, declarationFilePath }, sourceFile, emitOnlyDtsFiles);
42+
}
43+
}
44+
}
45+
46+
function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
47+
return options.sourceMap ? jsFilePath + ".map" : undefined;
48+
}
49+
50+
// JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
51+
// So for JavaScript files, '.jsx' is only emitted if the input was '.jsx', and JsxEmit.Preserve.
52+
// For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve
53+
function getOutputExtension(sourceFile: SourceFile, options: CompilerOptions): string {
54+
if (options.jsx === JsxEmit.Preserve) {
55+
if (isSourceFileJavaScript(sourceFile)) {
56+
if (fileExtensionIs(sourceFile.fileName, ".jsx")) {
57+
return ".jsx";
58+
}
59+
}
60+
else if (sourceFile.languageVariant === LanguageVariant.JSX) {
61+
// TypeScript source file preserving JSX syntax
62+
return ".jsx";
63+
}
64+
}
65+
return ".js";
66+
}
67+
68+
function getOriginalSourceFileOrBundle(sourceFileOrBundle: SourceFile | Bundle) {
69+
if (sourceFileOrBundle.kind === SyntaxKind.Bundle) {
70+
return updateBundle(sourceFileOrBundle, sameMap(sourceFileOrBundle.sourceFiles, getOriginalSourceFile));
71+
}
72+
return getOriginalSourceFile(sourceFileOrBundle);
73+
}
74+
1175
/*@internal*/
1276
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature
1377
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile, emitOnlyDtsFiles?: boolean, transformers?: TransformerFactory<SourceFile>[]): EmitResult {

src/compiler/factory.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,19 @@ namespace ts {
21192119
: node;
21202120
}
21212121

2122+
export function createBundle(sourceFiles: SourceFile[]) {
2123+
const node = <Bundle>createNode(SyntaxKind.Bundle);
2124+
node.sourceFiles = sourceFiles;
2125+
return node;
2126+
}
2127+
2128+
export function updateBundle(node: Bundle, sourceFiles: SourceFile[]) {
2129+
if (node.sourceFiles !== sourceFiles) {
2130+
return createBundle(sourceFiles);
2131+
}
2132+
return node;
2133+
}
2134+
21222135
// Compound nodes
21232136

21242137
export function createComma(left: Expression, right: Expression) {

src/compiler/utilities.ts

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,27 +2129,7 @@ namespace ts {
21292129
|| positionIsSynthesized(node.end);
21302130
}
21312131

2132-
export function createBundle(sourceFiles: SourceFile[]) {
2133-
const node = <Bundle>createNode(SyntaxKind.Bundle);
2134-
node.sourceFiles = sourceFiles;
2135-
return node;
2136-
}
2137-
2138-
export function updateBundle(node: Bundle, sourceFiles: SourceFile[]) {
2139-
if (node.sourceFiles !== sourceFiles) {
2140-
return createBundle(sourceFiles);
2141-
}
2142-
return node;
2143-
}
2144-
2145-
export function getOriginalSourceFileOrBundle(sourceFileOrBundle: SourceFile | Bundle) {
2146-
if (sourceFileOrBundle.kind === SyntaxKind.Bundle) {
2147-
return updateBundle(sourceFileOrBundle, sameMap(sourceFileOrBundle.sourceFiles, getOriginalSourceFile));
2148-
}
2149-
return getOriginalSourceFile(sourceFileOrBundle);
2150-
}
2151-
2152-
function getOriginalSourceFile(sourceFile: SourceFile) {
2132+
export function getOriginalSourceFile(sourceFile: SourceFile) {
21532133
return getParseTreeNode(sourceFile, isSourceFile) || sourceFile;
21542134
}
21552135

@@ -2664,62 +2644,6 @@ namespace ts {
26642644
return !(options.noEmitForJsFiles && isSourceFileJavaScript(sourceFile)) && !sourceFile.isDeclarationFile && !isSourceFileFromExternalLibrary(sourceFile);
26652645
}
26662646

2667-
/**
2668-
* Iterates over the source files that are expected to have an emit output.
2669-
*
2670-
* @param host An EmitHost.
2671-
* @param action The action to execute.
2672-
* @param sourceFilesOrTargetSourceFile
2673-
* If an array, the full list of source files to emit.
2674-
* Else, calls `getSourceFilesToEmit` with the (optional) target source file to determine the list of source files to emit.
2675-
*/
2676-
export function forEachEmittedFile(
2677-
host: EmitHost, action: (emitFileNames: EmitFileNames, sourceFileOrBundle: SourceFile | Bundle, emitOnlyDtsFiles: boolean) => void,
2678-
sourceFilesOrTargetSourceFile?: SourceFile[] | SourceFile,
2679-
emitOnlyDtsFiles?: boolean) {
2680-
2681-
const sourceFiles = isArray(sourceFilesOrTargetSourceFile) ? sourceFilesOrTargetSourceFile : getSourceFilesToEmit(host, sourceFilesOrTargetSourceFile);
2682-
const options = host.getCompilerOptions();
2683-
if (options.outFile || options.out) {
2684-
if (sourceFiles.length) {
2685-
const jsFilePath = options.outFile || options.out;
2686-
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
2687-
const declarationFilePath = options.declaration ? removeFileExtension(jsFilePath) + ".d.ts" : "";
2688-
action({ jsFilePath, sourceMapFilePath, declarationFilePath }, createBundle(sourceFiles), emitOnlyDtsFiles);
2689-
}
2690-
}
2691-
else {
2692-
for (const sourceFile of sourceFiles) {
2693-
const jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, getOutputExtension(sourceFile, options));
2694-
const sourceMapFilePath = getSourceMapFilePath(jsFilePath, options);
2695-
const declarationFilePath = !isSourceFileJavaScript(sourceFile) && (emitOnlyDtsFiles || options.declaration) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined;
2696-
action({ jsFilePath, sourceMapFilePath, declarationFilePath }, sourceFile, emitOnlyDtsFiles);
2697-
}
2698-
}
2699-
}
2700-
2701-
function getSourceMapFilePath(jsFilePath: string, options: CompilerOptions) {
2702-
return options.sourceMap ? jsFilePath + ".map" : undefined;
2703-
}
2704-
2705-
// JavaScript files are always LanguageVariant.JSX, as JSX syntax is allowed in .js files also.
2706-
// So for JavaScript files, '.jsx' is only emitted if the input was '.jsx', and JsxEmit.Preserve.
2707-
// For TypeScript, the only time to emit with a '.jsx' extension, is on JSX input, and JsxEmit.Preserve
2708-
function getOutputExtension(sourceFile: SourceFile, options: CompilerOptions): string {
2709-
if (options.jsx === JsxEmit.Preserve) {
2710-
if (isSourceFileJavaScript(sourceFile)) {
2711-
if (fileExtensionIs(sourceFile.fileName, ".jsx")) {
2712-
return ".jsx";
2713-
}
2714-
}
2715-
else if (sourceFile.languageVariant === LanguageVariant.JSX) {
2716-
// TypeScript source file preserving JSX syntax
2717-
return ".jsx";
2718-
}
2719-
}
2720-
return ".js";
2721-
}
2722-
27232647
export function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string) {
27242648
let sourceFilePath = getNormalizedAbsolutePath(sourceFile.fileName, host.getCurrentDirectory());
27252649
const commonSourceDirectory = host.getCommonSourceDirectory();

0 commit comments

Comments
 (0)