Skip to content

Commit

Permalink
tweak (+3 squashed commits)
Browse files Browse the repository at this point in the history
Squashed commits:
[765f423] WIP
[002305a] remove npm with a more general name
[6b080c2] wip
  • Loading branch information
Hongbo Zhang authored and bobzhang committed Jul 10, 2016
1 parent f93e328 commit b1edc71
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 87 deletions.
8 changes: 4 additions & 4 deletions docs/NPM-Support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```


Expand All @@ -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
```
115 changes: 67 additions & 48 deletions jscomp/common/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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


Expand Down
11 changes: 8 additions & 3 deletions jscomp/common/js_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ type env =
| Browser
| NodeJS
| AmdJS
| Goog of string option
| Goog

val cmj_ext : string
val get_env : unit -> env
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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions jscomp/js_cmj_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions jscomp/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 43 additions & 24 deletions jscomp/js_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion jscomp/js_program_loader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
*)
Expand Down
2 changes: 1 addition & 1 deletion jscomp/lam_stats_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}

2 changes: 1 addition & 1 deletion jscomp/runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down
2 changes: 1 addition & 1 deletion jscomp/stdlib/Makefile.shared
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b1edc71

Please sign in to comment.