JavaScript Transpiler

DWScript features a powerful Pascal-to-JavaScript compiler (dwsJSCodeGen). It allows you to write client-side logic in strongly-typed Object Pascal and deploy it as optimized JavaScript.

Key Features

  • Strong Typing: Catch errors at compile time before generating JS.
  • Smart Linking: The SmartLinker analyzes your code and removes unused classes, methods, and types from the final output, significantly reducing bundle size.
  • Optimizations: Loops, conditionals, and expressions are optimized for JS execution.
  • Source Maps: Debug your Pascal code directly in the browser.

Code Generation Options

Option Description
cgoSmartLink Enables the Smart Linker. Unreachable code is stripped.
cgoObfuscate Minifies and obfuscates symbol names to protect intellectual property and reduce size.
cgoPrettify Generates readable, indented JavaScript (useful for debugging).

Example

// Pascal Source
type
  TPoint = record x, y : Integer; end;

function Add(a, b : Integer) : Integer;
begin
  Result := a + b;
end;

// If "Add" is called, it will be in the JS output.
// If "TPoint" is unused, it will be removed by SmartLinker.

Libraries

Standard libraries like System, Math, and Classes are partially mapped to JS equivalents, allowing for seamless code sharing between server (native) and client (JS).

On this page