Skip to content

Commit 213b204

Browse files
authored
Bundle librarymode error & tstlverbose CLI flag (#1043)
* Disallow bundling with buildmode library * Add tstl verbose CLI flag * Add tstlverbose to CLI parser
1 parent d79a207 commit 213b204

File tree

19 files changed

+191
-88
lines changed

19 files changed

+191
-88
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"jest-circus": "^25.1.0",
6565
"lua-types": "^2.8.0",
6666
"lua-wasm-bindings": "^0.2.2",
67-
"prettier": "^2.0.5",
67+
"prettier": "^2.3.2",
6868
"ts-jest": "^26.3.0",
6969
"ts-node": "^8.6.2"
7070
}

src/CompilerOptions.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import * as ts from "typescript";
22
import * as diagnosticFactories from "./transpilation/diagnostics";
33

4-
type KnownKeys<T> = { [K in keyof T]: string extends K ? never : number extends K ? never : K } extends {
5-
[K in keyof T]: infer U;
6-
}
7-
? U
8-
: never;
9-
10-
type OmitIndexSignature<T extends Record<any, any>> = Pick<T, KnownKeys<T>>;
4+
type OmitIndexSignature<T> = {
5+
[K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
6+
};
117

128
export interface TransformerImport {
139
transform: string;
@@ -35,6 +31,7 @@ export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
3531
sourceMapTraceback?: boolean;
3632
luaPlugins?: LuaPluginImport[];
3733
plugins?: Array<ts.PluginImport | TransformerImport>;
34+
tstlVerbose?: boolean;
3835
[option: string]: any;
3936
};
4037

@@ -73,5 +70,9 @@ export function validateOptions(options: CompilerOptions): ts.Diagnostic[] {
7370
diagnostics.push(diagnosticFactories.usingLuaBundleWithInlineMightGenerateDuplicateCode());
7471
}
7572

73+
if (options.luaBundle && options.buildMode === BuildMode.Library) {
74+
diagnostics.push(diagnosticFactories.cannotBundleLibrary());
75+
}
76+
7677
return diagnostics;
7778
}

src/cli/parse.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export const optionDeclarations: CommandLineOption[] = [
7373
description: "List of TypeScriptToLua plugins.",
7474
type: "object",
7575
},
76+
{
77+
name: "tstlVerbose",
78+
description: "Provide verbose output useful for diagnosing problems.",
79+
type: "boolean",
80+
},
7681
];
7782

7883
export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine): ParsedCommandLine {

src/transformation/context/context.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ export interface DiagnosticsProducingTypeChecker extends ts.TypeChecker {
2727

2828
export class TransformationContext {
2929
public readonly diagnostics: ts.Diagnostic[] = [];
30-
public readonly checker: DiagnosticsProducingTypeChecker = (this
31-
.program as any).getDiagnosticsProducingTypeChecker();
30+
public readonly checker: DiagnosticsProducingTypeChecker = this.program.getDiagnosticsProducingTypeChecker();
3231
public readonly resolver: EmitResolver;
3332

3433
public readonly options: CompilerOptions = this.program.getCompilerOptions();

src/transpilation/diagnostics.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ export const usingLuaBundleWithInlineMightGenerateDuplicateCode = createSerialDi
4848
"Using 'luaBundle' with 'luaLibImport: \"inline\"' might generate duplicate code. " +
4949
"It is recommended to use 'luaLibImport: \"require\"'.",
5050
}));
51+
52+
export const cannotBundleLibrary = createDiagnosticFactory(
53+
() =>
54+
'Cannot bundle probjects with"buildmode": "library". Projects including the library can still bundle (which will include external library files).'
55+
);

