@@ -2,11 +2,13 @@ import * as path from "path";
22import { SourceNode } from "source-map" ;
33import * as ts from "typescript" ;
44import { couldNotFindBundleEntryPoint } from "./diagnostics" ;
5- import { resolveFromRootDir } from "./resolve" ;
65import { EmitHost , TranspiledFile } from "./Transpile" ;
76import { formatPathToLuaPath , trimExtension } from "./utils" ;
7+ import { escapeString } from "./TSHelper" ;
8+ import { CompilerOptions } from "./CompilerOptions" ;
89
9- const formatPath = ( path : string ) => formatPathToLuaPath ( trimExtension ( path ) ) ;
10+ const formatPath = ( path : string ) => escapeString ( formatPathToLuaPath ( trimExtension ( path ) ) ) ;
11+ const modulePath = ( baseDir : string , pathToResolve : string ) => formatPath ( path . relative ( baseDir , pathToResolve ) ) ;
1012
1113export function bundleTranspiledFiles (
1214 bundleFile : string ,
@@ -17,17 +19,29 @@ export function bundleTranspiledFiles(
1719) : [ ts . Diagnostic [ ] , TranspiledFile ] {
1820 const diagnostics : ts . Diagnostic [ ] = [ ] ;
1921
20- const resolvedEntryModule = resolveFromRootDir ( program , entryModule ) ;
21- if ( ! transpiledFiles . some ( f => resolveFromRootDir ( program , f . fileName ) === resolvedEntryModule ) ) {
22+ const options = program . getCompilerOptions ( ) as CompilerOptions ;
23+
24+ const projectRootDir = options . configFilePath
25+ ? path . dirname ( options . configFilePath )
26+ : emitHost . getCurrentDirectory ( ) ;
27+
28+ // Resolve project settings relative to project file.
29+ const resolvedEntryModule = path . resolve ( projectRootDir , entryModule ) ;
30+ const resolvedBundleFile = path . resolve ( projectRootDir , bundleFile ) ;
31+
32+ // Resolve source files relative to common source directory.
33+ const sourceRootDir = program . getCommonSourceDirectory ( ) ;
34+ if ( ! transpiledFiles . some ( f => path . resolve ( sourceRootDir , f . fileName ) === resolvedEntryModule ) ) {
2235 return [ [ couldNotFindBundleEntryPoint ( entryModule ) ] , { fileName : bundleFile } ] ;
2336 }
2437
25- // For each file: ["<file name>"] = function() <lua content> end,
26- const projectRootDir = program . getCommonSourceDirectory ( ) ;
27- const moduleTableEntries : SourceChunk [ ] = transpiledFiles . map ( f => moduleSourceNode ( f , projectRootDir ) ) ;
38+ // For each file: ["<module path>"] = function() <lua content> end,
39+ const moduleTableEntries : SourceChunk [ ] = transpiledFiles . map ( f =>
40+ moduleSourceNode ( f , modulePath ( sourceRootDir , f . fileName ) )
41+ ) ;
2842
2943 // If any of the modules contains a require for lualib_bundle, add it to the module table.
30- const lualibRequired = transpiledFiles . some ( f => f . lua && f . lua . match ( / r e q u i r e \ (" l u a l i b _ b u n d l e " \) / ) ) ;
44+ const lualibRequired = transpiledFiles . some ( f => f . lua && f . lua . includes ( ` require("lualib_bundle")` ) ) ;
3145 if ( lualibRequired ) {
3246 const lualibBundle = emitHost . readFile ( path . resolve ( __dirname , "../dist/lualib/lualib_bundle.lua" ) ) ;
3347 moduleTableEntries . push ( `["lualib_bundle"] = function() ${ lualibBundle } end,\n` ) ;
@@ -37,21 +51,30 @@ export function bundleTranspiledFiles(
3751 const moduleTable = createModuleTableNode ( moduleTableEntries ) ;
3852
3953 // Override `require` to read from ____modules table.
40- const requireOverride =
41- `local ____moduleCache = {}\n` +
42- `local ____originalRequire = require\n` +
43- `function require(file) if ____moduleCache[file] then return ____moduleCache[file] end\n` +
44- `if ____modules[file] then ____moduleCache[file] = ____modules[file](); return ____moduleCache[file] ` +
45- `else print("Could not find module '"..file.."' to require."); return ____originalRequire(file) end end\n` ;
46- const entryPoint = `return require("${ formatPath ( entryModule ) } ")\n` ;
54+ const requireOverride = `
55+ local ____moduleCache = {}
56+ local ____originalRequire = require
57+ function require(file)
58+ if ____moduleCache[file] then return ____moduleCache[file] end
59+ if ____modules[file] then
60+ ____moduleCache[file] = ____modules[file]()
61+ return ____moduleCache[file]
62+ else
63+ print("Could not find module '"..file.."' to require.")
64+ return ____originalRequire(file)
65+ end
66+ end\n` ;
67+
68+ // return require("<entry module path>")
69+ const entryPoint = `return require("${ modulePath ( sourceRootDir , resolvedEntryModule ) } ")\n` ;
4770
4871 const bundleNode = joinSourceChunks ( [ moduleTable , requireOverride , entryPoint ] ) ;
4972 const { code, map } = bundleNode . toStringWithSourceMap ( ) ;
5073
5174 return [
5275 diagnostics ,
5376 {
54- fileName : path . join ( program . getCommonSourceDirectory ( ) , bundleFile ) ,
77+ fileName : resolvedBundleFile ,
5578 lua : code ,
5679 sourceMap : map . toString ( ) ,
5780 sourceMapNode : moduleTable ,
@@ -61,9 +84,8 @@ export function bundleTranspiledFiles(
6184 ] ;
6285}
6386
64- function moduleSourceNode ( transpiledFile : TranspiledFile , projectRootDir : string ) : SourceNode {
65- const resolvedProjectPath = path . relative ( projectRootDir , transpiledFile . fileName ) ;
66- const tableEntryHead = `["${ formatPath ( resolvedProjectPath ) } "] = function() ` ;
87+ function moduleSourceNode ( transpiledFile : TranspiledFile , modulePath : string ) : SourceNode {
88+ const tableEntryHead = `["${ modulePath } "] = function() ` ;
6789 const tableEntryTail = `end,\n` ;
6890
6991 if ( transpiledFile . lua && transpiledFile . sourceMapNode ) {
0 commit comments