Skip to content

Commit 9a92671

Browse files
RFC: sourcemaps
1 parent 417dce0 commit 9a92671

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

0002-sourcemaps.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
Feature Name: Native Sourcemap Support for ReScript
3+
Start Date: 2025-02-19
4+
RFC PR: https://github.com/rescript-lang/rfcs/pull/2
5+
ReScript Issue: (leave this empty)
6+
---
7+
8+
## Summary
9+
10+
This RFC proposes adding native sourcemap support to the ReScript compiler. The goal is to generate sourcemap files during compilation, enabling developers to map the generated JavaScript back to ReScript source code. This feature will greatly enhance debugging capabilities and streamline the developer experience.
11+
12+
## Motivation
13+
14+
### 2.1. Debugging Efficiency
15+
16+
- **Problem:** Without sourcemaps, developers must debug minified or transformed JavaScript code, obscuring the original ReScript source.
17+
- **Benefit:** Sourcemaps enable direct debugging of the original ReScript code, reducing debugging time and improving overall developer productivity.
18+
19+
### 2.2. Developer Experience
20+
21+
- **Problem:** Debugging tools (e.g., Chrome DevTools, VS Code) rely on sourcemaps for accurate breakpoint placement and error reporting.
22+
- **Benefit:** Native sourcemap support improves integration with these tools, providing a seamless debugging experience.
23+
24+
### 2.3. Industry Alignment
25+
26+
- **Observation:** Many modern compilers (TypeScript, Babel, etc.) offer robust sourcemap support.
27+
- **Benefit:** Implementing sourcemaps in ReScript keeps the language competitive and aligned with current industry practices.
28+
29+
## Detailed design
30+
31+
### 3.1. Overview
32+
33+
The compiler will be extended to produce a JSON-based sourcemap file alongside the compiled JavaScript output. The sourcemap will adhere to the [Source Map](https://tc39.es/ecma426/) specification.
34+
35+
### 3.2. Compiler Changes
36+
37+
- **AST and Code Generation:**
38+
- Extend the compiler’s code generation phase to track source positions (line and column numbers) for each node.
39+
- Generate mappings between ReScript source files and the emitted JavaScript code.
40+
- **Output:**
41+
- For each compiled JavaScript file (e.g., `file.res.js`), produce a corresponding `file.res.js.map`.
42+
- Append a comment at the end of `file.res.js` to reference the sourcemap file:
43+
```js
44+
//# sourceMappingURL=file.js.map
45+
```
46+
47+
### 3.3. Configuration Options
48+
49+
Introduce new configuration options in the compiler’s configuration file (`rescript.json`):
50+
51+
- **`--sourcemap` flag:** Boolean flag to enable or disable sourcemap generation.
52+
- **Customization Options:**
53+
- Specify the output directory for sourcemaps.
54+
- Choose between inline sourcemaps (embedded in the JavaScript file) and external sourcemap files.
55+
56+
### 3.4. Handling Inline vs. External Sourcemaps
57+
58+
- **Inline Sourcemaps:**
59+
- **Definition:** Embed the sourcemap data as a base64-encoded string at the end of the JavaScript file.
60+
- **Pros:** Simplifies deployment by bundling everything in one file.
61+
- **Cons:** Increases file size.
62+
- **External Sourcemaps:**
63+
- **Definition:** Generate a separate `.map` file alongside the JavaScript file.
64+
- **Pros:** Keeps the main JavaScript file lean.
65+
- **Cons:** Requires managing multiple files.
66+
- **Default Configuration:** External sourcemaps, with an option for inline maps if desired by the user.
67+
68+
### 3.5. Error Handling & Edge Cases
69+
70+
- **Incomplete Mappings:** Ensure every segment of generated code has a corresponding mapping or a defined fallback.
71+
- **Minification:** Ensure compatibility with downstream minification tools by mapping back to the original ReScript code before minification.
72+
- **Non-linear Code Generation:** Accommodate non-sequential mapping entries due to ReScript-to-JS transformation complexities.
73+
74+
## Drawbacks
75+
76+
- **Increased Compilation Time:**
77+
- _Mitigation:_ Optimize the mapping generation process and offer a flag to disable sourcemaps for production builds.
78+
- **Compiler Complexity:**
79+
- _Mitigation:_ Keep the mapping logic modular and well-documented; encourage community code reviews and contributions.
80+
- **Maintenance Overhead:**
81+
- _Mitigation:_ Write comprehensive tests to prevent regressions and ensure long-term stability.
82+
83+
## Rationale and alternatives
84+
85+
- **External Tooling:**
86+
Rely on third-party tools to generate sourcemaps post-compilation.
87+
- **Drawback:** Less integration, higher risk of mismatches, and increased maintenance overhead.
88+
- **Partial Mapping:**
89+
Generate sourcemaps only for a subset of language features.
90+
- **Drawback:** Results in an inconsistent debugging experience and incomplete error mapping.
91+
92+
The chosen approach of native support within the compiler ensures a consistent and reliable debugging experience.
93+
94+
## Prior art
95+
96+
- TypeScript sourcemaps: https://www.typescriptlang.org/tsconfig/#sourceMap
97+
98+
## Adoption strategy
99+
100+
Ideally, people who need them will just enable this feature in `rescript.json` and/or via a compiler flag.
101+
102+
## Unresolved questions
103+
104+
- Find out what preparations are needed in the compiler (and build system/rewatch) that need to be landed before this can be implemented in a way which does not break on subsequent changes?
105+
- Should we introduce a production build mode / production build configuration?
106+
107+
## Future possibilities
108+
109+
Native support for tools like Sentry, Wallabyjs, etc.

0 commit comments

Comments
 (0)