Skip to content

Commit 9038a8c

Browse files
committed
code cleanup
1 parent 372347b commit 9038a8c

3 files changed

Lines changed: 58 additions & 111 deletions

File tree

examples/helloworld/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ load("//rules:defs.bzl", "closure_ts_library")
33
closure_ts_library(
44
name = "lib",
55
srcs = ["index.ts"],
6-
tsconfig = "//:tsconfig",
76
)

rules/private/closure_ts_library.bzl

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,11 @@ def _output_for_input(ctx, src):
44
filename = paths.replace_extension(src.basename, ".js")
55
return ctx.actions.declare_file(filename, sibling = src)
66

7-
def _tsickle_config_action(ctx):
8-
# Should conform with TsickleHostConfig interface
9-
config = struct(
10-
rootModulePath = "",
11-
es5Mode = True,
12-
ignoreWarningsPath = [],
13-
typeBlackListPaths = [],
14-
unknownTypesPaths = [],
15-
)
16-
17-
ctx.actions.write(
18-
output = ctx.outputs.tsickle_config,
19-
content = json.encode_indent(config, prefix = "", indent = " "),
20-
)
21-
227
def _tsickle_action(ctx):
238
args = ctx.actions.args()
249
args.add_all([src.short_path for src in ctx.files.srcs])
2510

26-
inputs = [ctx.file.tsconfig, ctx.outputs.tsickle_config] + ctx.files.srcs
11+
inputs = ctx.files.srcs
2712
outputs = [_output_for_input(ctx, f) for f in ctx.files.srcs]
2813

2914
ctx.actions.run(
@@ -36,21 +21,16 @@ def _tsickle_action(ctx):
3621
"BAZEL_BINDIR": ctx.bin_dir.path,
3722
"BAZEL_WORKSPACE": ctx.label.workspace_name,
3823
"BAZEL_PACKAGE": ctx.label.package,
39-
"TSICKLE_CONFIG_FILE": ctx.outputs.tsickle_config.short_path,
40-
"TS_CONFIG_FILE": ctx.file.tsconfig.short_path,
4124
},
4225
)
4326

4427
return outputs
4528

4629
def _closure_ts_library_impl(ctx):
47-
_tsickle_config_action(ctx)
48-
49-
output_files = [ctx.outputs.tsickle_config]
50-
output_files.extend(_tsickle_action(ctx))
30+
outputs = _tsickle_action(ctx)
5131

5232
return [DefaultInfo(
53-
files = depset(output_files),
33+
files = depset(outputs),
5434
)]
5535

5636
closure_ts_library = rule(
@@ -61,19 +41,11 @@ closure_ts_library = rule(
6141
allow_files = True,
6242
mandatory = True,
6343
),
64-
"tsconfig": attr.label(
65-
doc = "the tsconfig.json file",
66-
allow_single_file = True,
67-
mandatory = True,
68-
),
6944
"_compiler": attr.label(
7045
default = "//tools/tsicklecompiler",
7146
doc = "the tsickle driver tool",
7247
executable = True,
7348
cfg = "exec",
7449
),
7550
},
76-
outputs = {
77-
"tsickle_config": "%{name}.tsickle.json",
78-
},
7951
)

tools/tsicklecompiler/index.ts

Lines changed: 55 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { promises as asyncfs } from 'fs';
55
import ts from 'typescript';
66
import * as tsickle from 'tsickle';
77

8-
const DEBUG = true;
8+
const DEBUG = false;
99

