Files
2025-04-06 20:11:44 +08:00

98 lines
2.2 KiB
Markdown

# rbxts-transformer-instances
Enable the use of instance constructors in roblox-ts, reducing boilerplate by letting you use familiar class-like syntax.
## Overview
This transformer automatically converts constructor-style instance creation (`new Frame()`) into the standard Roblox `Instance.new("Frame")` pattern at compile time.
## Features
- ✨ Cleaner syntax for creating instances
- 🔍 Full TypeScript type safety
- ⚡ Zero runtime overhead (transforms at compile-time)
- 🔄 Supports all creatable Roblox instance types
## Installation
```bash
npm install rbxts-transformer-instances
```
### Configure in your tsconfig.json
Add the transformer to your `tsconfig.json`:
```json
{
"compilerOptions": {
...
"plugins": [
{
"transform": "rbxts-transformer-instances",
}
]
},
"include": [..., "node_modules/rbxts-transformer-instances"]
}
```
## Usage
### Basic Usage
```ts
// Instead of this:
const part = new Instance("Part");
part.Material = Enum.Material.Neon;
part.Position = new Vector3(0, 10, 0);
// You can write this:
const part = new Part();
part.Material = Enum.Material.Neon;
part.Position = new Vector3(0, 10, 0);
```
## Examples
### Input (TypeScript):
```ts
// Creating a basic UI
const screenGui = new ScreenGui();
screenGui.Parent = game.GetService("Players").LocalPlayer!.WaitForChild("PlayerGui");
const frame = new Frame();
frame.Size = new UDim2(0, 200, 0, 200);
frame.Position = new UDim2(0.5, -100, 0.5, -100);
frame.BackgroundColor3 = Color3.fromRGB(45, 45, 45);
frame.Parent = screenGui;
const textLabel = new TextLabel();
textLabel.Size = new UDim2(1, 0, 0, 50);
textLabel.Text = "Hello, World!";
textLabel.Parent = frame;
```
### Output (Lua):
```lua
-- Creates standard Instance.new calls
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0, 200, 0, 200)
frame.Position = UDim2.new(0.5, -100, 0.5, -100)
frame.BackgroundColor3 = Color3.fromRGB(45, 45, 45)
frame.Parent = screenGui
local textLabel = Instance.new("TextLabel")
textLabel.Size = UDim2.new(1, 0, 0, 50)
textLabel.Text = "Hello, World!"
textLabel.Parent = frame
```
## License
MIT