Skip to content

Add sourcemap generation to JavaScript codegen target #3675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
004f9a8
Basic support for generating SourceMaps on Typescript target
AlisCode Sep 29, 2024
ef807f0
Produce Sourcemap for case
AlisCode Sep 29, 2024
af87895
Add sourcemap for todo and panic instructions
AlisCode Sep 29, 2024
dd6f895
Add sourcemap support for type declaration
AlisCode Sep 29, 2024
bb2c476
Support sourcemap for assert
AlisCode Sep 29, 2024
067a3ec
Attach sourcemap location to more expressions
AlisCode Sep 30, 2024
f8e7bc2
Attach sourcemap location for inputs
AlisCode Sep 30, 2024
2f78884
Cleanup sourcemap generation by not generating sourcemaps for ALL ass…
AlisCode Oct 2, 2024
2797e29
Attach sourcemap location for pipeline and more expressions
AlisCode Oct 4, 2024
ca38425
Attach more sourcemap location
AlisCode Oct 4, 2024
14e31ca
Attach sourcemap location to tail-call-optimized functions
AlisCode Oct 4, 2024
667d11a
Attach sourcemap location to external functions
AlisCode Oct 4, 2024
fb8d9b1
Simplify sourcemap attachment process and harmonize usage across the …
AlisCode Oct 4, 2024
67f9175
Setup test infra for sourcemaps
AlisCode Oct 5, 2024
ceb4407
Cleanup implementation details to prepare for a PR
AlisCode Oct 6, 2024
8575228
Rename sourcemap-generation functionality sourcemap->sourcemaps
AlisCode Oct 7, 2024
55b8d10
Correct minor comments
AlisCode Oct 7, 2024
60b9974
Reduce the size of the SourceMapEmitter by boxing the SourceMapBuilder
AlisCode Oct 6, 2024
415afd9
Remove SourceMap information from formatting API
AlisCode Oct 9, 2024
73fa02d
Move sourcemap content generation to the sourcemap module, document t…
AlisCode Oct 15, 2024
d4d079b
Reuse LineNumbers inside CursorPositionWriter
AlisCode Oct 24, 2024
56d12a1
Move sourcemap declaration at the top
AlisCode Oct 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 137 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler-cli/src/compile_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn command(options: CompilePackage) -> Result<()> {
Target::Erlang => TargetCodegenConfiguration::Erlang { app_file: None },
Target::JavaScript => TargetCodegenConfiguration::JavaScript {
emit_typescript_definitions: false,
emit_source_map: false,
prelude_location: options
.javascript_prelude
.ok_or_else(|| Error::JavaScriptPreludeRequired)?,
Expand Down
1 change: 1 addition & 0 deletions compiler-cli/src/dependencies/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ fn package_config(
unstable: true,
location: None,
},
sourcemaps: false,
},
target: Target::Erlang,
internal_modules: None,
Expand Down
3 changes: 1 addition & 2 deletions compiler-cli/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use gleam_core::{
error::{Error, FileIoAction, FileKind, Result, StandardIoAction, Unformatted},
io::Content,
io::OutputFile,
io::{Content, OutputFile},
};
use std::{io::Read, str::FromStr};

Expand Down
2 changes: 2 additions & 0 deletions compiler-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ num-bigint = "0.4.6"
num-traits = "0.2.19"
# Encryption
age = { version = "0.11", features = ["armor"] }
# Sourcemap generation
sourcemap = "9"

async-trait.workspace = true
base16.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions compiler-core/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::ast::{
CallArg, CustomType, DefinitionLocation, Pattern, TypeAst, TypedArg, TypedDefinition,
TypedExpr, TypedFunction, TypedPattern, TypedStatement,
};
use crate::sourcemap::SourceMapEmitter;
use crate::type_::Type;
use crate::{
ast::{Definition, SrcSpan, TypedModule},
Expand Down Expand Up @@ -142,6 +143,7 @@ impl Default for Runtime {
pub enum TargetCodegenConfiguration {
JavaScript {
emit_typescript_definitions: bool,
emit_source_map: bool,
prelude_location: Utf8PathBuf,
},
Erlang {
Expand Down
19 changes: 17 additions & 2 deletions compiler-core/src/build/package_compiler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::analyse::{ModuleAnalyzerConstructor, TargetSupport};
use crate::line_numbers::{self, LineNumbers};
use crate::sourcemap::{SourceMapEmitter, SourceMapSupport};
use crate::type_::PRELUDE_MODULE_NAME;
use crate::{
ast::{SrcSpan, TypedModule, UntypedModule},
Expand Down Expand Up @@ -302,10 +303,12 @@ where
match self.target {
TargetCodegenConfiguration::JavaScript {
emit_typescript_definitions,
emit_source_map,
prelude_location,
} => self.perform_javascript_codegen(
modules,
*emit_typescript_definitions,
*emit_source_map,
prelude_location,
),
TargetCodegenConfiguration::Erlang { app_file } => {
Expand Down Expand Up @@ -365,6 +368,7 @@ where
&mut self,
modules: &[Module],
typescript: bool,
emit_source_map: bool,
prelude_location: &Utf8Path,
) -> Result<(), Error> {
let mut written = HashSet::new();
Expand All @@ -373,9 +377,20 @@ where
} else {
TypeScriptDeclarations::None
};
let sourcemap = if emit_source_map {
SourceMapSupport::Emit
} else {
SourceMapSupport::None
};

JavaScript::new(&self.out, typescript, prelude_location, self.target_support)
.render(&self.io, modules)?;
JavaScript::new(
&self.out,
typescript,
sourcemap,
prelude_location,
self.target_support,
)
.render(&self.io, modules)?;

if self.copy_native_files {
self.copy_project_native_files(&self.out, &mut written)?;
Expand Down
Loading