-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
187 lines (163 loc) · 5.85 KB
/
index.ts
File metadata and controls
187 lines (163 loc) · 5.85 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as path from 'path';
import * as fs from 'fs';
import { promises as asyncfs } from 'fs';
import ts from 'typescript';
import * as tsickle from '../../vendor/tsickle/src/tsickle';
const DEBUG = false;
function getExternsPath(): string | undefined {
return process.env.EXTERNS_PATH;
}
function getExecRoot(): string {
return process.env.JS_BINARY__EXECROOT || '.';
}
function getBazelBinDir(): string {
const binDir = process.env.BAZEL_BINDIR!;
return path.join(getExecRoot(), binDir);
}
async function listFiles(dir: string): Promise<string[]> {
return asyncfs.readdir(dir, { recursive: true });
}
async function listExecrootFiles() {
const execroot = process.env.JS_BINARY__EXECROOT || '.';
for (const filename of await listFiles(execroot)) {
if (filename.indexOf('.aspect_rules_js') >= 0) {
continue;
}
if (filename.indexOf('tsconfig.json') < 0) {
continue;
}
console.log(`{execroot}/${filename}`);
}
}
function getInputFiles(execRoot: string, args: string[]): string[] {
return args.map(arg => `${execRoot}/${arg}`);
}
function getTsCompilerOptions(): ts.CompilerOptions {
return {
target: ts.ScriptTarget.ES2017,
module: ts.ModuleKind.CommonJS,
outDir: getBazelBinDir(),
rootDir: getExecRoot(),
declaration: true,
importHelpers: true,
strict: true
};
}
/**
* stripExternalName is used to remove external workspace identifiers from bazel
* paths. This allows the filenames
* BAZEL_BIN/external/io_bazel_rules_closure+/google3/third_party/javascript/safevalues/builders/html_sanitizer/sanitizer_table/sanitizer_table.js
* and
* BAZEL_BIN/google3/third_party/javascript/safevalues/builders/html_sanitizer/sanitizer_table/sanitizer_table.js
* to generate the same moduleName.
*
* @param moduleName the module name to strip
* @returns
*/
function stripExternalName(moduleName: string): string {
if (!moduleName.startsWith('external.')) {
return moduleName;
}
const parts = moduleName.split('.');
if (parts.length <= 2) {
return moduleName;
}
return parts.slice(2).join('.');
}
function run(
options: ts.CompilerOptions,
fileNames: string[],
writeFile: ts.WriteFileCallback,
): tsickle.EmitResult {
// Use absolute paths to determine what files to process since files may be
// imported using relative or absolute paths
fileNames = fileNames.map(i => path.resolve(i));
const compilerHost = ts.createCompilerHost(options);
const program = ts.createProgram(fileNames, options, compilerHost);
const filesToProcess = new Set(fileNames);
const rootModulePath = options.rootDir!;
const transformerHost: tsickle.TsickleHost = {
rootDirsRelative: (f: string) => f,
shouldSkipTsickleProcessing: (fileName: string) => {
return !filesToProcess.has(path.resolve(fileName));
},
shouldIgnoreWarningsForPath: (fileName: string) => false,
pathToModuleName: (context, fileName) => {
const moduleName = stripExternalName(tsickle.pathToModuleName(rootModulePath, context, fileName));
if (DEBUG) {
console.log(`pathToModuleName: ${fileName} => ${moduleName}`);
}
return moduleName;
},
fileNameToModuleId: (fileName) => {
const moduleId = path.relative(rootModulePath, fileName);
if (DEBUG) {
console.log(`fileNameToModuleId: ${fileName} => ${moduleId}`);
}
return moduleId;
},
googmodule: true,
transformDecorators: true,
transformTypesToClosure: true,
typeBlackListPaths: new Set(),
untyped: false,
logWarning: (warning) =>
console.error(ts.formatDiagnostics([warning], compilerHost)),
options,
generateExtraSuppressions: false,
provideExternalModuleDtsNamespace: true,
transformDynamicImport: "closure",
};
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length > 0) {
return {
tsMigrationExportsShimFiles: new Map(),
diagnostics,
modulesManifest: new tsickle.ModulesManifest(),
externs: {},
emitSkipped: true,
emittedFiles: [],
fileSummaries: new Map(),
};
}
return tsickle.emit(program, transformerHost, writeFile);
}
async function main() {
const execRoot = getExecRoot();
const externsPath = getExternsPath();
const args = process.argv.slice(2);
const inputFiles = getInputFiles(execRoot, args);
if (inputFiles.length === 0) {
console.error('Usage: tsicklecompiler <input.ts> [input2.ts] ...');
process.exit(1);
}
const compilerOptions = getTsCompilerOptions();
if (DEBUG) {
const cwd = process.cwd()
console.log('env:', process.env);
console.log('pwd:', cwd);
console.log('args:', args);
console.log('inputFiles:', inputFiles);
console.log('execRoot:', execRoot);
console.log('compilerOptions:', compilerOptions);
}
const result = run(compilerOptions, inputFiles, (filePath: string, contents: string) => {
if (DEBUG) {
console.log('emitted file:', filePath);
}
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, contents, { encoding: 'utf-8' });
});
if (result.diagnostics.length) {
console.error(ts.formatDiagnostics(result.diagnostics, ts.createCompilerHost(compilerOptions)));
return 1;
}
if (externsPath) {
fs.mkdirSync(path.dirname(externsPath), { recursive: true });
fs.writeFileSync(
externsPath,
tsickle.getGeneratedExterns(result.externs, compilerOptions.rootDir || ''));
}
return 0;
}
void main()