Skip to content

Commit 8187412

Browse files
committed
Proof of concept: support directives.
Allow to specify directives emitted verbatim at the top of generated js files. See #5840
1 parent 44ef457 commit 8187412

File tree

7 files changed

+30
-1
lines changed

7 files changed

+30
-1
lines changed

jscomp/common/js_config.ml

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type jsx_module = React
2929
type jsx_mode = Classic | Automatic
3030

3131
let no_version_header = ref false
32+
33+
let directives = ref []
3234
let cross_module_inline = ref false
3335
let diagnose = ref false
3436

jscomp/common/js_config.mli

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ type jsx_mode = Classic | Automatic
3232
val no_version_header : bool ref
3333
(** set/get header *)
3434

35+
val directives : string list ref
36+
(** directives printed verbatims just after the version header *)
37+
3538
(** return [package_name] and [path]
3639
when in script mode:
3740
*)

jscomp/core/js_dump_program.ml

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ let pp_deps_program ~(output_prefix : string)
105105
if not !Js_config.no_version_header then (
106106
P.string f Bs_version.header;
107107
P.newline f);
108+
!Js_config.directives |> List.iter (fun prim ->
109+
P.string f prim;
110+
P.newline f);
111+
108112
if deps_program_is_empty program then P.string f empty_explanation
109113
(* This is empty module, it won't be referred anywhere *)
110114
else

jscomp/frontend/bs_builtin_ppx.ml

+4
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
431431
[ { pvb_pat; pvb_expr; pvb_attributes; pvb_loc } ] );
432432
})
433433
| Pstr_attribute ({ txt = "bs.config" | "config" }, _) -> str
434+
| Pstr_attribute ({ txt = "directive" },
435+
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->
436+
Js_config.directives := d :: !Js_config.directives;
437+
str
434438
| _ -> default_mapper.structure_item self str
435439

436440
let local_module_name =

jscomp/test/build.ninja

+2-1
Large diffs are not rendered by default.

jscomp/test/directives.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
first directive;
2+
second directive;
3+
'use strict';
4+
5+
var Belt_Array = require("../../lib/js/belt_Array.js");
6+
7+
var a = Belt_Array.forEach;
8+
9+
exports.a = a;
10+
/* No side effect */

jscomp/test/directives.res

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@@directive("first directive;")
2+
@@directive("second directive;")
3+
4+
let a = Belt.Array.forEach
5+

0 commit comments

Comments
 (0)