Skip to content

Commit

Permalink
fix excessive gleam.mjs generation
Browse files Browse the repository at this point in the history
This patch fixes excessive generation by checking for file existence.

An earlier attempt (gleam-lang#3238) had done this by checking for empty `modules`,
noting in its commit message that file existence checks will cause old
`gleam.mjs` to never be updated when a new version is available. I have
since learned that that was incorrect, as new versions of `gleam` delete
`build` folders created by previous versions.

closes gleam-lang#3178
  • Loading branch information
Ofek Doitch authored and Ofek Doitch committed Jun 20, 2024
1 parent 487148b commit 54f4af8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@

Else any functions which rely on this will not be compiled into Javascript.

- Fixed a bug where the compiler would unnecessarily generate `gleam.mjs`,
confusing build tools like Vite.
([Ofek Doitch](https://github.com/ofekd))

### Formatter

### Language Server
Expand Down
12 changes: 10 additions & 2 deletions compiler-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,19 @@ impl<'a> JavaScript<'a> {

fn write_prelude(&self, writer: &impl FileSystemWriter) -> Result<()> {
let rexport = format!("export * from \"{}\";\n", self.prelude_location);
writer.write(&self.output_directory.join("gleam.mjs"), &rexport)?;
let prelude_path = &self.output_directory.join("gleam.mjs");

if !prelude_path.exists() {
writer.write(prelude_path, &rexport)?;
}

if self.typescript == TypeScriptDeclarations::Emit {
let rexport = rexport.replace(".mjs", ".d.mts");
writer.write(&self.output_directory.join("gleam.d.mts"), &rexport)?;
let prelude_declaration_path = &self.output_directory.join("gleam.d.mts");

if !prelude_declaration_path.exists() {
writer.write(prelude_declaration_path, &rexport)?;
}
}

Ok(())
Expand Down

0 comments on commit 54f4af8

Please sign in to comment.