diff --git a/docs/NPM-Support.md b/docs/NPM-Support.md index f134547dae2..1c0ec669f83 100644 --- a/docs/NPM-Support.md +++ b/docs/NPM-Support.md @@ -12,7 +12,7 @@ Since CommonJS has no namespaces, to allow JS files live in different directories, we have a flag ```sh -bsc -bs-npm-output-path $npm_package_name:path/to/your/js/dir -c a.ml +bsc -bs-package-name $npm_package_name -bs-package-output-path path/to/your/js/dir -c a.ml ``` By passing this flag, `bsc` will store your `package_name` and @@ -37,7 +37,7 @@ If you follow the layout convention above, using an OCaml package is pretty straightforward: ``` -bsc -bs-npm-package-include ocaml-library -c a.ml +bsc -bs-package-include ocaml-library -c a.ml ``` @@ -47,6 +47,6 @@ bsc -bs-npm-package-include ocaml-library -c a.ml Your command line would be like this: ``` -bsc -bs-npm-package-include ocaml-library1 -bs-npm-package-include -ocaml-library2 -bs-npm-output-path $npm_package_name:lib/js/ -c a.ml +bsc -bs-package-include ocaml-library1 -bs-npm-package-include +ocaml-library2 -bs-package-name $npm_package_name -bs-package-output path/to/lib/js/ -c a.ml ``` diff --git a/jscomp/common/js_config.ml b/jscomp/common/js_config.ml index fa6d7ce2f98..30ef24c76bf 100644 --- a/jscomp/common/js_config.ml +++ b/jscomp/common/js_config.ml @@ -29,68 +29,87 @@ type env = - | Browser + | Browser + (* "browser-internal" used internal *) | NodeJS | AmdJS - | Goog of string option + | Goog (* of string option *) let default_env = ref NodeJS +type path = string + +type package_info = env * string + + (* | AmdJS of path *) + (* | NodeJS of path *) + (* | Goog *) + +type package_name = string +type packages_info = (package_name * package_info list) option +(** we don't force people to use package *) + + + let ext = ref ".js" let cmj_ext = ".cmj" let get_ext () = !ext let get_env () = !default_env let set_env env = default_env := env -let cmd_set_module str = - match str with - | "commonjs" -> default_env := NodeJS - | "amdjs" -> - default_env := AmdJS - | "browser-internal" -> (* used internal *) - default_env := Browser - | _ -> - if Ext_string.starts_with str "goog" then - let len = String.length str in - if len = 4 then - begin - default_env := Goog (Some ""); - ext := ".g.js" - end - else - if str.[4] = ':' && len > 5 then - begin - default_env := Goog (Some (Ext_string.tail_from str 5 )); - ext := ".g.js"; - end - else - Ext_pervasives.bad_argf "invalid module system %s" str - else - Ext_pervasives.bad_argf "invalid module system %s" str - - -let get_goog_package_name () = - match !default_env with - | Goog x -> x - | Browser - | AmdJS - | NodeJS -> None -let npm_package_path = ref None -let set_npm_package_path s = - match Ext_string.split ~keep_empty:false s ':' with - | [ package_name; path] -> - if String.length package_name = 0 then - (* TODO: check more [package_name] if it is a valid package name *) +(* let npm_package_path = ref None *) - Ext_pervasives.bad_argf "invalid npm package path: %s" s - else - npm_package_path := Some (package_name, path) - | _ -> - Ext_pervasives.bad_argf "invalid npm package path: %s" s -let get_npm_package_path () = !npm_package_path +let packages_info : packages_info ref = ref None + +let get_package_name () = + match !packages_info with + | None -> None + | Some(n,_) -> Some n + + + +let set_package_name name = + match !packages_info with + | None -> packages_info := Some(name, []) + | Some _ -> + Ext_pervasives.bad_argf "duplicated flag for -bs-package-name" + + +let set_npm_package_path s = + match !packages_info with + | None -> + Ext_pervasives.bad_argf "please set package name first using -bs-package-name "; + | Some (name, envs) -> + let env, path = + match Ext_string.split ~keep_empty:false s ':' with + | [ package_name; path] -> + (match package_name with + | "commonjs" -> NodeJS + | "amdjs" -> AmdJS + | "goog" -> Goog + | _ -> + Ext_pervasives.bad_argf "invalid module system %s" package_name), path + | [path] -> + NodeJS, path + | _ -> + Ext_pervasives.bad_argf "invalid npm package path: %s" s + in + packages_info := Some (name, (env,path) :: envs) + (* default_env := env , npm_package_path := Some path *) + + +let get_npm_package_path () = + match !packages_info with + | None -> None + | Some (name, paths) -> + begin match paths with + | (_ , package_path) :: _ -> + Some (name, package_path) + | [] -> None + end let cross_module_inline = ref false @@ -115,7 +134,7 @@ let get_output_dir filename = Filename.dirname filename else Filename.dirname filename - | Some (_package_name, x) -> + | Some (_, x) -> Lazy.force Ext_filename.package_dir // x diff --git a/jscomp/common/js_config.mli b/jscomp/common/js_config.mli index e6384a9e7ec..abe9b276794 100644 --- a/jscomp/common/js_config.mli +++ b/jscomp/common/js_config.mli @@ -31,7 +31,7 @@ type env = | Browser | NodeJS | AmdJS - | Goog of string option + | Goog val cmj_ext : string val get_env : unit -> env @@ -39,11 +39,16 @@ val get_ext : unit -> string val get_output_dir : string -> string val get_output_file : string -> string -val get_goog_package_name : unit -> string option + val set_npm_package_path : string -> unit + +(** return [package_name] and [path] *) val get_npm_package_path : unit -> (string * string) option +val set_package_name : string -> unit +val get_package_name : unit -> string option + val cross_module_inline : bool ref val set_cross_module_inline : bool -> unit val get_cross_module_inline : unit -> bool @@ -53,7 +58,7 @@ val get_diagnose : unit -> bool val set_diagnose : bool -> unit val set_env : env -> unit -val cmd_set_module : string -> unit + val default_gen_tds : bool ref val no_builtin_ppx_ml : bool ref diff --git a/jscomp/js_cmj_format.ml b/jscomp/js_cmj_format.ml index 491d2ef3901..21af18bf751 100644 --- a/jscomp/js_cmj_format.ml +++ b/jscomp/js_cmj_format.ml @@ -40,6 +40,19 @@ type effect = string option type npm_package_path = string * string +type package_name = string + +type path = string + +type package_info = + | AmdJS of path + | CommonJS of path + | Goog + +type packages_info = + (package_name * package_info) option +(** we don't force people to use package *) + type t = { values : cmj_value String_map.t; effect : effect; diff --git a/jscomp/js_dump.ml b/jscomp/js_dump.ml index e91a8666f2e..8fa90e72fac 100644 --- a/jscomp/js_dump.ml +++ b/jscomp/js_dump.ml @@ -1659,12 +1659,11 @@ let pp_deps_program ( program : J.deps_program) (f : Ext_pp.t) = (* amd_program f program *) | _ -> amd_program f program end - | Goog opt -> + | Goog -> let goog_package = let v = Js_config.get_module_name () in - match opt with + match Js_config.get_package_name () with | None - | Some "" -> v | Some x -> x ^ "." ^ v in diff --git a/jscomp/js_main.ml b/jscomp/js_main.ml index b0f8e39f253..63a1beff921 100644 --- a/jscomp/js_main.ml +++ b/jscomp/js_main.ml @@ -64,32 +64,51 @@ let set_noassert () = let buckle_script_flags = - ("-bs-npm-output-path", Arg.String Js_config.set_npm_package_path, - " set npm-output-path: package-name:path, for example `bs-platform:lib/js`") + ("-bs-package-name", + Arg.String Js_config.set_package_name, + " set package name, useful when you want to produce npm packages") :: - ("-bs-npm-package-include", Arg.String add_include_path, + ("-bs-package-output-path", + Arg.String Js_config.set_npm_package_path, + " set npm-output-path: [opt_module]:path, for example: 'lib/cjs', 'amdjs:lib/amdjs' and 'goog:lib/gjs'") + :: + ("-bs-package-include", + Arg.String add_include_path, " set package names, for example bs-platform " ) - :: ("-bs-module", Arg.String Js_config.cmd_set_module, - " set module system: commonjs (default), amdjs, google:package_name") - :: ("-bs-no-builtin-ppx-ml", Arg.Set Js_config.no_builtin_ppx_ml, - "disable built-in ppx for ml files (internal use)") - :: ("-bs-no-builtin-ppx-mli", Arg.Set Js_config.no_builtin_ppx_mli, - "disable built-in ppx for mli files (internal use)") - :: ("-bs-cross-module-opt", Arg.Set Js_config.cross_module_inline, - "enable cross module inlining(experimental), default(false)") - :: ("-bs-gen-tds", Arg.Set Js_config.default_gen_tds, - " set will generate `.d.ts` file for typescript (experimental)") - :: ("-bs-diagnose", Arg.Set Js_config.diagnose, - " More verbose output") - :: ("-bs-no-check-div-by-zero", Arg.Clear Js_config.check_div_by_zero, - " unsafe mode, don't check div by zero and mod by zero") - :: ("-bs-no-any-assert", Arg.Unit set_noassert, - " no code containing any assertion" - ) - :: ("-bs-files", Arg.Rest collect_file, - " Provide batch of files, the compiler will sort it before compiling" - ) - + :: + ("-bs-no-builtin-ppx-ml", + Arg.Set Js_config.no_builtin_ppx_ml, + "disable built-in ppx for ml files (internal use)") + :: + ("-bs-no-builtin-ppx-mli", + Arg.Set Js_config.no_builtin_ppx_mli, + "disable built-in ppx for mli files (internal use)") + :: + ("-bs-cross-module-opt", + Arg.Set Js_config.cross_module_inline, + "enable cross module inlining(experimental), default(false)") + :: + ("-bs-gen-tds", + Arg.Set Js_config.default_gen_tds, + " set will generate `.d.ts` file for typescript (experimental)") + :: + ("-bs-diagnose", + Arg.Set Js_config.diagnose, + " More verbose output") + :: + ("-bs-no-check-div-by-zero", + Arg.Clear Js_config.check_div_by_zero, + " unsafe mode, don't check div by zero and mod by zero") + :: + ("-bs-no-any-assert", + Arg.Unit set_noassert, + " no code containing any assertion" + ) + :: + ("-bs-files", + Arg.Rest collect_file, + " Provide batch of files, the compiler will sort it before compiling" + ) :: Ocaml_options.mk_impl impl :: Ocaml_options.mk_intf intf :: Ocaml_options.mk__ anonymous diff --git a/jscomp/js_program_loader.ml b/jscomp/js_program_loader.ml index 57919b928f2..5a205785fd0 100644 --- a/jscomp/js_program_loader.ml +++ b/jscomp/js_program_loader.ml @@ -45,7 +45,7 @@ let string_of_module_id (x : Lam_module_ident.t) : string = let id = x.id in let file = Printf.sprintf "%s.js" id.name in begin match Js_config.get_env () with - | Goog _ -> + | Goog -> (*TODO: we should store the goog module name in the [cmj] file *) diff --git a/jscomp/lam_stats_export.ml b/jscomp/lam_stats_export.ml index fdb4ae83458..3b69fef6737 100644 --- a/jscomp/lam_stats_export.ml +++ b/jscomp/lam_stats_export.ml @@ -159,7 +159,7 @@ let export_to_cmj in {values; effect ; - goog_package = Js_config.get_goog_package_name (); + goog_package = Js_config.get_package_name (); npm_package_path = Js_config.get_npm_package_path (); } diff --git a/jscomp/runtime/Makefile b/jscomp/runtime/Makefile index c71ba147a78..b983617c36b 100644 --- a/jscomp/runtime/Makefile +++ b/jscomp/runtime/Makefile @@ -25,7 +25,7 @@ $(addsuffix .cmi, $(OTHERS)): js.cmi RUNTIME := $(addsuffix .cmj, $(SOURCE_LIST)) -COMPFLAGS += $(MODULE_FLAGS) -I ../stdlib -nostdlib -nopervasives -open Pervasives -w -40 -bs-npm-output-path $(npm_package_name):lib/js -bs-no-check-div-by-zero -bs-cross-module-opt -bs-diagnose +COMPFLAGS += $(MODULE_FLAGS) -I ../stdlib -nostdlib -nopervasives -open Pervasives -w -40 -bs-package-name $(npm_package_name) -bs-package-output-path lib/js -bs-no-check-div-by-zero -bs-cross-module-opt -bs-diagnose diff --git a/jscomp/stdlib/Makefile.shared b/jscomp/stdlib/Makefile.shared index 819de287d21..8897aed824d 100755 --- a/jscomp/stdlib/Makefile.shared +++ b/jscomp/stdlib/Makefile.shared @@ -22,7 +22,7 @@ CAMLC=$(CAMLRUN) $(COMPILER) #COMPFLAGS=-strict-sequence -w +33..39 -g -warn-error A -bin-annot -nostdlib \ # -safe-string COMPFLAGS= $(MODULE_FLAGS) -strict-sequence -w +33..39 -g -warn-error A -nostdlib \ - -safe-string -I ../runtime -bs-npm-output-path $(npm_package_name):lib/js -bs-no-builtin-ppx-ml -bs-no-builtin-ppx-mli -bs-cross-module-opt -bs-diagnose -bs-no-check-div-by-zero + -safe-string -I ../runtime -bs-package-name $(npm_package_name) -bs-package-output-path lib/js -bs-no-builtin-ppx-ml -bs-no-builtin-ppx-mli -bs-cross-module-opt -bs-diagnose -bs-no-check-div-by-zero # OPTCOMPILER=ocamlopt.opt diff --git a/jscomp/test/Makefile b/jscomp/test/Makefile index 1784cdb8411..1d07e030fcf 100644 --- a/jscomp/test/Makefile +++ b/jscomp/test/Makefile @@ -10,7 +10,7 @@ TESTS := $(addsuffix .cmj, $(SOURCE_LIST) ) $(addsuffix .cmi, $(SOURCE_LIST)): ../runtime/js.cmi COMPFLAGS+= $(MODULE_FLAGS) -w -40 -COMPFLAGS+= -bs-diagnose -bs-cross-module-opt -bs-npm-output-path $(npm_package_name):lib/js/test/ +COMPFLAGS+= -bs-diagnose -bs-cross-module-opt -bs-package-name $(npm_package_name) -bs-package-output-path lib/js/test/ $(TESTS): $(CAMLC)