Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • X
  • BlueSky
  • GitHub
  • Forum
Language Manual
Overview
  • Introduction
  • Installation
  • Editor Plugins
  • Migrate to ReScript Syntax
  • Try
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Variant
  • Polymorphic Variant
  • Null, Undefined and Option
  • Array & List
  • Function
  • If-Else & Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
  • JSX
  • Exception
  • Lazy Value
  • Async & Promise
  • Module
  • Import & Export
  • Attribute (Decorator)
  • Unboxed
  • Reserved Keyword
JavaScript Interop
  • Interop Cheatsheet
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Inlining Constants
    • Tips & Tricks
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Libraries & Publishing
Build System
  • Overview
  • Configuration
  • Configuration Schema
  • External Stdlib
  • Pinned Dependencies
  • Interop with JS Build Systems
  • Performance
Guides
  • Converting from JS
Extra
  • Newcomer Examples
  • Project Structure
  • FAQ
Docs / Language Manual / Inlining Constants
Edit

Inlining Constants

Sometime, in the JavaScript output, you might want a certain value to be forcefully inlined. For example:

JS
if (process.env.mode === 'development') { console.log("Dev-only code here!") }

The reason is that your JavaScript bundler (e.g. Webpack) might turn that into:

JS
if ('production' === 'development') { console.log("Dev-only code here!") }

Then your subsequent Uglifyjs optimization would remove that entire if block. This is how projects like ReactJS provide a development mode code with plenty of dev warnings, while ensuring that the uglified (minified) production code is free of those expensive blocks.

So, in ReScript, producing that example if (process.env.mode === 'development') output is important. This first try doesn't work:

ReScriptJS Output
@val external process: 'a = "process"

let mode = "development"

if (process["env"]["mode"] === mode) {
  Js.log("Dev-only code here!")
}

The JS output shows if (process.env.mode === mode), which isn't what we wanted. To inline mode's value, use @inline:

ReScriptJS Output
@val external process: 'a = "process"

@inline
let mode = "development"

if (process["env"]["mode"] === mode) {
  Js.log("Dev-only code here!")
}

Now your resulting JS code can pass through Webpack and Uglifyjs like the rest of your JavaScript code, and that whole console.log can be removed.

The inlining currently only works for string, float and boolean.

Tips & Tricks

This is not an optimization. This is an edge-case feature for folks who absolutely need particular values inlined for a JavaScript post-processing step, like conditional compilation. Beside the difference in code that the conditional compilation might end up outputting, there's no performance difference between inlining and not inlining simple values in the eyes of a JavaScript engine.

JSONUse Illegal Identifier Names

© 2024 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on