src/transpilation/resolve.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import * as fs from "fs";
55
import { EmitHost, ProcessedFile } from "./utils";
66
import { SourceNode } from "source-map";
77
import { getEmitPathRelativeToOutDir, getProjectRoot, getSourceDir } from "./transpiler";
8-
import { formatPathToLuaPath, trimExtension } from "../utils";
8+
import { formatPathToLuaPath, normalizeSlashes, trimExtension } from "../utils";
99
import { couldNotReadDependency, couldNotResolveRequire } from "./diagnostics";
10-
import { BuildMode } from "../CompilerOptions";
10+
import { BuildMode, CompilerOptions } from "../CompilerOptions";
1111

1212
const resolver = resolve.ResolverFactory.createResolver({
1313
extensions: [".lua"],
@@ -24,9 +24,14 @@ interface ResolutionResult {
2424
export function resolveDependencies(program: ts.Program, files: ProcessedFile[], emitHost: EmitHost): ResolutionResult {
2525
const outFiles: ProcessedFile[] = [...files];
2626
const diagnostics: ts.Diagnostic[] = [];
27+
const options = program.getCompilerOptions() as CompilerOptions;
2728

2829
// Resolve dependencies for all processed files
2930
for (const file of files) {
31+
if (options.tstlVerbose) {
32+
console.log(`Resolving dependencies for ${normalizeSlashes(file.fileName)}`);
33+
}
34+
3035
const resolutionResult = resolveFileDependencies(file, program, emitHost);
3136
outFiles.push(...resolutionResult.resolvedFiles);
3237
diagnostics.push(...resolutionResult.diagnostics);
@@ -38,6 +43,7 @@ export function resolveDependencies(program: ts.Program, files: ProcessedFile[],
3843
function resolveFileDependencies(file: ProcessedFile, program: ts.Program, emitHost: EmitHost): ResolutionResult {
3944
const dependencies: ProcessedFile[] = [];
4045
const diagnostics: ts.Diagnostic[] = [];
46+
const options = program.getCompilerOptions() as CompilerOptions;
4147

4248
for (const required of findRequiredPaths(file.code)) {
4349
// Do no resolve lualib
@@ -57,6 +63,10 @@ function resolveFileDependencies(file: ProcessedFile, program: ts.Program, emitH
5763
const fileDir = path.dirname(file.fileName);
5864
const resolvedDependency = resolveDependency(fileDir, required, program, emitHost);
5965
if (resolvedDependency) {
66+
if (options.tstlVerbose) {
67+
console.log(`Resolved ${required} to ${normalizeSlashes(resolvedDependency)}`);
68+
}
69+
6070
// Figure out resolved require path and dependency output path
6171
const resolvedRequire = getEmitPathRelativeToOutDir(resolvedDependency, program);
6272

@@ -100,6 +110,11 @@ function resolveDependency(
100110
program: ts.Program,
101111
emitHost: EmitHost
102112
): string | undefined {
113+
const options = program.getCompilerOptions() as CompilerOptions;
114+
if (options.tstlVerbose) {
115+
console.log(`Resolving "${dependency}" from ${normalizeSlashes(fileDirectory)}`);
116+
}
117+
103118
// Check if file is a file in the project
104119
const resolvedPath = path.join(fileDirectory, dependency);
105120

src/transpilation/transpile.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export function getProgramTranspileResult(
2727
): TranspileResult {
2828
const options = program.getCompilerOptions() as CompilerOptions;
2929

30+
if (options.tstlVerbose) {
31+
console.log("Parsing project settings");
32+
}
33+
3034
const diagnostics = validateOptions(options);
3135
let transpiledFiles: ProcessedFile[] = [];
3236

@@ -57,13 +61,26 @@ export function getProgramTranspileResult(
5761
}
5862

5963
const plugins = getPlugins(program, diagnostics, customPlugins);
64+
65+
if (options.tstlVerbose) {
66+
console.log(`Successfully loaded ${plugins.length} plugins`);
67+
}
68+
6069
const visitorMap = createVisitorMap(plugins.map(p => p.visitors).filter(isNonNull));
6170
const printer = createPrinter(plugins.map(p => p.printer).filter(isNonNull));
6271
const processSourceFile = (sourceFile: ts.SourceFile) => {
72+
if (options.tstlVerbose) {
73+
console.log(`Transforming ${sourceFile.fileName}`);
74+
}
75+
6376
const { file, diagnostics: transformDiagnostics } = transformSourceFile(program, sourceFile, visitorMap);
6477

6578
diagnostics.push(...transformDiagnostics);
6679
if (!options.noEmit && !options.emitDeclarationOnly) {
80+
if (options.tstlVerbose) {
81+
console.log(`Printing ${sourceFile.fileName}`);
82+
}
83+
6784
const printResult = printer(program, emitHost, sourceFile.fileName, file);
6885
transpiledFiles.push({
6986
sourceFiles: [sourceFile],

src/transpilation/transpiler.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import * as ts from "typescript";
3-
import { isBundleEnabled } from "../CompilerOptions";
3+
import { CompilerOptions, isBundleEnabled } from "../CompilerOptions";
44
import { getLuaLibBundle } from "../LuaLib";
55
import { normalizeSlashes, trimExtension } from "../utils";
66
import { getBundleResult } from "./bundle";
@@ -29,6 +29,7 @@ export class Transpiler {
2929

3030
public emit(emitOptions: EmitOptions): EmitResult {
3131
const { program, writeFile = this.emitHost.writeFile } = emitOptions;
32+
const verbose = (program.getCompilerOptions() as CompilerOptions).tstlVerbose;
3233
const { diagnostics, transpiledFiles: freshFiles } = getProgramTranspileResult(
3334
this.emitHost,
3435
writeFile,
@@ -37,15 +38,27 @@ export class Transpiler {
3738

3839
const { emitPlan } = this.getEmitPlan(program, diagnostics, freshFiles);
3940

41+
if (verbose) {
42+
console.log("Emitting output");
43+
}
44+
4045
const options = program.getCompilerOptions();
4146
const emitBOM = options.emitBOM ?? false;
4247
for (const { outputPath, code, sourceMap, sourceFiles } of emitPlan) {
48+
if (verbose) {
49+
console.log(`Emitting ${normalizeSlashes(outputPath)}`);
50+
}
51+
4352
writeFile(outputPath, code, emitBOM, undefined, sourceFiles);
4453
if (options.sourceMap && sourceMap !== undefined) {
4554
writeFile(outputPath + ".map", sourceMap, emitBOM, undefined, sourceFiles);
4655
}
4756
}
4857

58+
if (verbose) {
59+
console.log("Emit finished!");
60+
}
61+
4962
return { diagnostics, emitSkipped: emitPlan.length === 0 };
5063
}
5164

@@ -54,10 +67,18 @@ export class Transpiler {
5467
diagnostics: ts.Diagnostic[],
5568
files: ProcessedFile[]
5669
): { emitPlan: EmitFile[] } {
57-
const options = program.getCompilerOptions();
70+
const options = program.getCompilerOptions() as CompilerOptions;
71+
72+
if (options.tstlVerbose) {
73+
console.log("Constructing emit plan");
74+
}
5875

5976
const lualibRequired = files.some(f => f.code.includes('require("lualib_bundle")'));
6077
if (lualibRequired) {
78+
if (options.tstlVerbose) {
79+
console.log("Including lualib bundle");
80+
}
81+
6182
// Add lualib bundle to source dir 'virtually', will be moved to correct output dir in emitPlan
6283
const fileName = normalizeSlashes(path.resolve(getSourceDir(program), "lualib_bundle.lua"));
6384
files.unshift({ fileName, code: getLuaLibBundle(this.emitHost) });

src/typescript-internal.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { DiagnosticsProducingTypeChecker } from "./transformation/context";
2+
13
export {};
24

35
declare module "typescript" {
@@ -14,6 +16,7 @@ declare module "typescript" {
1416

1517
interface Program {
1618
getCommonSourceDirectory(): string;
19+
getDiagnosticsProducingTypeChecker(): DiagnosticsProducingTypeChecker;
1720
}
1821

1922
interface CompilerOptions {

0 commit comments

Comments
 (0)