Skip to content

Commit 4164d68

Browse files
- included compiler options and emit host in module resolution plugin
interface - removed try catch on plugin execution
1 parent 37187f4 commit 4164d68

2 files changed

Lines changed: 27 additions & 29 deletions

File tree

src/transpilation/plugins.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ export interface Plugin {
4646
result: EmitFile[]
4747
) => ts.Diagnostic[] | void;
4848

49-
moduleResolution?: (moduleIdentifier : string, requiringFile : string) => string | undefined
49+
moduleResolution?: (
50+
moduleIdentifier: string,
51+
requiringFile: string,
52+
options: CompilerOptions,
53+
emitHost: EmitHost
54+
) => string | undefined
5055
}
5156

5257
export function getPlugins(program: ts.Program): { diagnostics: ts.Diagnostic[]; plugins: Plugin[] } {

src/transpilation/resolve.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,38 +96,31 @@ class ResolutionContext {
9696
}
9797

9898
private resolveDependencyPathsWithPlugins(required: ProcessedFile, dependency: string) {
99-
if (this.plugins != null) {
100-
const requiredFromLuaFile = required.fileName.endsWith(".lua");
101-
const dependencyPath = requiredFromLuaFile ? luaRequireToPath(dependency) : dependency;
102-
103-
for (const p of this.plugins) {
104-
try {
105-
if (p.moduleResolution != null) {
106-
const pluginResolvedPath = p.moduleResolution(dependency, dependencyPath)
107-
if (pluginResolvedPath !== undefined) {
108-
109-
// If lua file is in node_module
110-
if (requiredFromLuaFile && isNodeModulesFile(required.fileName)) {
111-
// If requiring file is in lua module, try to resolve sibling in that file first
112-
const resolvedNodeModulesFile = this.resolveLuaDependencyPathFromNodeModules(required, pluginResolvedPath);
113-
if (resolvedNodeModulesFile) {
114-
if (this.options.tstlVerbose) {
115-
console.log(`Resolved file path for module ${dependency} to path ${dependencyPath} using plugin.`)
116-
}
117-
return resolvedNodeModulesFile
118-
}
119-
}
120-
121-
if (this.getFileFromPath(pluginResolvedPath)) {
99+
const requiredFromLuaFile = required.fileName.endsWith(".lua");
100+
const dependencyPath = requiredFromLuaFile ? luaRequireToPath(dependency) : dependency;
101+
102+
for (const p of this.plugins) {
103+
if (p.moduleResolution != null) {
104+
const pluginResolvedPath = p.moduleResolution(dependency, dependencyPath, this.options, this.emitHost)
105+
if (pluginResolvedPath !== undefined) {
106+
107+
// If lua file is in node_module
108+
if (requiredFromLuaFile && isNodeModulesFile(required.fileName)) {
109+
// If requiring file is in lua module, try to resolve sibling in that file first
110+
const resolvedNodeModulesFile = this.resolveLuaDependencyPathFromNodeModules(required, pluginResolvedPath);
111+
if (resolvedNodeModulesFile) {
112+
if (this.options.tstlVerbose) {
122113
console.log(`Resolved file path for module ${dependency} to path ${dependencyPath} using plugin.`)
123-
return pluginResolvedPath;
124114
}
115+
return resolvedNodeModulesFile
125116
}
126117
}
127-
} catch (e: any) {
128-
// if plugin throws an error
129-
if (this.options.tstlVerbose) {
130-
console.log(e.details ?? e);
118+
119+
if (this.getFileFromPath(pluginResolvedPath)) {
120+
if (this.options.tstlVerbose) {
121+
console.log(`Resolved file path for module ${dependency} to path ${dependencyPath} using plugin.`)
122+
}
123+
return pluginResolvedPath;
131124
}
132125
}
133126
}

0 commit comments

Comments
 (0)