-
-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathoutputFile.ts
More file actions
130 lines (114 loc) · 3.98 KB
/
outputFile.ts
File metadata and controls
130 lines (114 loc) · 3.98 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as path from 'path';
import { promises as fs } from 'fs';
import type typescript from 'typescript';
import type { OutputOptions, PluginContext, SourceDescription } from 'rollup';
import type { ParsedCommandLine } from 'typescript';
import type TSCache from './tscache';
export interface TypescriptSourceDescription extends Partial<SourceDescription> {
declarations: string[];
}
/**
* Checks if the given OutputFile represents some code
*/
export function isCodeOutputFile(name: string): boolean {
return !isMapOutputFile(name) && !isDeclarationOutputFile(name);
}
/**
* Checks if the given OutputFile represents some source map
*/
export function isMapOutputFile(name: string): boolean {
return name.endsWith('.map');
}
/**
* Checks if the given OutputFile represents some TypeScript source map
*/
export function isTypeScriptMapOutputFile(name: string): boolean {
return name.endsWith('ts.map');
}
/**
* Checks if the given OutputFile represents some declaration
*/
export function isDeclarationOutputFile(name: string): boolean {
return /\.d(\..+)?\.[cm]?ts$/.test(name);
}
/**
* Returns the content of a filename either from the current
* typescript compiler instance or from the cached content.
* @param fileName The filename for the contents to retrieve
* @param emittedFiles The files emitted in the current typescript instance
* @param tsCache A cache to files cached by Typescript
*/
export function getEmittedFile(
fileName: string | undefined,
emittedFiles: ReadonlyMap<string, string>,
tsCache: TSCache
): string | undefined {
let code: string | undefined;
if (fileName) {
if (emittedFiles.has(fileName)) {
code = emittedFiles.get(fileName);
} else {
code = tsCache.getCached(fileName);
}
}
return code;
}
/**
* Finds the corresponding emitted Javascript files for a given Typescript file.
* @param id Path to the Typescript file.
* @param emittedFiles Map of file names to source code,
* containing files emitted by the Typescript compiler.
*/
export default function findTypescriptOutput(
ts: typeof typescript,
parsedOptions: ParsedCommandLine,
id: string,
emittedFiles: ReadonlyMap<string, string>,
tsCache: TSCache
): TypescriptSourceDescription {
const emittedFileNames = ts.getOutputFileNames(
parsedOptions,
id,
!ts.sys.useCaseSensitiveFileNames
);
const codeFile = emittedFileNames.find(isCodeOutputFile);
const mapFile = emittedFileNames.find(isMapOutputFile);
return {
code: getEmittedFile(codeFile, emittedFiles, tsCache),
map: getEmittedFile(mapFile, emittedFiles, tsCache),
declarations: emittedFileNames.filter((name) => name !== codeFile && name !== mapFile)
};
}
export function normalizePath(fileName: string) {
return fileName.split(path.win32.sep).join(path.posix.sep);
}
export async function emitFile(
{ dir }: OutputOptions,
outputToFilesystem: boolean | undefined,
context: PluginContext,
filePath: string,
fileSource: string
) {
const normalizedFilePath = normalizePath(filePath);
// const normalizedPath = normalizePath(filePath);
// Note: `dir` can be a value like `dist` in which case, `path.relative` could result in a value
// of something like `'../.tsbuildinfo'. Our else-case below needs to mimic `path.relative`
// returning a dot-notated relative path, so the first if-then branch is entered into
const relativePath = dir ? path.relative(dir, normalizedFilePath) : '..';
// legal paths do not start with . nor .. : https://github.com/rollup/rollup/issues/3507#issuecomment-616495912
if (relativePath.startsWith('..')) {
if (outputToFilesystem == null) {
context.warn(`@rollup/plugin-typescript: outputToFilesystem option is defaulting to true.`);
}
if (outputToFilesystem !== false) {
await fs.mkdir(path.dirname(normalizedFilePath), { recursive: true });
await fs.writeFile(normalizedFilePath, fileSource);
}
} else {
context.emitFile({
type: 'asset',
fileName: relativePath,
source: fileSource
});
}
}