Skip to content

Commit

Permalink
Merge pull request #316 from davesnx/Refactor-mode-arg
Browse files Browse the repository at this point in the history
Enable flag to be availalbe from ppxlib
  • Loading branch information
davesnx authored Jan 27, 2023
2 parents b752c8c + e77ee85 commit 0b6b3d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
4 changes: 1 addition & 3 deletions packages/ppx/src/main.re
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,6 @@ let static_pattern =
)
);

let compatibleModeWithBsEmotionPpx = Config.getArgsBeforeConfigLoaded();

module Mapper = {
let match = module_expr => {
open Ast_pattern;
Expand Down Expand Up @@ -736,7 +734,7 @@ Driver.register_transformation(
),
Context_free.Rule.extension(
Extension.declare(
compatibleModeWithBsEmotionPpx ? "css_" : "css",
Config.compatibleModeWithBsEmotionPpx() ? "css_" : "css",
Extension.Context.Expression,
string_payload,
(~loc as _, ~path as _, payload, _label, _) => {
Expand Down
26 changes: 9 additions & 17 deletions packages/ppx/src/ppx_config.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
exception ConfigNotFound;

type t = {
compatibleModeWithBsEmotionPpx: bool,
production: bool,
Expand All @@ -14,24 +12,18 @@ let value = ref(None);
let set = config => value := Some(config);
let setDefault = () => set(default);

let getUnsafe = () => {
switch (value^) {
| Some(v) => v
| None => raise(ConfigNotFound)
}
};
let get = () => value^

let update = fn => value := value^ |> Option.map(fn);

let updateCompatibleModeWithBsEmotionPpx = bool => {
let config = getUnsafe();
set({ ...config, compatibleModeWithBsEmotionPpx: bool })
get()
|> Option.map(config => set({ ...config, compatibleModeWithBsEmotionPpx: bool }))
|> Option.value(~default=())
};

let compatibleModeWithBsEmotionPpx = () =>
getUnsafe().compatibleModeWithBsEmotionPpx;

let find = (name, args) => args |> Array.to_list |> List.find_opt(a => a == name);
let getArgsBeforeConfigLoaded = () => {
Sys.argv |> find("--compat-with-bs-emotion-ppx") |> Option.is_some;
};
let compatibleModeWithBsEmotionPpx = () => {
get()
|> Option.map(c => c.compatibleModeWithBsEmotionPpx)
|> Option.value(~default=default.compatibleModeWithBsEmotionPpx)
}

0 comments on commit 0b6b3d0

Please sign in to comment.