diff --git a/compiler-core/src/build/package_compiler.rs b/compiler-core/src/build/package_compiler.rs index 9e0147e45f8..7eec4613952 100644 --- a/compiler-core/src/build/package_compiler.rs +++ b/compiler-core/src/build/package_compiler.rs @@ -377,7 +377,7 @@ where JavaScript::new(&self.out, typescript, prelude_location, self.target_support).render( &self.io, modules, - self.stdlib_is_a_dependency(), + self.stdlib_package(), )?; if self.copy_native_files { @@ -431,11 +431,21 @@ where Ok(()) } - fn stdlib_is_a_dependency(&self) -> bool { - self.config.dependencies.contains_key("gleam_stdlib") + fn stdlib_package(&self) -> StdlibPackage { + if self.config.dependencies.contains_key("gleam_stdlib") { + StdlibPackage::Present + } else { + StdlibPackage::Missing + } } } +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum StdlibPackage { + Present, + Missing, +} + fn analyse( package_config: &PackageConfig, target: Target, diff --git a/compiler-core/src/codegen.rs b/compiler-core/src/codegen.rs index e4f2ab5a6c9..6831b85c8aa 100644 --- a/compiler-core/src/codegen.rs +++ b/compiler-core/src/codegen.rs @@ -1,6 +1,6 @@ use crate::{ analyse::TargetSupport, - build::{ErlangAppCodegenConfiguration, Module}, + build::{package_compiler::StdlibPackage, ErlangAppCodegenConfiguration, Module}, config::PackageConfig, erlang, io::FileSystemWriter, @@ -182,14 +182,14 @@ impl<'a> JavaScript<'a> { &self, writer: &impl FileSystemWriter, modules: &[Module], - stdlib_is_a_dependency: bool, + stdlib_package: StdlibPackage, ) -> 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, stdlib_is_a_dependency)? + self.js_module(writer, module, &js_name, stdlib_package)? } self.write_prelude(writer)?; Ok(()) @@ -240,7 +240,7 @@ impl<'a> JavaScript<'a> { writer: &impl FileSystemWriter, module: &Module, js_name: &str, - stdblib_is_a_dependency: bool, + stdlib_package: StdlibPackage, ) -> Result<()> { let name = format!("{js_name}.mjs"); let path = self.output_directory.join(name); @@ -252,7 +252,7 @@ impl<'a> JavaScript<'a> { &module.code, self.target_support, self.typescript, - stdblib_is_a_dependency, + stdlib_package, ); 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 f4dea5c36d7..36c7d3aa907 100644 --- a/compiler-core/src/javascript.rs +++ b/compiler-core/src/javascript.rs @@ -7,6 +7,7 @@ mod tests; mod typescript; use crate::analyse::TargetSupport; +use crate::build::package_compiler::StdlibPackage; use crate::build::Target; use crate::codegen::TypeScriptDeclarations; use crate::type_::PRELUDE_MODULE_NAME; @@ -45,7 +46,7 @@ pub struct Generator<'a> { current_module_name_segments_count: usize, target_support: TargetSupport, typescript: TypeScriptDeclarations, - stdlib_is_a_dependency: bool, + stdlib_package: StdlibPackage, } impl<'a> Generator<'a> { @@ -54,7 +55,7 @@ impl<'a> Generator<'a> { module: &'a TypedModule, target_support: TargetSupport, typescript: TypeScriptDeclarations, - stdlib_is_a_dependency: bool, + stdlib_package: StdlibPackage, ) -> Self { let current_module_name_segments_count = module.name.split('/').count(); @@ -66,7 +67,7 @@ impl<'a> Generator<'a> { module_scope: Default::default(), target_support, typescript, - stdlib_is_a_dependency, + stdlib_package, } } @@ -173,7 +174,7 @@ impl<'a> Generator<'a> { }; let echo = if self.tracker.echo_used { - if self.stdlib_is_a_dependency { + if StdlibPackage::Present == self.stdlib_package { self.register_import( &mut imports, "gleam_stdlib", @@ -594,14 +595,14 @@ pub fn module( src: &EcoString, target_support: TargetSupport, typescript: TypeScriptDeclarations, - stdlib_is_a_dependency: bool, + stdlib_package: StdlibPackage, ) -> Result { let document = Generator::new( line_numbers, module, target_support, typescript, - stdlib_is_a_dependency, + stdlib_package, ) .compile() .map_err(|error| crate::Error::JavaScript { diff --git a/compiler-core/src/javascript/tests.rs b/compiler-core/src/javascript/tests.rs index 690e676bcd7..e65723b4870 100644 --- a/compiler-core/src/javascript/tests.rs +++ b/compiler-core/src/javascript/tests.rs @@ -204,7 +204,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 stdlib_package = StdlibPackage::Present; let output = module( &ast, &line_numbers, @@ -212,7 +212,7 @@ pub fn compile_js(src: &str, deps: Vec<(&str, &str, &str)>) -> Result