Skip to content

Commit 691c9cc

Browse files
authored
Module resolution from node_modules (#1011)
* Create test project with node_modules * testProject test util * Fix typos in module resolution test * Base case module resolution * Replace requires in source maps too * Fixed incorrect path behavior * More tests * add module resolution with sourceDir node_modules * Get all module-resolution testcases to work * Restrict resolver to only lua files * All tests working * Added more in-project dependency checks to sourceDir test * Fixed problem with lua sibling files * Also resolve JSON modules * Add debug to resolution test to try to figure out why CI is failing * fix test runner path preloading * Revert "Add debug to resolution test to try to figure out why CI is failing" This reverts commit 7b8bd80. * Changed resolution failure from error to warning * move json.lua from dist to src in benchmark * Use commit version instead of master to compile benchmark scripts * Added module resolution test project with lua sources * Add library compilation mode * renamed compilemode to buildmode * clean up resolve * Fix tests * Removed old project test runner * PR comments * Remove file casing test * Resolution + library mode combined test * remove out path logic from printer * Fixed bundle entry point require not being resolved correctly * Add header to bundle * Made couldNotResolveRequire an error instead of warning * updated couldnotResolveRequire snapshot
1 parent 5abb60b commit 691c9cc

File tree

86 files changed

+1102
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1102
-197
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/test/cli/errors
44
/test/cli/watch
55
/test/transpile/directories
6+
/test/transpile/module-resolution/*/node_modules
67
/test/transpile/outFile

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,29 @@ jobs:
7676
run: rm -rf ./master/benchmark && cp -rf ./commit/benchmark ./master/benchmark
7777
# Run master benchmark first and output to commit benchmark data
7878
- name: Build benchmark Lua 5.3 master
79-
run: node ../dist/tstl.js -p tsconfig.53.json
79+
run: node ../../commit/dist/tstl.js -p tsconfig.53.json
8080
working-directory: master/benchmark
8181
- name: Run benchmark Lua 5.3 master
8282
id: benchmark-lua-master
8383
run: lua5.3 -- run.lua ../../../commit/benchmark/data/benchmark_master_53.json
8484
working-directory: master/benchmark/dist
8585
- name: Build benchmark LuaJIT master
86-
run: node ../dist/tstl.js -p tsconfig.jit.json
86+
run: node ../../commit/dist/tstl.js -p tsconfig.jit.json
8787
working-directory: master/benchmark
8888
- name: Run benchmark LuaJIT master
8989
id: benchmark-jit-master
9090
run: luajit -- run.lua ../../../commit/benchmark/data/benchmark_master_jit.json
9191
working-directory: master/benchmark/dist
9292
# Run commit benchmark and compare with master
9393
- name: Build benchmark Lua 5.3 commit
94-
run: node ../dist/tstl.js -p tsconfig.53.json
94+
run: node ../../commit/dist/tstl.js -p tsconfig.53.json
9595
working-directory: commit/benchmark
9696
- name: Run benchmark Lua 5.3 commit
9797
id: benchmark-lua-commit
9898
run: lua5.3 -- run.lua ../data/benchmark_master_vs_commit_53.json ../data/benchmark_master_53.json
9999
working-directory: commit/benchmark/dist
100100
- name: Build benchmark LuaJIT commit
101-
run: node ../dist/tstl.js -p tsconfig.jit.json
101+
run: node ../../commit/dist/tstl.js -p tsconfig.jit.json
102102
working-directory: commit/benchmark
103103
- name: Run benchmark LuaJIT commit
104104
id: benchmark-jit-commit

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug current jest test",
6+
"type": "node",
7+
"request": "launch",
8+
"env": { "CI": "true" },
9+
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest",
10+
"args": ["--runInBand", "--no-cache", "--runTestsByPath", "${relativeFile}"],
11+
"cwd": "${workspaceRoot}",
12+
"protocol": "inspector",
13+
"console": "integratedTerminal",
14+
"internalConsoleOptions": "neverOpen"
15+
}
16+
]
17+
}

package-lock.json

Lines changed: 37 additions & 9 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"node": ">=12.13.0"
4343
},
4444
"dependencies": {
45+
"enhanced-resolve": "^5.8.2",
4546
"resolve": "^1.15.1",
4647
"source-map": "^0.7.3",
4748
"typescript": "~4.3.2"

src/CompilerOptions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface LuaPluginImport {
2525
}
2626

2727
export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
28+
buildMode?: BuildMode;
2829
noImplicitSelf?: boolean;
2930
noHeader?: boolean;
3031
luaBundle?: string;
@@ -53,6 +54,11 @@ export enum LuaTarget {
5354
LuaJIT = "JIT",
5455
}
5556

57+
export enum BuildMode {
58+
Default = "default",
59+
Library = "library",
60+
}
61+
5662
export const isBundleEnabled = (options: CompilerOptions) =>
5763
options.luaBundle !== undefined && options.luaBundleEntry !== undefined;
5864

src/LuaPrinter.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import * as path from "path";
21
import { Mapping, SourceMapGenerator, SourceNode } from "source-map";
32
import * as ts from "typescript";
43
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
54
import * as lua from "./LuaAST";
65
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
76
import { isValidLuaIdentifier } from "./transformation/utils/safe-names";
87
import { EmitHost } from "./transpilation";
9-
import { intersperse, normalizeSlashes, trimExtension } from "./utils";
8+
import { intersperse, trimExtension } from "./utils";
109

1110
// https://www.lua.org/pil/2.4.html
1211
// https://www.ecma-international.org/ecma-262/10.0/index.html#table-34
@@ -25,6 +24,8 @@ const escapeStringMap: Record<string, string> = {
2524

2625
export const escapeString = (value: string) => `"${value.replace(escapeStringRegExp, char => escapeStringMap[char])}"`;
2726

27+
export const tstlHeader = "--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]\n";
28+
2829
/**
2930
* Checks that a name is valid for use in lua function declaration syntax:
3031
*
@@ -124,22 +125,7 @@ export class LuaPrinter {
124125

125126
constructor(private emitHost: EmitHost, program: ts.Program, fileName: string) {
126127
this.options = program.getCompilerOptions();
127-
128-
if (this.options.outDir) {
129-
const relativeFileName = path.relative(program.getCommonSourceDirectory(), fileName);
130-
if (this.options.sourceRoot) {
131-
// When sourceRoot is specified, just use relative path inside rootDir
132-
this.sourceFile = relativeFileName;
133-
} else {
134-
// Calculate relative path from rootDir to outDir
135-
const outputPath = path.resolve(this.options.outDir, relativeFileName);
136-
this.sourceFile = path.relative(path.dirname(outputPath), fileName);
137-
}
138-
// We want forward slashes, even in windows
139-
this.sourceFile = normalizeSlashes(this.sourceFile);
140-
} else {
141-
this.sourceFile = path.basename(fileName); // File will be in same dir as source
142-
}
128+
this.sourceFile = fileName;
143129
}
144130

145131
public print(file: lua.File): PrintResult {
@@ -201,7 +187,7 @@ export class LuaPrinter {
201187
let header = file.trivia;
202188

203189
if (!this.options.noHeader) {
204-
header += "--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]\n";
190+
header += tstlHeader;
205191
}
206192

207193
const luaLibImport = this.options.luaLibImport ?? LuaLibImportKind.Require;

src/cli/parse.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from "typescript";
2-
import { CompilerOptions, LuaLibImportKind, LuaTarget } from "../CompilerOptions";
2+
import { BuildMode, CompilerOptions, LuaLibImportKind, LuaTarget } from "../CompilerOptions";
33
import * as cliDiagnostics from "./diagnostics";
44

55
export interface ParsedCommandLine extends ts.ParsedCommandLine {
@@ -24,6 +24,12 @@ interface CommandLineOptionOfPrimitive extends CommandLineOptionBase {
2424
type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfPrimitive;
2525

2626
export const optionDeclarations: CommandLineOption[] = [
27+
{
28+
name: "buildMode",
29+
description: "'default' or 'library'. Compiling as library will not resolve external dependencies.",
30+
type: "enum",
31+
choices: Object.values(BuildMode),
32+
},
2733
{
2834
name: "luaBundle",
2935
description: "The name of the lua file to bundle output lua to. Requires luaBundleEntry.",

src/transformation/utils/diagnostics.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ export const invalidAmbientIdentifierName = createErrorDiagnosticFactory(
9494
(text: string) => `Invalid ambient identifier name '${text}'. Ambient identifiers must be valid lua identifiers.`
9595
);
9696

97-
export const unresolvableRequirePath = createErrorDiagnosticFactory(
98-
(path: string) => `Cannot create require path. Module '${path}' does not exist within --rootDir.`
99-
);
100-
10197
export const unsupportedVarDeclaration = createErrorDiagnosticFactory(
10298
"`var` declarations are not supported. Use `let` or `const` instead."
10399
);

0 commit comments

Comments
 (0)