fix(bundle): deduplicate symlinked dependencies in luaBundle output by ChouUn · Pull Request #1684 · TypeScriptToLua/TypeScriptToLua · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/transpilation/resolve.ts
45 changes: 45 additions & 0 deletions test/transpile/module-resolution.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from "path";
import * as fs from "fs";
import * as tstl from "../../src";
import * as util from "../util";
import * as ts from "typescript";
Expand Down Expand Up @@ -383,6 +384,50 @@ describe("dependency with complicated inner structure", () => {
});
});

describe("symlinked dependency deduplication", () => {
const projectPath = path.resolve(__dirname, "module-resolution", "project-with-symlinked-dependency");
const tsConfigPath = path.join(projectPath, "tsconfig.json");
const mainFilePath = path.join(projectPath, "main.ts");
const symlinkPath = path.join(projectPath, "node_modules", "shared-lib");

beforeAll(() => {
// Dynamically create symlink to avoid git symlink portability issues
Comment thread
ChouUn marked this conversation as resolved.
const symlinkDir = path.dirname(symlinkPath);
if (!fs.existsSync(symlinkDir)) {
fs.mkdirSync(symlinkDir, { recursive: true });
}
if (!fs.existsSync(symlinkPath)) {
fs.symlinkSync(path.join(projectPath, "shared-lib"), symlinkPath, "junction");
}
});

afterAll(() => {
if (fs.existsSync(symlinkPath) && fs.lstatSync(symlinkPath).isSymbolicLink()) {
fs.unlinkSync(symlinkPath);
}
});

test("bundle should not contain duplicate files from symlinked dependencies", () => {
const mainFile = path.join(projectPath, "main.ts");
const { transpiledFiles } = util
.testProject(tsConfigPath)
.setMainFileName(mainFilePath)
.setOptions({ luaBundle: "bundle.lua", luaBundleEntry: mainFile })
.expectToEqual({
directResult: "Hello, World",
indirectResult: "Hello, World, welcome!",
})
.getLuaResult();

expect(transpiledFiles).toHaveLength(1);
const lua = transpiledFiles[0].lua!;
// shared-lib is used by both main.ts (directly) and consumer (indirectly via symlink),
// but should only appear once in the bundle
const moduleEntries = (lua.match(/\["[^"]*shared-lib[^"]*"\]\s*=\s*function/g) ?? []).length;
expect(moduleEntries).toBe(1);
});
});

test("module resolution should not try to resolve @noResolution annotation", () => {
util.testModule`
import * as json from "json";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as sharedLib from "shared-lib";
import * as consumer from "consumer";

export const directResult = sharedLib.greet("World");
export const indirectResult = consumer.welcome("World");
Comment thread
ChouUn marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @noSelfInFile */
export declare function greet(name: string): string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
greet = function(name)
return "Hello, " .. name
end
}