Skip to content

Commit

Permalink
fix(preamble): add the preamble before the // Generated comment (#574)
Browse files Browse the repository at this point in the history
* fix(preamble): add the preamble before the `// Generated` comment

* add missing unstaged test
  • Loading branch information
anmonteiro authored May 12, 2023
1 parent f37a015 commit cf803f3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ Unreleased
([#554](https://github.com/melange-re/melange/pull/554))
- melange: add `--preamble` flag to add a preamble to emitted JS. An example is
`"use client";` in React Server Components, which needs to appear before
imports ([#545](https://github.com/melange-re/melange/pull/545))
imports ([#545](https://github.com/melange-re/melange/pull/545),
[#574](https://github.com/melange-re/melange/pull/574))

0.3.2 2022-11-19
---------------
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

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

6 changes: 3 additions & 3 deletions jscomp/core/js_dump_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ let es6_program ~package_info ~output_info ~output_dir f (x : J.deps_program) =

let pp_deps_program ~package_info ~(output_info : Js_packages_info.output_info)
~(output_prefix : string) (f : Ext_pp.t) (program : J.deps_program) =
if not !Js_config.no_version_header then (
P.string f Melange_version.header;
P.newline f);
Option.iter
(fun preamble ->
P.string f preamble;
P.newline f)
program.preamble;
if not !Js_config.no_version_header then (
P.string f Melange_version.header;
P.newline f);
if deps_program_is_empty program then P.string f empty_explanation
(* This is empty module, it won't be referred anywhere *)
else
Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/dist/jscomp/test/preamble_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Generated by Melange
"use client";
// Generated by Melange
'use strict';


Expand Down
2 changes: 1 addition & 1 deletion nix/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ in
with ocamlPackages;

mkShell {
inputsFrom = lib.attrValues derivations ++ [ melange-compiler-libs ];
inputsFrom = lib.attrValues derivations;
nativeBuildInputs = [
ocamlformat
utop
Expand Down
34 changes: 34 additions & 0 deletions test/preamble.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Demonstrate usage of `--preamble`

$ . ./setup.sh
$ cat > x.ml <<EOF
> let x = 1
> EOF
$ melc x.ml --preamble '"use client";'
"use client";
// Generated by Melange
'use strict';
var x = 1;
exports.x = x;
/* No side effect */
$ cat > x.ml <<EOF
> [@@@bs.config { flags = [| "--preamble"; "\"use client\";" |] }]
> let x = 1
> EOF
$ melc x.ml
"use client";
// Generated by Melange
'use strict';
var x = 1;
exports.x = x;
/* No side effect */

0 comments on commit cf803f3

Please sign in to comment.