1010
function getExecRoot(): string {
1111
return process.env.JS_BINARY__EXECROOT || '.';
@@ -16,12 +16,6 @@ function getBazelBinDir(): string {
1616
return path.join(getExecRoot(), binDir);
1717
}
1818

19-
function getOutDir(execRoot: string): string {
20-
const pkgdir = process.env.BAZEL_PACKAGE!;
21-
const bindir = process.env.BAZEL_BINDIR!;
22-
return path.join(execRoot, bindir, pkgdir);
23-
}
24-
2519
async function listFiles(dir: string): Promise<string[]> {
2620
return asyncfs.readdir(dir, { recursive: true });
2721
}
@@ -40,12 +34,7 @@ async function listExecrootFiles() {
4034
}
4135

4236
function getInputFiles(execRoot: string, args: string[]): string[] {
43-
const inputFiles = args.map(arg => `${execRoot}/${arg}`);
44-
if (inputFiles.length === 0) {
45-
console.error('Usage: tsicklecompiler <input.ts> [input2.ts] ...');
46-
process.exit(1);
47-
}
48-
return inputFiles;
37+
return args.map(arg => `${execRoot}/${arg}`);
4938
}
5039

5140
function getTsCompilerOptions(): ts.CompilerOptions {
@@ -59,79 +48,20 @@ function getTsCompilerOptions(): ts.CompilerOptions {
5948
};
6049
}
6150

62-
/**
63-
* Determine the lowest-level common parent directory of the given list of files.
64-
*/
65-
function getCommonParentDirectory(fileNames: string[]): string {
66-
const pathSplitter = /[\/\\]+/;
67-
const commonParent = fileNames[0].split(pathSplitter);
68-
for (let i = 1; i < fileNames.length; i++) {
69-
const thisPath = fileNames[i].split(pathSplitter);
70-
let j = 0;
71-
while (thisPath[j] === commonParent[j]) {
72-
j++;
73-
}
74-
commonParent.length = j; // Truncate without copying the array
75-
}
76-
if (commonParent.length === 0) {
77-
return '/';
78-
} else {
79-
return commonParent.join(path.sep);
80-
}
81-
}
82-
83-
async function main() {
84-
const execRoot = getExecRoot();
85-
const outDir = getOutDir(execRoot);
86-
const args = process.argv.slice(2);
87-
const inputFiles = getInputFiles(execRoot, args);
88-
// const inputFiles = args;
89-
const compilerOptions = getTsCompilerOptions();
90-
91-
if (DEBUG) {
92-
const cwd = process.cwd()
93-
// console.log('env:', process.env);
94-
console.log('pwd:', cwd);
95-
console.log('args:', args);
96-
console.log('files:', inputFiles);
97-
console.log('compilerOptions:', compilerOptions);
98-
}
99-
100-
const result = run(compilerOptions, inputFiles, (filePath: string, contents: string) => {
101-
console.log('emitted file:', filePath);
102-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
103-
fs.writeFileSync(filePath, contents, { encoding: 'utf-8' });
104-
});
105-
106-
if (result.diagnostics.length) {
107-
console.error(ts.formatDiagnostics(result.diagnostics, ts.createCompilerHost(compilerOptions)));
108-
return 1;
109-
}
110-
111-
// if (settings.externsPath) {
112-
// fs.mkdirSync(path.dirname(settings.externsPath), { recursive: true });
113-
// fs.writeFileSync(
114-
// settings.externsPath,
115-
// tsickle.getGeneratedExterns(result.externs, config.options.rootDir || ''));
116-
// }
117-
118-
return 0;
119-
120-
}
121-
12251
function run(
12352
options: ts.CompilerOptions,
124-
absoluteFileNames: string[],
53+
fileNames: string[],
12554
writeFile: ts.WriteFileCallback,
12655
): tsickle.EmitResult {
12756
// Use absolute paths to determine what files to process since files may be imported using
12857
// relative or absolute paths
12958
// const absoluteFileNames = fileNames.map(i => path.resolve(i));
13059

13160
const compilerHost = ts.createCompilerHost(options);
132-
const program = ts.createProgram(absoluteFileNames, options, compilerHost);
133-
const filesToProcess = new Set(absoluteFileNames);
134-
const rootModulePath = options.rootDir || getCommonParentDirectory(absoluteFileNames);
61+
const program = ts.createProgram(fileNames, options, compilerHost);
62+
const filesToProcess = new Set(fileNames);
63+
const rootModulePath = options.rootDir!;
64+
13565
const transformerHost: tsickle.TsickleHost = {
13666
rootDirsRelative: (f: string) => f,
13767
shouldSkipTsickleProcessing: (fileName: string) => {
@@ -150,9 +80,9 @@ function run(
15080
console.error(ts.formatDiagnostics([warning], compilerHost)),
15181
options,
15282
generateExtraSuppressions: true,
153-
// transformDynamicImport: 'nodejs',
15483
moduleResolutionHost: compilerHost,
15584
};
85+
15686
const diagnostics = ts.getPreEmitDiagnostics(program);
15787
if (diagnostics.length > 0) {
15888
return {
@@ -162,10 +92,56 @@ function run(
16292
externs: {},
16393
emitSkipped: true,
16494
emittedFiles: [],
165-
// fileSummaries: new Map(),
16695
};
16796
}
97+
16898
return tsickle.emit(program, transformerHost, writeFile);
16999
}
170100

101+
async function main() {
102+
const execRoot = getExecRoot();
103+
104+
const args = process.argv.slice(2);
105+
const inputFiles = getInputFiles(execRoot, args);
106+
if (inputFiles.length === 0) {
107+
console.error('Usage: tsicklecompiler <input.ts> [input2.ts] ...');
108+
process.exit(1);
109+
}
110+
111+
const compilerOptions = getTsCompilerOptions();
112+
113+
if (DEBUG) {
114+
const cwd = process.cwd()
115+
console.log('env:', process.env);
116+
console.log('pwd:', cwd);
117+
console.log('args:', args);
118+
console.log('inputFiles:', inputFiles);
119+
console.log('execRoot:', execRoot);
120+
console.log('compilerOptions:', compilerOptions);
121+
}
122+
123+
const result = run(compilerOptions, inputFiles, (filePath: string, contents: string) => {
124+
if (DEBUG) {
125+
console.log('emitted file:', filePath);
126+
}
127+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
128+
fs.writeFileSync(filePath, contents, { encoding: 'utf-8' });
129+
});
130+
131+
if (result.diagnostics.length) {
132+
console.error(ts.formatDiagnostics(result.diagnostics, ts.createCompilerHost(compilerOptions)));
133+
return 1;
134+
}
135+
136+
// if (settings.externsPath) {
137+
// fs.mkdirSync(path.dirname(settings.externsPath), { recursive: true });
138+
// fs.writeFileSync(
139+
// settings.externsPath,
140+
// tsickle.getGeneratedExterns(result.externs, config.options.rootDir || ''));
141+
// }
142+
143+
return 0;
144+
145+
}
146+
171147
void main()

0 commit comments

Comments
 (0)