chore: rename runtime Lib/Module.lua & Lib/Class.lua to lowercase lib/module.lua & lib/class.lua (v0.3.1)
This commit is contained in:
1 parent
b1a53d8660
commit
867b76bb4a
13 files changed
+42
-37
No files matched your search
@@ -4,6 +4,11 @@ All notable changes to `ts2lua-unreal` will be documented in this file.
|
||||
|
||||
The format is based on [Semantic Versioning](https://semver.org/).
|
||||
|
||||
## [0.3.1] - 2026-11-30
|
||||
|
||||
### Changed
|
||||
- 将运行时基础库目录与文件名统一改为全小写:`runtime/lib/module.lua` 与 `runtime/lib/class.lua`(默认输出目录 `runtimeDir` 从 `"Lib"` 调整为 `"lib"`,启动加载改为 `require("lib.module")` / `require("lib.class")`)。
|
||||
|
||||
## [0.3.0] - 2026-11-30
|
||||
|
||||
### Added
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
## 特性概览
|
||||
|
||||
- **自定义模块化与单文件运行时热更(`v0.3.0+`)**:内置 `Lib/Module.lua`(`_G.__MODULE__`)与 `Lib/Class.lua`(扁平全局函数 `_G.__TS__DefineClass`,零表查询开销)。项目内 TS 模块统一编译为 `__MODULE__.Import("...", __module)`、独立 `_ENV` 原地更新、下游 `UpdateImports()` 自动刷新、Barrel 再导出级联、`/** @module_data */` 跨热更状态保留及纯 TS 类原型复用;UnLua 内置脚本与外部原生 Lua/C 插件(如 `const cjson = require("cjson")`)保持原生 `require` 不变,零入侵 UnLua。
|
||||
- **自定义模块化与单文件运行时热更(`v0.3.0+`)**:内置 `lib/module.lua`(`_G.__MODULE__`)与 `lib/class.lua`(扁平全局函数 `_G.__TS__DefineClass`,零表查询开销)。项目内 TS 模块统一编译为 `__MODULE__.Import("...", __module)`、独立 `_ENV` 原地更新、下游 `UpdateImports()` 自动刷新、Barrel 再导出级联、`/** @module_data */` 跨热更状态保留及纯 TS 类原型复用;UnLua 内置脚本与外部原生 Lua/C 插件(如 `const cjson = require("cjson")`)保持原生 `require` 不变,零入侵 UnLua。
|
||||
- **原生 UnLua 代码生成**:通过全局免 `import` 编译期装饰器 `@unlua` 标记绑定类,直接输出 `local M = UnLua.Class(...)`、`function M.Method(self, ...)` 与 `return M`,零 `__TS__Class` / `__TS__Decorate` / `____exports` 垫片污染。
|
||||
- **精确的 `super` 映射**:
|
||||
- 继承 UE C++/蓝图原生基类 -> `self.Overridden.Method(self, ...)`
|
||||
|
||||
@@ -29,8 +29,8 @@ function printInfo() {
|
||||
unluaTypes: path.join(pkgRoot, "types", "unlua.d.ts"),
|
||||
moduleTypes: path.join(pkgRoot, "types", "module.d.ts"),
|
||||
runtime: {
|
||||
module: path.join(pkgRoot, "runtime", "Lib", "Module.lua"),
|
||||
class: path.join(pkgRoot, "runtime", "Lib", "Class.lua"),
|
||||
module: path.join(pkgRoot, "runtime", "lib", "module.lua"),
|
||||
class: path.join(pkgRoot, "runtime", "lib", "class.lua"),
|
||||
},
|
||||
docs: {
|
||||
integrationGuide: path.join(pkgRoot, "docs", "integration-guide.md"),
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
- **禁用对 `UObject` 使用 `instanceof` 或对象展开 `{ ...obj }`** -> 类型判断用 `obj.IsA(UE.ClassName.StaticClass())`。
|
||||
|
||||
## 4. 自定义模块化、纯逻辑模块、容器与性能约定
|
||||
- 项目内 TS 模块默认编译为 `__MODULE__.Import` 单文件热更体系(基础库位于 `<outDir>/Lib/Module.lua` 与 `<outDir>/Lib/Class.lua`,启动时用原生 `require("Lib.Module"); require("Lib.Class")` 加载)。
|
||||
- 项目内 TS 模块默认编译为 `__MODULE__.Import` 单文件热更体系(基础库位于 `<outDir>/lib/module.lua` 与 `<outDir>/lib/class.lua`,启动时用原生 `require("lib.module"); require("lib.class")` 加载)。
|
||||
- UnLua 自身脚本与外部 Lua/C 原生插件在 TS 中继续使用原生 `require("cjson")` / `require("UnLua.*")`,不纳入 `__MODULE__` 管理。
|
||||
- 需跨单文件热更保留的模块顶层状态必须显式标注 `/** @module_data */`(仅允许模块顶层独立变量,禁止解构);模块生命周期回调使用 `__module.OnImport` / `__module.OnReimport` / `__module.OnDepModuleReimported`。
|
||||
- 纯函数工具模块文件顶部加 `/** @noSelfInFile */`,编译为点号调用(如 `Log.Info(...)`)。
|
||||
|
||||
@@ -158,13 +158,13 @@ export default class WBP_TempPopup {
|
||||
|
||||
### 3.4 自定义模块化与单文件热更(`v0.3.0+` 默认启用)
|
||||
|
||||
`ts2lua-unreal` 默认启用 `moduleSystem: "hot-reload"`,会在编译输出目录自动生成 `Lib/Module.lua` 与 `Lib/Class.lua`(零入侵 UnLua,不劫持 `_G.require`)。
|
||||
`ts2lua-unreal` 默认启用 `moduleSystem: "hot-reload"`,会在编译输出目录自动生成 `lib/module.lua` 与 `lib/class.lua`(零入侵 UnLua,不劫持 `_G.require`)。
|
||||
|
||||
**启动引导(`Boot.lua` 或 UE 启动入口)**:
|
||||
```lua
|
||||
-- 启动期以原生 require 加载基础库(不热更)
|
||||
require("Lib.Module")
|
||||
require("Lib.Class")
|
||||
require("lib.module")
|
||||
require("lib.class")
|
||||
-- 进入业务 TS 模块体系
|
||||
__MODULE__.Import("Main").Startup()
|
||||
```
|
||||
@@ -172,7 +172,7 @@ __MODULE__.Import("Main").Startup()
|
||||
**`require` 与 `__MODULE__.Import` 边界**:
|
||||
- UnLua 自身代码、外部原生 Lua/C 插件:TS 中直接写 `const cjson = require("cjson")` / `require("UnLua.HotReload")`,编译后原样保留 `require(...)`(且不会触发 TSTL 100000 报错);
|
||||
- 项目内业务/框架 TS 模块之间的 `import` / `export ... from`:自动编译为 `__MODULE__.Import("...", __module)` 并生成 `__module.UpdateImports()`;
|
||||
- 推荐在 UE C++ 层仅保留少量薄桥接类,通过回调分发至纯 TS 模块与纯 TS 类(`Lib/Class.lua` 的 `__TS__DefineClass` 自动复用类原型表),实现全业务无痛单文件热更。
|
||||
- 推荐在 UE C++ 层仅保留少量薄桥接类,通过回调分发至纯 TS 模块与纯 TS 类(`lib/class.lua` 的 `__TS__DefineClass` 自动复用类原型表),实现全业务无痛单文件热更。
|
||||
|
||||
**跨热更保留状态(`@module_data`)与生命周期钩子**:
|
||||
```ts
|
||||
@@ -201,7 +201,7 @@ const [ok, err] = __MODULE__.ReimportSafely("Framework.Counter");
|
||||
```
|
||||
|
||||
> 如需切回传统 TSTL `require` 产物或关闭运行时自动输出,可在 `tsconfig.json` 的 `tstl.luaPlugins` 中传入:
|
||||
> `{ "name": "ts2lua-unreal", "moduleSystem": "standard" }` 或 `{ "name": "ts2lua-unreal", "emitRuntime": false, "runtimeDir": "Lib" }`。
|
||||
> `{ "name": "ts2lua-unreal", "moduleSystem": "standard" }` 或 `{ "name": "ts2lua-unreal", "emitRuntime": false, "runtimeDir": "lib" }`。
|
||||
|
||||
### 3.5 纯逻辑工具模块(非 `UObject`)
|
||||
文件顶部加 `/** @noSelfInFile */`,编译为点号调用模块(如 `Log.Info(...)`、`EventBus.On(...)`):
|
||||
|
||||
@@ -270,7 +270,7 @@ StartCoroutine(this, () => {
|
||||
```
|
||||
|
||||
### 5.6 自定义模块化与单文件热重载(Hot Reload)规范
|
||||
- **基础运行时**:`Lib/Module.lua`(`_G.__MODULE__`)与 `Lib/Class.lua`(扁平全局函数 `_G.__TS__DefineClass`)由编译插件自动输出,启动时通过原生 `require("Lib.Module")` / `require("Lib.Class")` 初始化,不劫持 `_G.require`、不入侵 UnLua 自身脚本。
|
||||
- **基础运行时**:`lib/module.lua`(`_G.__MODULE__`)与 `lib/class.lua`(扁平全局函数 `_G.__TS__DefineClass`)由编译插件自动输出,启动时通过原生 `require("lib.module")` / `require("lib.class")` 初始化,不劫持 `_G.require`、不入侵 UnLua 自身脚本。
|
||||
- **依赖导入边界**:项目内 TS 模块之间一律使用标准 ES `import`(编译为 `__MODULE__.Import("...", __module)` + `__module.UpdateImports()`);外部 Lua/C 插件与 UnLua 内置模块继续使用原生 `require("cjson")` / `require("UnLua.*")`。
|
||||
- **模块状态保留**:需要跨单文件热更保留的顶层变量必须标注 `/** @module_data */`(仅支持模块顶层独立标识符,禁用解构);未标注的顶层变量在热更时重置。
|
||||
- **类原型复用**:顶层纯 TS 类自动通过扁平全局函数 `__TS__DefineClass("Name", __module)` 定义,热更时原地复用 `cls` 与 `cls.prototype` 并清理已删除方法,保证既有实例与子类继承链无缝生效。
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ts2lua-unreal",
|
||||
"version": "0.3.0",
|
||||
"version": "0.3.1",
|
||||
"description": "TypeScript to Lua toolchain and compiler plugin tailored for Unreal Engine + UnLua",
|
||||
"main": "dist/plugin/index.js",
|
||||
"types": "dist/plugin/index.d.ts",
|
||||
@@ -15,8 +15,8 @@
|
||||
"./tsconfig.base.json": "./tsconfig.base.json",
|
||||
"./types/unlua.d.ts": "./types/unlua.d.ts",
|
||||
"./types/module.d.ts": "./types/module.d.ts",
|
||||
"./runtime/Lib/Module.lua": "./runtime/Lib/Module.lua",
|
||||
"./runtime/Lib/Class.lua": "./runtime/Lib/Class.lua",
|
||||
"./runtime/lib/module.lua": "./runtime/lib/module.lua",
|
||||
"./runtime/lib/class.lua": "./runtime/lib/class.lua",
|
||||
"./docs/ts-unlua-spec.md": "./docs/ts-unlua-spec.md",
|
||||
"./docs/integration-guide.md": "./docs/integration-guide.md",
|
||||
"./docs/ai-rules-template.md": "./docs/ai-rules-template.md",
|
||||
|
||||
File renamed without changes.
File renamed without changes.
+5
-5
@@ -32,26 +32,26 @@ import {
|
||||
export interface UnLuaPluginOptions {
|
||||
/**
|
||||
* 模块化编译模式:
|
||||
* - `"hot-reload"`(默认):基于 `Lib/Module.lua` + `Lib/Class.lua` 生成支持单文件热更的 `__MODULE__.Import` / `__TS__DefineClass` 产物;
|
||||
* - `"hot-reload"`(默认):基于 `lib/module.lua` + `lib/class.lua` 生成支持单文件热更的 `__MODULE__.Import` / `__TS__DefineClass` 产物;
|
||||
* - `"standard"`:沿用 TSTL 标准 `require` 与 `local ____exports = {}` 产物。
|
||||
*/
|
||||
moduleSystem?: "hot-reload" | "standard";
|
||||
/** 是否在 `beforeEmit` 自动产出 `Lib/Module.lua` 与 `Lib/Class.lua`(默认 `true`,仅 `hot-reload` 模式生效)。 */
|
||||
/** 是否在 `beforeEmit` 自动产出 `lib/module.lua` 与 `lib/class.lua`(默认 `true`,仅 `hot-reload` 模式生效)。 */
|
||||
emitRuntime?: boolean;
|
||||
/** 运行时基础库输出相对目录(默认 `"Lib"`)。 */
|
||||
/** 运行时基础库输出相对目录(默认 `"lib"`)。 */
|
||||
runtimeDir?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* TypeScriptToLua (TSTL) UnLua 定制插件:
|
||||
* 1. 编译期强制执行 ts2lua-unreal 语言子集约束(禁用 get/set、async/await、try/catch 等);
|
||||
* 2. 支持自定义模块化单文件运行时热更(`Lib/Module.lua` + `Lib/Class.lua` + `@module_data`);
|
||||
* 2. 支持自定义模块化单文件运行时热更(`lib/module.lua` + `lib/class.lua` + `@module_data`);
|
||||
* 3. 将全局 `@unlua` 标记的绑定类转译为原生 UnLua 风格(UnLua.Class / return M / self.Overridden)。
|
||||
*/
|
||||
export function createUnLuaPlugin(options?: UnLuaPluginOptions): tstl.Plugin {
|
||||
const moduleSystem = options?.moduleSystem ?? "hot-reload";
|
||||
const emitRuntime = options?.emitRuntime ?? true;
|
||||
const runtimeDir = options?.runtimeDir ?? "Lib";
|
||||
const runtimeDir = options?.runtimeDir ?? "lib";
|
||||
const isHotReload = moduleSystem === "hot-reload";
|
||||
|
||||
const simpleValidators = createSimpleValidationVisitors();
|
||||
|
||||
@@ -57,17 +57,17 @@ const fileModuleStates = new WeakMap<ts.SourceFile, FileModuleState>();
|
||||
let cachedRuntimeModuleLua: string | undefined;
|
||||
let cachedRuntimeClassLua: string | undefined;
|
||||
|
||||
function getRuntimeFileContent(fileName: "Module.lua" | "Class.lua"): string {
|
||||
if (fileName === "Module.lua" && cachedRuntimeModuleLua !== undefined) {
|
||||
function getRuntimeFileContent(fileName: "module.lua" | "class.lua"): string {
|
||||
if (fileName === "module.lua" && cachedRuntimeModuleLua !== undefined) {
|
||||
return cachedRuntimeModuleLua;
|
||||
}
|
||||
if (fileName === "Class.lua" && cachedRuntimeClassLua !== undefined) {
|
||||
if (fileName === "class.lua" && cachedRuntimeClassLua !== undefined) {
|
||||
return cachedRuntimeClassLua;
|
||||
}
|
||||
// dist/plugin/module-transform.js -> ../../runtime/Lib/<fileName>
|
||||
const filePath = path.resolve(__dirname, "..", "..", "runtime", "Lib", fileName);
|
||||
// dist/plugin/module-transform.js -> ../../runtime/lib/<fileName>
|
||||
const filePath = path.resolve(__dirname, "..", "..", "runtime", "lib", fileName);
|
||||
const content = fs.readFileSync(filePath, "utf8");
|
||||
if (fileName === "Module.lua") {
|
||||
if (fileName === "module.lua") {
|
||||
cachedRuntimeModuleLua = content;
|
||||
} else {
|
||||
cachedRuntimeClassLua = content;
|
||||
@@ -875,7 +875,7 @@ export function transformHotReloadSourceFile(
|
||||
}
|
||||
|
||||
/**
|
||||
* 在 `beforeEmit` 阶段自动向编译产物目录输出 `Lib/Module.lua` 与 `Lib/Class.lua`(不覆盖已有同名产物)。
|
||||
* 在 `beforeEmit` 阶段自动向编译产物目录输出 `lib/module.lua` 与 `lib/class.lua`(不覆盖已有同名产物)。
|
||||
*/
|
||||
export function emitModuleRuntimeFiles(
|
||||
_program: ts.Program,
|
||||
@@ -888,7 +888,7 @@ export function emitModuleRuntimeFiles(
|
||||
result.map((f) => path.normalize(f.outputPath).replace(/\\/g, "/"))
|
||||
);
|
||||
|
||||
const filesToEmit: Array<"Module.lua" | "Class.lua"> = ["Module.lua", "Class.lua"];
|
||||
const filesToEmit: Array<"module.lua" | "class.lua"> = ["module.lua", "class.lua"];
|
||||
for (const fileName of filesToEmit) {
|
||||
const outputPath = path.join(outDir, runtimeDir, fileName);
|
||||
const normalized = path.normalize(outputPath).replace(/\\/g, "/");
|
||||
|
||||
+10
-10
@@ -84,8 +84,8 @@ function runLuaInDir(dir, script) {
|
||||
package.path = ${JSON.stringify(path.join(dir, "?.lua"))} .. ";" .. ${JSON.stringify(
|
||||
path.join(dir, "?/init.lua")
|
||||
)} .. ";" .. package.path
|
||||
require("Lib.Module")
|
||||
require("Lib.Class")
|
||||
require("lib.module")
|
||||
require("lib.class")
|
||||
${script}
|
||||
`;
|
||||
const proc = spawnSync(luaBin, ["-e", wrapped], { encoding: "utf8", cwd: dir });
|
||||
@@ -106,7 +106,7 @@ function withTempDir(fn) {
|
||||
// 1. 编译期产物与开关校验
|
||||
// ============================================================
|
||||
|
||||
test("hot-reload mode (default) auto-emits Lib/Module.lua and Lib/Class.lua and transforms imports/exports/classes", () => {
|
||||
test("hot-reload mode (default) auto-emits lib/module.lua and lib/class.lua and transforms imports/exports/classes", () => {
|
||||
const result = transpile({
|
||||
"Framework/Log.ts": `
|
||||
/** @noSelfInFile */
|
||||
@@ -146,9 +146,9 @@ test("hot-reload mode (default) auto-emits Lib/Module.lua and Lib/Class.lua and
|
||||
|
||||
assert.equal(getErrors(result).length, 0, formatDiagnostics(getErrors(result)));
|
||||
|
||||
// 自动产出 Lib/Module.lua 与 Lib/Class.lua
|
||||
assert.ok(getLuaFile(result, "Lib/Module.lua"), "Expected Lib/Module.lua to be emitted");
|
||||
assert.ok(getLuaFile(result, "Lib/Class.lua"), "Expected Lib/Class.lua to be emitted");
|
||||
// 自动产出 lib/module.lua 与 lib/class.lua
|
||||
assert.ok(getLuaFile(result, "lib/module.lua"), "Expected lib/module.lua to be emitted");
|
||||
assert.ok(getLuaFile(result, "lib/class.lua"), "Expected lib/class.lua to be emitted");
|
||||
|
||||
const workerLua = getLuaFile(result, "Logic/Worker.lua").lua;
|
||||
assert.match(workerLua, /local ____exports = __module\.env/);
|
||||
@@ -173,8 +173,8 @@ test("plugin options support emitRuntime: false and moduleSystem: 'standard'", (
|
||||
{ emitRuntime: false }
|
||||
);
|
||||
assert.equal(getErrors(noRuntime).length, 0);
|
||||
assert.equal(getLuaFile(noRuntime, "Lib/Module.lua"), undefined);
|
||||
assert.equal(getLuaFile(noRuntime, "Lib/Class.lua"), undefined);
|
||||
assert.equal(getLuaFile(noRuntime, "lib/module.lua"), undefined);
|
||||
assert.equal(getLuaFile(noRuntime, "lib/class.lua"), undefined);
|
||||
|
||||
const standard = transpile(
|
||||
{
|
||||
@@ -184,7 +184,7 @@ test("plugin options support emitRuntime: false and moduleSystem: 'standard'", (
|
||||
{ moduleSystem: "standard" }
|
||||
);
|
||||
assert.equal(getErrors(standard).length, 0);
|
||||
assert.equal(getLuaFile(standard, "Lib/Module.lua"), undefined);
|
||||
assert.equal(getLuaFile(standard, "lib/module.lua"), undefined);
|
||||
const bLua = getLuaFile(standard, "B.lua").lua;
|
||||
assert.match(bLua, /local ____exports = \{\}/);
|
||||
assert.match(bLua, /require\("A"\)/);
|
||||
@@ -384,7 +384,7 @@ test("Lua 5.4 runtime: @module_data preserves local/exported/nil values and keep
|
||||
});
|
||||
});
|
||||
|
||||
test("Lua 5.4 runtime: Lib/Class.lua hot-reloads pure TS base & child classes preserving instances, inheritance, instanceof, and cleaning removed methods", () => {
|
||||
test("Lua 5.4 runtime: lib/class.lua hot-reloads pure TS base & child classes preserving instances, inheritance, instanceof, and cleaning removed methods", () => {
|
||||
withTempDir((dir) => {
|
||||
const v1 = transpile({
|
||||
"Logic/Animal.ts": `
|
||||
|
||||
Vendored
+1
-1
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* ts2lua-unreal 自定义模块化与单文件运行时热更系统类型声明。
|
||||
* 对应运行时基础库:`Lib/Module.lua`(_G.__MODULE__)与 `Lib/Class.lua`(_G.__TS__DefineClass)。
|
||||
* 对应运行时基础库:`lib/module.lua`(_G.__MODULE__)与 `lib/class.lua`(_G.__TS__DefineClass)。
|
||||
*/
|
||||
|
||||
declare namespace __MODULE__ {
|
||||
|
||||
Reference in new issue
Block a user