Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements custom resolvers #6132

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands/commandUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ let make_options ~flowconfig ~lazy_mode ~root (options_flags: Options_flags.t) =
opt_profile = options_flags.profile;
opt_strip_root = options_flags.strip_root;
opt_module = FlowConfig.module_system flowconfig;
opt_module_resolver = FlowConfig.module_resolver flowconfig;
opt_munge_underscores =
options_flags.munge_underscore_members || FlowConfig.munge_underscores flowconfig;
opt_temp_dir = temp_dir;
Expand Down
12 changes: 12 additions & 0 deletions src/commands/config/flowConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Opts = struct
haste_use_name_reducers: bool;
ignore_non_literal_requires: bool;
include_warnings: bool;
module_resolver: Path.t option;
module_system: Options.module_system;
module_name_mappers: (Str.regexp * string) list;
node_resolver_dirnames: string list;
Expand Down Expand Up @@ -154,6 +155,7 @@ module Opts = struct
ignore_non_literal_requires = false;
include_warnings = false;
merge_timeout = Some 100;
module_resolver = None;
module_system = Options.Node;
module_name_mappers = [];
node_resolver_dirnames = ["node_modules"];
Expand Down Expand Up @@ -677,6 +679,15 @@ let parse_options config lines =
);
}

|> define_opt "module.resolver" {
initializer_ = USE_DEFAULT;
flags = [];
optparser = optparse_filepath;
setter = (fun opts v -> Ok {
opts with module_resolver = Some v;
});
}

|> define_opt "module.system" {
initializer_ = USE_DEFAULT;
flags = [];
Expand Down Expand Up @@ -1019,6 +1030,7 @@ let max_workers c = c.options.Opts.max_workers
let merge_timeout c = c.options.Opts.merge_timeout
let module_file_exts c = c.options.Opts.module_file_exts
let module_name_mappers c = c.options.Opts.module_name_mappers
let module_resolver c = c.options.Opts.module_resolver
let module_resource_exts c = c.options.Opts.module_resource_exts
let module_system c = c.options.Opts.module_system
let modules_are_use_strict c = c.options.Opts.modules_are_use_strict
Expand Down
1 change: 1 addition & 0 deletions src/commands/config/flowConfig.mli
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ val max_workers: config -> int
val merge_timeout: config -> int option
val module_file_exts: config -> SSet.t
val module_name_mappers: config -> (Str.regexp * string) list
val module_resolver: config -> Path.t option
val module_resource_exts: config -> SSet.t
val module_system: config -> Options.module_system
val modules_are_use_strict: config -> bool
Expand Down
2 changes: 2 additions & 0 deletions src/common/options.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type t = {
opt_merge_timeout: float option;
opt_module: module_system;
opt_module_name_mappers: (Str.regexp * string) list;
opt_module_resolver: Path.t option;
opt_modules_are_use_strict: bool;
opt_munge_underscores: bool;
opt_profile : bool;
Expand Down Expand Up @@ -107,6 +108,7 @@ let max_trace_depth opts = opts.opt_traces
let max_workers opts = opts.opt_max_workers
let merge_timeout opts = opts.opt_merge_timeout
let module_name_mappers opts = opts.opt_module_name_mappers
let module_resolver opts = opts.opt_module_resolver
let module_system opts = opts.opt_module
let modules_are_use_strict opts = opts.opt_modules_are_use_strict
let root opts = opts.opt_root
Expand Down
40 changes: 40 additions & 0 deletions src/services/inference/module_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,45 @@ let eponymous_module file =

(*******************************)

module External = struct
let external_channels = ref None

let get_external_channels resolver =
match !external_channels with
| Some channels -> channels
| None ->
let program = Path.to_string resolver in

let (child_r, parent_w) = Unix.pipe () in
let (parent_r, child_w) = Unix.pipe () in

let channels = (
(Unix.out_channel_of_descr parent_w),
(Unix.in_channel_of_descr parent_r)
) in

ignore (Unix.create_process program [| program |] child_r child_w Unix.stderr);

external_channels := Some channels;
channels

let resolve_import opts f r =
match Options.module_resolver opts with
| None -> None
| Some resolver ->
let issuer = File_key.to_string f in
let payload = Printf.sprintf "[\"%s\", \"%s\"]\n" r issuer in

let (out_channel, in_channel) = (get_external_channels resolver) in

output_string out_channel payload;
Pervasives.flush out_channel;

Some (input_line in_channel)
end

(*******************************)

module Node = struct
let exported_module _ file _ =
eponymous_module file
Expand Down Expand Up @@ -542,6 +581,7 @@ module Haste: MODULE_SYSTEM = struct
let resolve_import ~options node_modules_containers f loc ?resolution_acc r =
let file = File_key.to_string f in
lazy_seq [
lazy (External.resolve_import options f r);
lazy (Node.resolve_import ~options node_modules_containers f loc ?resolution_acc r);
lazy (match expanded_name r with
| Some r ->
Expand Down