Skip to content

Commit

Permalink
support generating multiple formats of packages at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
Hongbo Zhang committed Jul 11, 2016
1 parent bd696c0 commit 444dccf
Show file tree
Hide file tree
Showing 32 changed files with 359 additions and 279 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/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
```
2 changes: 1 addition & 1 deletion jscomp/common/common.mllib
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
js_config
literals
ext_log
lam_current_unit

151 changes: 93 additions & 58 deletions jscomp/common/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,68 +29,82 @@


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 module_system =
[ `NodeJS | `AmdJS | `Goog ]
type package_info =
( module_system * string )

type package_name = string
type packages_info =
| Empty (* No set *)
| Browser
| NonBrowser of (package_name * package_info list)
(** 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 is_browser () = !default_env = Browser

let get_ext () = !ext


let packages_info : packages_info ref = ref Empty

let set_browser () =
packages_info := Browser

let get_package_name () =
match !packages_info with
| Empty | Browser -> None
| NonBrowser(n,_) -> Some n



let set_package_name name =
match !packages_info with
| Empty -> packages_info := NonBrowser(name, [])
| _ ->
Ext_pervasives.bad_argf "duplicated flag for -bs-package-name"


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 *)
match !packages_info with
| Empty ->
Ext_pervasives.bad_argf "please set package name first using -bs-package-name ";
| Browser ->
Ext_pervasives.bad_argf "invalid options, already set to browser ";
| NonBrowser(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 := NonBrowser (name, ((env,path) :: envs))
(** Browser is not set via command line only for internal use *)


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 cross_module_inline = ref false

Expand All @@ -104,28 +118,48 @@ let get_diagnose () = !diagnose
let set_diagnose b = diagnose := b

let (//) = Filename.concat

let get_packages_info () = !packages_info

let get_npm_package_path kind =
match !packages_info with
| Empty
| Browser -> None
| NonBrowser (name, paths) ->
begin match List.find (fun (k,_) -> k = kind) paths with
| (_ , package_path) ->
Some (name, package_path)
| exception _ -> assert false
end


(* for a single pass compilation, [output_dir]
can be cached
*)
let get_output_dir filename =
match get_npm_package_path () with
| None ->
let get_output_dir kind filename =
match !packages_info with
| Empty | Browser ->
if Filename.is_relative filename then
Lazy.force Ext_filename.cwd //
Filename.dirname filename
else
Filename.dirname filename
| Some (_package_name, x) ->
Lazy.force Ext_filename.package_dir // x
| NonBrowser (_, modules) ->
begin match List.find (fun (k,_) -> k = kind) modules with
| (_, x) -> Lazy.force Ext_filename.package_dir // x
| exception _ -> assert false
end





(* Note that we can have different [basename] when passed
to many files
*)
let get_output_file filename =
let get_output_file kind filename =
let basename = Filename.basename filename in
Filename.concat (get_output_dir filename)
Filename.concat (get_output_dir kind filename)
(Ext_filename.chop_extension ~loc:__LOC__ basename ^ get_ext())


Expand Down Expand Up @@ -251,7 +285,8 @@ let debug_file = ref ""
let set_current_file f = current_file := f
let get_current_file () = !current_file
let get_module_name () =
Filename.chop_extension (String.uncapitalize !current_file)
Filename.chop_extension
(Filename.basename (String.uncapitalize !current_file))

let iset_debug_file _ = ()
let set_debug_file f = debug_file := f
Expand Down
71 changes: 47 additions & 24 deletions jscomp/common/js_config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,77 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)


type module_system =
[ `NodeJS | `AmdJS | `Goog ] (* This will be serliazed *)


type package_info =
(module_system * string )

type package_name = string
type packages_info =
| Empty (* No set *)
| Browser
| NonBrowser of (package_name * package_info list)


type env =
| Browser
| NodeJS
| AmdJS
| Goog of string option

val cmj_ext : string
val get_env : unit -> env


val is_browser : unit -> bool
val set_browser : unit -> unit


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 get_output_file : module_system -> string -> string
val get_output_dir : module_system -> string -> string


(** return [package_name] and [path] *)
val set_npm_package_path : string -> unit
val get_npm_package_path : unit -> (string * string) option
val get_packages_info : unit -> packages_info
val get_npm_package_path : module_system -> (string * string) option

val set_package_name : string -> unit
val get_package_name : unit -> string option

(** corss module inline option *)
val cross_module_inline : bool ref
val set_cross_module_inline : bool -> unit
val get_cross_module_inline : unit -> bool

(** diagnose option *)
val diagnose : bool ref
val get_diagnose : unit -> bool
val set_diagnose : bool -> unit

val set_env : env -> unit
val cmd_set_module : string -> unit

(** generate tds option *)
val default_gen_tds : bool ref

(** options for builtion ppx *)
val no_builtin_ppx_ml : bool ref
val no_builtin_ppx_mli : bool ref

(** check-div-by-zero option *)
val check_div_by_zero : bool ref
val get_check_div_by_zero : unit -> bool

(* It will imply [-noassert] be set too, note from the implmentation point of view,
in the lambda layer, it is impossible to tell whehther it is [assert (3 <> 2)] or
[if (3<>2) then assert false]
*)
val no_any_assert : bool ref
val set_no_any_assert : unit -> unit
val get_no_any_assert : unit -> bool




(** Internal use *)
val runtime_set : String_set.t
val stdlib_set : String_set.t

Expand Down Expand Up @@ -103,15 +138,3 @@ val is_same_file : unit -> bool

val tool_name : string

val check_div_by_zero : bool ref

val get_check_div_by_zero : unit -> bool

(* It will imply [-noassert] be set too, note from the implmentation point of view,
in the lambda layer, it is impossible to tell whehther it is [assert (3 <> 2)] or
[if (3<>2) then assert false]
*)
val no_any_assert : bool ref

val set_no_any_assert : unit -> unit
val get_no_any_assert : unit -> bool
45 changes: 0 additions & 45 deletions jscomp/common/lam_current_unit.ml

This file was deleted.

0 comments on commit 444dccf

Please sign in to comment.