diff --git a/compiler-core/src/build/package_compiler.rs b/compiler-core/src/build/package_compiler.rs index d57aa409b44..9e0147e45f8 100644 --- a/compiler-core/src/build/package_compiler.rs +++ b/compiler-core/src/build/package_compiler.rs @@ -374,8 +374,11 @@ where TypeScriptDeclarations::None }; - JavaScript::new(&self.out, typescript, prelude_location, self.target_support) - .render(&self.io, modules)?; + JavaScript::new(&self.out, typescript, prelude_location, self.target_support).render( + &self.io, + modules, + self.stdlib_is_a_dependency(), + )?; if self.copy_native_files { self.copy_project_native_files(&self.out, &mut written)?; @@ -427,6 +430,10 @@ where Ok(()) } + + fn stdlib_is_a_dependency(&self) -> bool { + self.config.dependencies.contains_key("gleam_stdlib") + } } fn analyse( diff --git a/compiler-core/src/codegen.rs b/compiler-core/src/codegen.rs index 5038ea1533b..e4f2ab5a6c9 100644 --- a/compiler-core/src/codegen.rs +++ b/compiler-core/src/codegen.rs @@ -178,13 +178,18 @@ impl<'a> JavaScript<'a> { } } - pub fn render(&self, writer: &impl FileSystemWriter, modules: &[Module]) -> Result<()> { + pub fn render( + &self, + writer: &impl FileSystemWriter, + modules: &[Module], + stdlib_is_a_dependency: bool, + ) -> Result<()> { for module in modules { let js_name = module.name.clone(); if self.typescript == TypeScriptDeclarations::Emit { self.ts_declaration(writer, module, &js_name)?; } - self.js_module(writer, module, &js_name)? + self.js_module(writer, module, &js_name, stdlib_is_a_dependency)? } self.write_prelude(writer)?; Ok(()) @@ -235,6 +240,7 @@ impl<'a> JavaScript<'a> { writer: &impl FileSystemWriter, module: &Module, js_name: &str, + stdblib_is_a_dependency: bool, ) -> Result<()> { let name = format!("{js_name}.mjs"); let path = self.output_directory.join(name); @@ -246,6 +252,7 @@ impl<'a> JavaScript<'a> { &module.code, self.target_support, self.typescript, + stdblib_is_a_dependency, ); tracing::debug!(name = ?js_name, "Generated js module"); writer.write(&path, &output?) diff --git a/compiler-core/src/javascript.rs b/compiler-core/src/javascript.rs index d1c24153e9c..f4dea5c36d7 100644 --- a/compiler-core/src/javascript.rs +++ b/compiler-core/src/javascript.rs @@ -45,6 +45,7 @@ pub struct Generator<'a> { current_module_name_segments_count: usize, target_support: TargetSupport, typescript: TypeScriptDeclarations, + stdlib_is_a_dependency: bool, } impl<'a> Generator<'a> { @@ -53,6 +54,7 @@ impl<'a> Generator<'a> { module: &'a TypedModule, target_support: TargetSupport, typescript: TypeScriptDeclarations, + stdlib_is_a_dependency: bool, ) -> Self { let current_module_name_segments_count = module.name.split('/').count(); @@ -64,6 +66,7 @@ impl<'a> Generator<'a> { module_scope: Default::default(), target_support, typescript, + stdlib_is_a_dependency, } } @@ -170,18 +173,18 @@ impl<'a> Generator<'a> { }; let echo = if self.tracker.echo_used { - // TODO! Check stdlib is among the dependencies or this will fail at - // runtime with an exception. - self.register_import( - &mut imports, - "gleam_stdlib", - "dict", - &Some(( - AssignName::Variable("stdlib$dict".into()), - SrcSpan::default(), - )), - &[], - ); + if self.stdlib_is_a_dependency { + self.register_import( + &mut imports, + "gleam_stdlib", + "dict", + &Some(( + AssignName::Variable("stdlib$dict".into()), + SrcSpan::default(), + )), + &[], + ); + } self.register_prelude_usage(&mut imports, "BitArray", Some("$BitArray")); self.register_prelude_usage(&mut imports, "List", Some("$List")); self.register_prelude_usage(&mut imports, "UtfCodepoint", Some("$UtfCodepoint")); @@ -591,14 +594,21 @@ pub fn module( src: &EcoString, target_support: TargetSupport, typescript: TypeScriptDeclarations, + stdlib_is_a_dependency: bool, ) -> Result { - let document = Generator::new(line_numbers, module, target_support, typescript) - .compile() - .map_err(|error| crate::Error::JavaScript { - path: path.to_path_buf(), - src: src.clone(), - error, - })?; + let document = Generator::new( + line_numbers, + module, + target_support, + typescript, + stdlib_is_a_dependency, + ) + .compile() + .map_err(|error| crate::Error::JavaScript { + path: path.to_path_buf(), + src: src.clone(), + error, + })?; Ok(document.to_pretty_string(80)) } diff --git a/compiler-core/src/javascript/tests.rs b/compiler-core/src/javascript/tests.rs index 0ebdeaf552c..1cf1dc38c88 100644 --- a/compiler-core/src/javascript/tests.rs +++ b/compiler-core/src/javascript/tests.rs @@ -178,6 +178,7 @@ pub fn compile(src: &str, deps: Vec<(&str, &str, &str)>) -> TypedModule { pub fn compile_js(src: &str, deps: Vec<(&str, &str, &str)>) -> Result { let ast = compile(src, deps); let line_numbers = LineNumbers::new(src); + let stdlib_is_a_dependency = true; let output = module( &ast, &line_numbers, @@ -185,6 +186,7 @@ pub fn compile_js(src: &str, deps: Vec<(&str, &str, &str)>) -> Result