Skip to content

Commit

Permalink
build: switch to commonjs for the transpile project; this allows use …
Browse files Browse the repository at this point in the history
…of export =
  • Loading branch information
YoRyan committed Oct 17, 2023
1 parent d01f1a6 commit 5b65402
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
20 changes: 12 additions & 8 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class WatchQueue {
}
private async buildFile(file: Path) {
const bundle = await readVirtualBundle();
await timedTranspile(file, bundle);
await timedTranspile(readCompilerOptions(), file, bundle);
}
}

Expand All @@ -88,7 +88,7 @@ async function build() {
const bundle = await readVirtualBundle();
const entryPoints = await globEntryPoints();
for (const entry of entryPoints) {
await timedTranspile(entry, bundle);
await timedTranspile(readCompilerOptions(), entry, bundle);
}
}

Expand Down Expand Up @@ -119,11 +119,16 @@ async function globEntryPoints() {
return await glob("mod/**/*.ts", { cwd: "./src", withFileTypes: true });
}

async function timedTranspile(entryFile: Path, virtualBundle: [string, string][]) {
function readCompilerOptions() {
const configJson = ts.readConfigFile("./src/tsconfig.json", ts.sys.readFile);
return ts.parseJsonConfigFileContent(configJson.config, ts.sys, ".").options;
}

async function timedTranspile(compilerOptions: ts.CompilerOptions, entryFile: Path, virtualBundle: [string, string][]) {
const startMs = nowTime();
let err = undefined;
try {
await transpile(entryFile, virtualBundle);
await transpile(compilerOptions, entryFile, virtualBundle);
} catch (e) {
err = e;
}
Expand All @@ -134,17 +139,16 @@ async function timedTranspile(entryFile: Path, virtualBundle: [string, string][]
console.log(`${entryPath} ${result}`);
}

async function transpile(entryFile: Path, virtualBundle: [string, string][]) {
async function transpile(compilerOptions: ts.CompilerOptions, entryFile: Path, virtualBundle: [string, string][]) {
// Create a virtual project that includes the entry point file.
const virtualProject = Object.fromEntries([await readVirtualFile(entryFile), ...virtualBundle]);

// Call TypeScriptToLua.
const bundleFile = (entryFile.parent ?? entryFile).resolve(path.basename(entryFile.name, ".ts") + ".lua");
const result = tstl.transpileVirtualProject(virtualProject, {
target: ts.ScriptTarget.ESNext,
...compilerOptions,
// Drop the jest types here.
types: ["lua-types/5.0", "@typescript-to-lua/language-extensions"],
baseUrl: ".",
strict: true,
luaTarget: tstl.LuaTarget.Lua50,
sourceMapTraceback: false,
luaBundle: bundleFile.relative(),
Expand Down
5 changes: 0 additions & 5 deletions jest.config.js

This file was deleted.

8 changes: 8 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Config } from "jest";

const config: Config = {
preset: "ts-jest",
testEnvironment: "node",
rootDir: "./src",
};
export default config;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"scripts": {
"lint": "prettier --check .",
"fix:prettier": "prettier --write .",
"test": "jest",
"watch": "ts-node-esm ./build.ts watch",
"build": "ts-node-esm ./build.ts"
},
Expand Down
11 changes: 11 additions & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"baseUrl": ".",
"strict": true,
"types": ["lua-types/5.0", "@typescript-to-lua/language-extensions", "jest"],
"module": "CommonJS",
"moduleResolution": "Node10",
"target": "ESNext"
},
"include": ["@types", "lib", "mod", "test"]
}
16 changes: 5 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// Note that this file is only used for editing in your IDE. It is not used by
// the build script.
{
"compilerOptions": {
"target": "esnext",
"lib": ["esnext"],
"moduleResolution": "node",
"baseUrl": "./src/",
"target": "ESNext",
"lib": ["ESNext"],
"strict": true,
"types": ["lua-types/5.0", "jest"],
// Needed for build.ts and ts-node.
"module": "ESNext",
"allowSyntheticDefaultImports": true
"module": "NodeNext",
"moduleResolution": "NodeNext"
},
"include": ["src"]
"include": ["build.ts"]
}

0 comments on commit 5b65402

Please sign in to comment.