Description
Enabling bundle-mode (luaBundle and luaBundleEntry) with sourceMapTraceback) will cause to appear {#SourceMapTraceback} in the result file.
Steps to reproduce
- Create a project with dependencies:
{
"typescript": "^4.4.2",
"typescript-to-lua": "^1.0.0"
}
- Create
tsconfig.json with listed content:
{
"$schema": "https://raw.githubusercontent.com/TypeScriptToLua/vscode-typescript-to-lua/master/tsconfig-schema.json",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"tstl": {
"luaTarget": "JIT",
"sourceMapTraceback": true,
"luaBundleEntry": "src/index.ts",
"luaBundle": "dist/index.lua"
}
}
- Create an empty
src/index.ts file
- Run
tstl
- Check the received
dist/index.lua. It looks like:
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
local ____modules = {}
local ____moduleCache = {}
local ____originalRequire = require
local function require(file)
--- require code here, ok .....
end
____modules = {
["lualib_bundle"] = function()
--- lualib code here, ok .....
end,
["index"] = function() --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
require("lualib_bundle");
{#SourceMapTraceback} -- <<--- Look here
end,
}
return require("index")
Why does it happen
The "macro-string" {#SourceMapTraceback} should be replaced here:
|
if (this.options.sourceMapTraceback) { |
|
const stackTraceOverride = this.printStackTraceOverride(rootSourceNode); |
|
code = code.replace("{#SourceMapTraceback}", stackTraceOverride); |
|
} |
Debugging
I will try to debug the compiler and list the ideas below. I believe it will help to determine the problem,
- Debugger says, that
print function from LuaPrinter.ts successfully replaces the macro-string
- Correct version is being added into the
transpiledFiles array
|
const processSourceFile = (sourceFile: ts.SourceFile) => { |
|
const { file, diagnostics: transformDiagnostics } = transformSourceFile(program, sourceFile, visitorMap); |
|
|
|
diagnostics.push(...transformDiagnostics); |
|
if (!options.noEmit && !options.emitDeclarationOnly) { |
|
const printResult = printer(program, emitHost, sourceFile.fileName, file); |
|
transpiledFiles.push({ |
|
sourceFiles: [sourceFile], |
|
fileName: path.normalize(sourceFile.fileName), |
|
luaAst: file, |
|
...printResult, |
|
}); |
|
} |
- I think error is in this line (especially, in
getEmitPlan function). Because freshFiles contains correct code (with replaced macro-string)
|
const { emitPlan } = this.getEmitPlan(program, diagnostics, freshFiles); |
- On this line:
resolutionResult.resolvedFiles contains correct code, bundleFile -- does not
|
const [bundleDiagnostics, bundleFile] = getBundleResult(program, resolutionResult.resolvedFiles); |
- Here. The
moduleSourceNode function. Returns array of strings for lualib_bundle and array with incorrect SourceNode for index
|
const moduleTableEntries = files.map(f => moduleSourceNode(f, createModulePath(f.fileName, program))); |

- Finally, this line. The function
moduleSourceNode contains code (with correct code) and sourceMapNode with child with incorrect code.
|
return joinSourceChunks([tableEntryHead, sourceMapNode ?? code, tableEntryTail]); |
Suggestion
I have replaced the line (p. 6) by return joinSourceChunks([tableEntryHead, code, tableEntryTail]); and it works.
Moreover, here specified, that code always exists. So, what about to put code instead of sourceMapNode ?? code?
|
interface BaseFile { |
|
code: string; |
|
sourceMap?: string; |
|
sourceFiles?: ts.SourceFile[]; |
|
} |
Description
Enabling bundle-mode (
luaBundleandluaBundleEntry) withsourceMapTraceback) will cause to appear{#SourceMapTraceback}in the result file.Steps to reproduce
{ "typescript": "^4.4.2", "typescript-to-lua": "^1.0.0" }tsconfig.jsonwith listed content:{ "$schema": "https://raw.githubusercontent.com/TypeScriptToLua/vscode-typescript-to-lua/master/tsconfig-schema.json", "compilerOptions": { "target": "es5", "module": "commonjs", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true }, "tstl": { "luaTarget": "JIT", "sourceMapTraceback": true, "luaBundleEntry": "src/index.ts", "luaBundle": "dist/index.lua" } }src/index.tsfiletstldist/index.lua. It looks like:Why does it happen
The "macro-string"
{#SourceMapTraceback}should be replaced here:TypeScriptToLua/src/LuaPrinter.ts
Lines 150 to 153 in 691c9cc
Debugging
I will try to debug the compiler and list the ideas below. I believe it will help to determine the problem,
printfunction from LuaPrinter.ts successfully replaces the macro-stringtranspiledFilesarrayTypeScriptToLua/src/transpilation/transpile.ts
Lines 62 to 74 in 691c9cc
getEmitPlanfunction). BecausefreshFilescontains correct code (with replaced macro-string)TypeScriptToLua/src/transpilation/transpiler.ts
Line 38 in 691c9cc
resolutionResult.resolvedFilescontains correct code,bundleFile-- does notTypeScriptToLua/src/transpilation/transpiler.ts
Line 72 in 691c9cc
moduleSourceNodefunction. Returns array of strings forlualib_bundleand array with incorrect SourceNode forindexTypeScriptToLua/src/transpilation/bundle.ts
Line 52 in 691c9cc
moduleSourceNodecontainscode(with correct code) andsourceMapNodewith child with incorrect code.TypeScriptToLua/src/transpilation/bundle.ts
Line 84 in 691c9cc
Suggestion
I have replaced the line (p. 6) by
return joinSourceChunks([tableEntryHead, code, tableEntryTail]);and it works.Moreover, here specified, that code always exists. So, what about to put
codeinstead ofsourceMapNode ?? code?TypeScriptToLua/src/transpilation/utils.ts
Lines 17 to 21 in 691c9cc