diff --git a/docs/docson/build-schema.json b/docs/docson/build-schema.json index 8ab04e62df..0163dee27a 100644 --- a/docs/docson/build-schema.json +++ b/docs/docson/build-schema.json @@ -426,6 +426,13 @@ "type": "string" }, "description": "(Not needed usually) arguments to pass to `refmt` above. Default: `[\"--print\", \"binary\"]`." + }, + "suffix" : { + "enum" : [ + ".js", + ".bs.js" + ], + "description": "suffix of generated js files, default to [.js] " } }, "additionalProperties": false, diff --git a/jscomp/all.depend b/jscomp/all.depend index 4807757329..ba974bf4c8 100644 --- a/jscomp/all.depend +++ b/jscomp/all.depend @@ -96,8 +96,7 @@ ext/ext_filename.cmx : ext/literals.cmx ext/ext_string.cmx \ ext/ext_pervasives.cmx ext/ext_path.cmx ext/ext_filename.cmi ext/ext_modulename.cmx : ext/ext_string.cmx ext/ext_path.cmx \ ext/ext_modulename.cmi -ext/ext_namespace.cmx : ext/literals.cmx ext/ext_string.cmx \ - ext/ext_namespace.cmi +ext/ext_namespace.cmx : ext/ext_string.cmx ext/ext_namespace.cmi ext/literals.cmi : ext/ext_util.cmi : ext/ext_int.cmi : @@ -275,7 +274,7 @@ depends/ast_extract.cmx : ext/string_map.cmx ext/string_hashtbl.cmx \ depends/binary_ast.cmx : common/ml_binary.cmx depends/ast_extract.cmx \ depends/binary_ast.cmi core/config_util.cmi : -core/js_packages_info.cmi : core/lam_module_ident.cmi +core/js_packages_info.cmi : core/lam_module_ident.cmi ext/ext_namespace.cmi core/js_packages_state.cmi : core/js_packages_info.cmi core/ocaml_types.cmi : core/bs_conditional_initial.cmi : @@ -292,7 +291,7 @@ core/lam_inline_util.cmi : core/lam.cmi core/lam_analysis.cmi : core/lam.cmi core/lam_closure.cmi : core/lam.cmi ext/ident_set.cmi ext/ident_map.cmi core/js_cmj_format.cmi : ext/string_map.cmi core/lam_arity.cmi core/lam.cmi \ - core/js_packages_info.cmi + core/js_packages_info.cmi ext/ext_namespace.cmi core/js_cmj_load.cmi : core/js_cmj_format.cmi core/js_fun_env.cmi : ext/ident_set.cmi core/js_call_info.cmi : @@ -416,7 +415,7 @@ core/lam_closure.cmx : core/lam_analysis.cmx core/lam.cmx ext/ident_set.cmx \ ext/ident_map.cmx ext/ext_list.cmx core/lam_closure.cmi core/js_cmj_format.cmx : ext/string_map.cmx core/lam_arity.cmx core/lam.cmx \ core/js_packages_info.cmx ext/ext_string.cmx ext/ext_pervasives.cmx \ - core/js_cmj_format.cmi + ext/ext_namespace.cmx core/js_cmj_format.cmi core/js_cmj_load.cmx : core/js_cmj_format.cmx core/config_util.cmx \ depends/bs_exception.cmx core/js_cmj_load.cmi core/js_fun_env.cmx : ext/ident_set.cmx ext/ext_list.cmx core/js_fun_env.cmi @@ -753,7 +752,7 @@ ounit_tests/ounit_vec_test.cmx : ext/set_int.cmx ounit/oUnit.cmx \ ext/int_vec.cmx ext/ext_list.cmx ext/ext_json.cmx ounit_tests/ounit_data_random.cmx : ounit_tests/ounit_string_tests.cmx : ounit_tests/ounit_data_random.cmx \ - ounit/oUnit.cmx ext/ext_string.cmx ext/ext_namespace.cmx + ounit/oUnit.cmx ext/ext_string.cmx ext/ext_path.cmx ext/ext_namespace.cmx ounit_tests/ounit_utf8_test.cmx : ounit/oUnit.cmx ext/ext_utf8.cmx ounit_tests/ounit_js_regex_checker_tests.cmx : ounit/oUnit.cmx \ ext/ext_js_regex.cmx @@ -872,9 +871,10 @@ bsb/bsb_ninja_regen.cmx : ext/literals.cmx ext/ext_path.cmx ext/ext_list.cmx \ bsb/bsb_ninja_util.cmx : ext/ext_string.cmx bsb/bsb_rule.cmx \ bsb/bsb_ninja_util.cmi bsb/bsb_package_specs.cmx : ext/string_map.cmx ext/literals.cmx \ - ext/ext_string.cmx ext/ext_path.cmx ext/ext_namespace.cmx \ - ext/ext_json_types.cmx ext/ext_json.cmx bsb/bsb_exception.cmx \ - bsb/bsb_config.cmx bsb/bsb_build_schemas.cmx bsb/bsb_package_specs.cmi + common/js_config.cmx ext/ext_string.cmx ext/ext_path.cmx \ + ext/ext_namespace.cmx ext/ext_json_types.cmx ext/ext_json.cmx \ + bsb/bsb_exception.cmx bsb/bsb_config.cmx bsb/bsb_build_schemas.cmx \ + bsb/bsb_package_specs.cmi bsb/bsb_parse_sources.cmx : ext/string_vec.cmx ext/string_set.cmx \ ext/string_map.cmx ext/string_hashtbl.cmx ext/ext_string.cmx \ ext/ext_path.cmx ext/ext_list.cmx ext/ext_json_types.cmx ext/ext_json.cmx \ @@ -955,7 +955,7 @@ bsb/bsb_world.cmi : bsb/bsb_config_types.cmx jscmj_main.cmx : ext/literals.cmx core/js_cmj_format.cmx ext/ext_string.cmx \ ext/ext_pp.cmx ext/ext_pervasives.cmx ext/ext_list.cmx ext/ext_array.cmx \ jscmj_main.cmi -jsgen_main.cmx : ext/literals.cmx ext/ext_string.cmx ext/ext_pervasives.cmx \ - ext/ext_list.cmx ext/ext_array.cmx +jsgen_main.cmx : ext/ext_pervasives.cmx ext/ext_path.cmx ext/ext_list.cmx \ + ext/ext_array.cmx rollup_test.cmx : jscmj_main.cmi : diff --git a/jscomp/bb_require.js b/jscomp/bb_require.js deleted file mode 100644 index 408431e362..0000000000 --- a/jscomp/bb_require.js +++ /dev/null @@ -1,124 +0,0 @@ -var globalEval = eval; - -/** - * assume user input is - * @param {string} base './stdlib/array[.js]' - * @rel '../runtime/caml_array' - */ -function relativePath(base, rel){ - return base + '/../' + rel -} - -/** - * Used in cache - * @param {string} path - * for example './stdlib/array.js' - * - */ -function normalizePath(path){ - if (!path.endsWith(".js")){ - path += ".js" - } - var files = path.split('/') - var real = [] - for (var i = 0 ; i < files.length; ++i){ - if (files[i] === '..'){ - real.pop() - } - else if(files[i] === '.'){ - // nothing - } - else if (files[i] === ''){ - - } - else { - real.push(files[i]) - } - } - return real.join('/') -} - - - -function extractDeps(code){ - var deps = []; - code.replace(/(?:^|[^\w\$_.])require\s*\(\s*["']([^"']*)["']\s*\)/g, function (_, id) { - deps.push(id); - }); - //return [deps, "(function (require,exports) {\n" + code + "\n})"]; - return deps; -} - -/** - * @url example : './stdlib/array.js' - */ -function getContent(url, cb) { - url = normalizePath(url); - var x = new XMLHttpRequest(); - x.open('GET', url, true); - x.addEventListener('load', function(event){ return cb(url, x.response)}); - x.send() -} - -var Modules = {} - -function initModule(url, deps, response){ - if(!Modules[url]){ - Modules[url] = { - deps : deps , - exports : undefined, - response : response, - url : url - }; - } - return Modules[url] -} -function decrDeps(mod){ - -- mod.deps ; - if(mod.deps === 0){ - evalResponse(mod.url, mod.response) - } -} -function evalResponse(url, response) { - if (!Modules[url].exports) { - (globalEval("(function(require, exports){\n" + response + "\n})//# sourceURL=" + document.getElementById('base_ref').href + url))( - function (link) { return req(url, link); }, - Modules[url].exports = {}); - } -} -function req(currentloc, file) { - var url = normalizePath(relativePath(currentloc, file)); - if (Modules[url].exports !== undefined) { - return Modules[url].exports - } else { - throw new Error(currentloc + " " + file + "not loaded"); - } -} -function cb(url, response, evalResponse) { - url = normalizePath(url); - var deps = extractDeps(response); - var mod = initModule(url, deps.length, response) - if (deps.length > 0) { - /* async.map(deps, function (value,yet_ignored){ - console.log(url, '..', value); - getContent(relativePath(url,value),function(url,response){ - cb(url,response, yet_ignored); - // yet_ignored(null,true); - }); - - }, function(err, results){ - evalResponse(url,response) - }); - */ - for (var i = 0; i < deps.length; ++i) { - // console.log(url,deps[i], relativePath(url,deps[i])); - getContent(relativePath(url, deps[i]), function (url, response) { - cb(url, response); - decrDeps(mod) - }) - } - // evalResponse(url, response) - } else { - evalResponse(null, url, response) - } -} diff --git a/jscomp/bin/all_ounit_tests.ml b/jscomp/bin/all_ounit_tests.ml index aebe146a52..e89d23d43b 100644 --- a/jscomp/bin/all_ounit_tests.ml +++ b/jscomp/bin/all_ounit_tests.ml @@ -4894,7 +4894,7 @@ val suffix_rei : string val suffix_d : string val suffix_mlastd : string val suffix_mliastd : string -val suffix_js : string + val suffix_mli : string val suffix_cmt : string val suffix_cmti : string @@ -5029,7 +5029,7 @@ let suffix_mliast_simple = ".mliast_simple" let suffix_d = ".d" let suffix_mlastd = ".mlast.d" let suffix_mliastd = ".mliast.d" -let suffix_js = ".js" + let commonjs = "commonjs" let amdjs = "amdjs" @@ -11521,7 +11521,9 @@ val chop_extension : ?loc:string -> string -> string val chop_extension_if_any : string -> string - +val chop_all_extensions_if_any : + string -> string + (** {[ get_extension "a.txt" = ".txt" @@ -11690,6 +11692,11 @@ let chop_extension ?(loc="") name = let chop_extension_if_any fname = try Filename.chop_extension fname with Invalid_argument _ -> fname +let rec chop_all_extensions_if_any fname = + match Filename.chop_extension fname with + | x -> chop_all_extensions_if_any x + | exception _ -> fname + let get_extension x = let pos = Ext_string.rindex_neg x '.' in if pos < 0 then "" @@ -13768,25 +13775,32 @@ module Ext_namespace : sig * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) (** [make ~ns "a" ] - A typical example would return "a-Ns" - Note the namespace comes from the output of [namespace_of_package_name] + A typical example would return "a-Ns" + Note the namespace comes from the output of [namespace_of_package_name] *) val make : ns:string -> string -> string (* Note we have to output uncapitalized file Name, - or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` - relevant issues: #1609, #913 - - #1933 when removing ns suffix, don't pass the bound - of basename -*) -val js_name_of_basename : string -> string + or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` + relevant issues: #1609, #913 -(** [js_name_of_modulename ~little A-Ns] + #1933 when removing ns suffix, don't pass the bound + of basename *) -val js_name_of_modulename : little:bool -> string -> string +val js_name_of_basename : + bool -> + string -> string + +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + (** [js_name_of_modulename ~little A-Ns] + *) +val js_name_of_modulename : file_kind -> string -> string (* TODO handle cases like '@angular/core' @@ -13852,15 +13866,29 @@ let remove_ns_suffix name = if i < 0 then name else String.sub name 0 i - -let js_name_of_basename s = - remove_ns_suffix s ^ Literals.suffix_js - -let js_name_of_modulename ~little s = - if little then - remove_ns_suffix (String.uncapitalize s) ^ Literals.suffix_js - else - remove_ns_suffix s ^ Literals.suffix_js +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + +let suffix_js = ".js" +let bs_suffix_js = ".bs.js" + +let js_name_of_basename bs_suffix s = + remove_ns_suffix s ^ + (if bs_suffix then bs_suffix_js else suffix_js ) + +let js_name_of_modulename little s = + match little with + | Little_js -> + remove_ns_suffix (String.uncapitalize s) ^ suffix_js + | Little_bs -> + remove_ns_suffix (String.uncapitalize s) ^ bs_suffix_js + | Upper_js -> + remove_ns_suffix s ^ suffix_js + | Upper_bs -> + remove_ns_suffix s ^ bs_suffix_js (* https://docs.npmjs.com/files/package.json Some rules: @@ -13931,7 +13959,7 @@ let ((>::), let (=~) = OUnit.assert_equal - +let printer_string = fun x -> x let suites = @@ -14227,18 +14255,23 @@ let suites = =~ "Reason" end; __LOC__ >:: begin fun _ -> - Ext_namespace.js_name_of_basename "a-b" + Ext_namespace.js_name_of_basename false "a-b" =~ "a.js"; - Ext_namespace.js_name_of_basename "a-" + Ext_namespace.js_name_of_basename false "a-" =~ "a.js"; - Ext_namespace.js_name_of_basename "a--" + Ext_namespace.js_name_of_basename false "a--" =~ "a-.js"; - Ext_namespace.js_name_of_basename "AA-b" + Ext_namespace.js_name_of_basename false "AA-b" =~ "AA.js"; - Ext_namespace.js_name_of_modulename ~little:true "AA-b" + Ext_namespace.js_name_of_modulename + Little_js "AA-b" =~ "aA.js"; - Ext_namespace.js_name_of_modulename ~little:false "AA-b" + Ext_namespace.js_name_of_modulename + Upper_js "AA-b" =~ "AA.js"; + Ext_namespace.js_name_of_modulename + Upper_bs "AA-b" + =~ "AA.bs.js"; end; __LOC__ >:: begin fun _ -> @@ -14252,7 +14285,14 @@ let suites = let v = "bc" in f v =~ "Bc"; v =~ "bc" - end + end; + __LOC__ >:: begin fun _ -> + let (=~) = OUnit.assert_equal ~printer:printer_string in + Ext_path.chop_all_extensions_if_any "a.bs.js" =~ "a" ; + Ext_path.chop_all_extensions_if_any "a.js" =~ "a"; + Ext_path.chop_all_extensions_if_any "a" =~ "a"; + Ext_path.chop_all_extensions_if_any "a.x.bs.js" =~ "a" + end; ] end diff --git a/jscomp/bin/bsb.d b/jscomp/bin/bsb.d index 0303cb9e98..946df2adfe 100644 --- a/jscomp/bin/bsb.d +++ b/jscomp/bin/bsb.d @@ -60,6 +60,7 @@ bin/bsb.ml : ext/string_set.mli bin/bsb.ml : ext/string_vec.mli bin/bsb.ml : bsb/bsb_default.mli bin/bsb.ml : bsb/bsb_warning.mli +bin/bsb.ml : common/js_config.ml bin/bsb.ml : ext/ext_file_pp.mli bin/bsb.ml : ext/ext_filename.ml bin/bsb.ml : ext/ext_position.ml @@ -70,6 +71,7 @@ bin/bsb.ml : bsb/bsb_exception.ml bin/bsb.ml : bsb/bsb_ninja_gen.ml bin/bsb.ml : bsb/bsb_templates.ml bin/bsb.ml : common/bs_version.ml +bin/bsb.ml : common/js_config.mli bin/bsb.ml : ext/ext_filename.mli bin/bsb.ml : ext/ext_namespace.ml bin/bsb.ml : ext/ext_position.mli diff --git a/jscomp/bin/bsb.ml b/jscomp/bin/bsb.ml index ec501ff61f..d14032747d 100644 --- a/jscomp/bin/bsb.ml +++ b/jscomp/bin/bsb.ml @@ -144,6 +144,7 @@ let in_source = "in-source" let warnings = "warnings" let number = "number" let error = "error" +let suffix = "suffix" end module Ext_array : sig #1 "ext_array.mli" @@ -3356,7 +3357,7 @@ val suffix_rei : string val suffix_d : string val suffix_mlastd : string val suffix_mliastd : string -val suffix_js : string + val suffix_mli : string val suffix_cmt : string val suffix_cmti : string @@ -3491,7 +3492,7 @@ let suffix_mliast_simple = ".mliast_simple" let suffix_d = ".d" let suffix_mlastd = ".mlast.d" let suffix_mliastd = ".mliast.d" -let suffix_js = ".js" + let commonjs = "commonjs" let amdjs = "amdjs" @@ -5200,7 +5201,9 @@ val chop_extension : ?loc:string -> string -> string val chop_extension_if_any : string -> string - +val chop_all_extensions_if_any : + string -> string + (** {[ get_extension "a.txt" = ".txt" @@ -5369,6 +5372,11 @@ let chop_extension ?(loc="") name = let chop_extension_if_any fname = try Filename.chop_extension fname with Invalid_argument _ -> fname +let rec chop_all_extensions_if_any fname = + match Filename.chop_extension fname with + | x -> chop_all_extensions_if_any x + | exception _ -> fname + let get_extension x = let pos = Ext_string.rindex_neg x '.' in if pos < 0 then "" @@ -6508,25 +6516,32 @@ module Ext_namespace : sig * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) (** [make ~ns "a" ] - A typical example would return "a-Ns" - Note the namespace comes from the output of [namespace_of_package_name] + A typical example would return "a-Ns" + Note the namespace comes from the output of [namespace_of_package_name] *) val make : ns:string -> string -> string (* Note we have to output uncapitalized file Name, - or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` - relevant issues: #1609, #913 - - #1933 when removing ns suffix, don't pass the bound - of basename -*) -val js_name_of_basename : string -> string + or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` + relevant issues: #1609, #913 -(** [js_name_of_modulename ~little A-Ns] + #1933 when removing ns suffix, don't pass the bound + of basename *) -val js_name_of_modulename : little:bool -> string -> string +val js_name_of_basename : + bool -> + string -> string + +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + (** [js_name_of_modulename ~little A-Ns] + *) +val js_name_of_modulename : file_kind -> string -> string (* TODO handle cases like '@angular/core' @@ -6592,15 +6607,29 @@ let remove_ns_suffix name = if i < 0 then name else String.sub name 0 i - -let js_name_of_basename s = - remove_ns_suffix s ^ Literals.suffix_js - -let js_name_of_modulename ~little s = - if little then - remove_ns_suffix (String.uncapitalize s) ^ Literals.suffix_js - else - remove_ns_suffix s ^ Literals.suffix_js +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + +let suffix_js = ".js" +let bs_suffix_js = ".bs.js" + +let js_name_of_basename bs_suffix s = + remove_ns_suffix s ^ + (if bs_suffix then bs_suffix_js else suffix_js ) + +let js_name_of_modulename little s = + match little with + | Little_js -> + remove_ns_suffix (String.uncapitalize s) ^ suffix_js + | Little_bs -> + remove_ns_suffix (String.uncapitalize s) ^ bs_suffix_js + | Upper_js -> + remove_ns_suffix s ^ suffix_js + | Upper_bs -> + remove_ns_suffix s ^ bs_suffix_js (* https://docs.npmjs.com/files/package.json Some rules: @@ -6649,6 +6678,226 @@ let namespace_of_package_name (s : string) : string = aux true 0 len ; Buffer.contents buf +end +module Js_config : sig +#1 "js_config.mli" +(* Copyright (C) 2015-2016 Bloomberg Finance L.P. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition to the permissions granted to you by the LGPL, you may combine + * or link a "work that uses the Library" with a publicly distributed version + * of this file to produce a combined library or application, then distribute + * that combined work under the terms of your choosing, with no requirement + * to comply with the obligations normally placed on you by section 4 of the + * LGPL version 3 (or the corresponding section of a later version of the LGPL + * should you choose to use a later version). + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) + + + + + +(* val get_packages_info : + unit -> Js_packages_info.t *) + + +(** set/get header *) +val no_version_header : bool ref + + +(** return [package_name] and [path] + when in script mode: +*) + +(* val get_current_package_name_and_path : + Js_packages_info.module_system -> + Js_packages_info.info_query *) + + +(* 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 + + +(** 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 +val no_warn_ffi_type : bool ref +val no_warn_unused_bs_attribute : bool ref +val no_error_unused_bs_attribute : 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 + + + +(** Debugging utilies *) +val set_current_file : string -> unit +val get_current_file : unit -> string +val get_module_name : unit -> string + +val iset_debug_file : string -> unit +val set_debug_file : string -> unit +val get_debug_file : unit -> string + +val is_same_file : unit -> bool + +val tool_name : string + + +val sort_imports : bool ref +val dump_js : bool ref +val syntax_only : bool ref +val binary_ast : bool ref + + +val bs_suffix : bool ref +end = struct +#1 "js_config.ml" +(* Copyright (C) 2015-2016 Bloomberg Finance L.P. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition to the permissions granted to you by the LGPL, you may combine + * or link a "work that uses the Library" with a publicly distributed version + * of this file to produce a combined library or application, then distribute + * that combined work under the terms of your choosing, with no requirement + * to comply with the obligations normally placed on you by section 4 of the + * LGPL version 3 (or the corresponding section of a later version of the LGPL + * should you choose to use a later version). + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) + + + + + + +(* let add_npm_package_path s = + match !packages_info with + | Empty -> + Ext_pervasives.bad_argf "please set package name first using -bs-package-name "; + | NonBrowser(name, envs) -> + let env, path = + match Ext_string.split ~keep_empty:false s ':' with + | [ package_name; path] -> + (match Js_packages_info.module_system_of_string package_name with + | Some x -> x + | None -> + 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 *) + + +let no_version_header = ref false + +let cross_module_inline = ref false + +let get_cross_module_inline () = !cross_module_inline +let set_cross_module_inline b = + cross_module_inline := b + + +let diagnose = ref false +let get_diagnose () = !diagnose +let set_diagnose b = diagnose := b + +let (//) = Filename.concat + +(* let get_packages_info () = !packages_info *) + +let default_gen_tds = ref false +let no_builtin_ppx_ml = ref false +let no_builtin_ppx_mli = ref false +let no_warn_ffi_type = ref false + +(** TODO: will flip the option when it is ready *) +let no_warn_unused_bs_attribute = ref false +let no_error_unused_bs_attribute = ref false + +let current_file = ref "" +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 + (Filename.basename (String.uncapitalize !current_file)) + +let iset_debug_file _ = () +let set_debug_file f = debug_file := f +let get_debug_file () = !debug_file + + +let is_same_file () = + !debug_file <> "" && !debug_file = !current_file + +let tool_name = "BuckleScript" + +let check_div_by_zero = ref true +let get_check_div_by_zero () = !check_div_by_zero + +let no_any_assert = ref false + +let set_no_any_assert () = no_any_assert := true +let get_no_any_assert () = !no_any_assert + +let sort_imports = ref true +let dump_js = ref false + + + +let syntax_only = ref false +let binary_ast = ref false + +let bs_suffix = ref false end module Bsb_package_specs : sig #1 "bsb_package_specs.mli" @@ -6879,10 +7128,12 @@ let package_output ({format; in_source } : spec) output= *) let get_list_of_output_js package_specs output_file_sans_extension = - Spec_set.fold (fun format acc -> - package_output format - ( Ext_namespace.js_name_of_basename output_file_sans_extension) - :: acc + Spec_set.fold + (fun format acc -> + package_output format + ( Ext_namespace.js_name_of_basename !Js_config.bs_suffix + output_file_sans_extension) + :: acc ) package_specs [] @@ -10131,6 +10382,7 @@ type t = entries : entries_t list ; generators : string String_map.t ; cut_generators : bool; (* note when used as a dev mode, we will always ignore it *) + bs_suffix : bool ; (* true means [.bs.js] we should pass [-bs-suffix] flag *) } end @@ -10840,7 +11092,17 @@ let interpret_json | Some (Obj {map }) -> Bsb_warning.from_map map | Some config -> Bsb_exception.config_error config "expect an object" in + let bs_suffix = + match String_map.find_opt Bsb_build_schemas.suffix map with + | None -> false + | Some (Str {str = ".js"} ) -> false + | Some (Str {str = ".bs.js"}) -> true + | Some config -> + Bsb_exception.config_error config + "expect .bs.js or .js string here" + in { + bs_suffix ; package_name ; namespace ; warning = warning; @@ -12181,6 +12443,7 @@ let output_ninja_and_namespace_map ~bsc_dir ~no_dev ({ + bs_suffix; package_name; external_includes; bsc_flags ; @@ -12228,12 +12491,14 @@ let output_ninja_and_namespace_map Ext_string.inter2 "-ns" s in let bsc_flags = - Ext_string.inter2 Literals.dash_nostdlib @@ - match built_in_dependency with - | None -> bsc_flags - | Some {package_install_path} -> - Ext_string.inter3 dash_i (Filename.quote package_install_path) bsc_flags - + let result = + Ext_string.inter2 Literals.dash_nostdlib @@ + match built_in_dependency with + | None -> bsc_flags + | Some {package_install_path} -> + Ext_string.inter3 dash_i (Filename.quote package_install_path) bsc_flags + in + if bs_suffix then Ext_string.inter2 "-bs-suffix" result else result in let reason_react_jsx_flag = match reason_react_jsx with @@ -12242,7 +12507,7 @@ let output_ninja_and_namespace_map Ext_string.inter2 "-ppx" s in let warnings = Bsb_warning.opt_warning_to_string no_dev warning in - + Bsb_ninja_util.output_kvs [| Bsb_ninja_global_vars.bs_package_flags, bs_package_flags ; diff --git a/jscomp/bin/bsb_helper.ml b/jscomp/bin/bsb_helper.ml index 49d70c4fe2..74c63b352d 100644 --- a/jscomp/bin/bsb_helper.ml +++ b/jscomp/bin/bsb_helper.ml @@ -1836,7 +1836,7 @@ val suffix_rei : string val suffix_d : string val suffix_mlastd : string val suffix_mliastd : string -val suffix_js : string + val suffix_mli : string val suffix_cmt : string val suffix_cmti : string @@ -1971,7 +1971,7 @@ let suffix_mliast_simple = ".mliast_simple" let suffix_d = ".d" let suffix_mlastd = ".mlast.d" let suffix_mliastd = ".mliast.d" -let suffix_js = ".js" + let commonjs = "commonjs" let amdjs = "amdjs" @@ -2048,7 +2048,9 @@ val chop_extension : ?loc:string -> string -> string val chop_extension_if_any : string -> string - +val chop_all_extensions_if_any : + string -> string + (** {[ get_extension "a.txt" = ".txt" @@ -2217,6 +2219,11 @@ let chop_extension ?(loc="") name = let chop_extension_if_any fname = try Filename.chop_extension fname with Invalid_argument _ -> fname +let rec chop_all_extensions_if_any fname = + match Filename.chop_extension fname with + | x -> chop_all_extensions_if_any x + | exception _ -> fname + let get_extension x = let pos = Ext_string.rindex_neg x '.' in if pos < 0 then "" @@ -3425,25 +3432,32 @@ module Ext_namespace : sig * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) (** [make ~ns "a" ] - A typical example would return "a-Ns" - Note the namespace comes from the output of [namespace_of_package_name] + A typical example would return "a-Ns" + Note the namespace comes from the output of [namespace_of_package_name] *) val make : ns:string -> string -> string (* Note we have to output uncapitalized file Name, - or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` - relevant issues: #1609, #913 - - #1933 when removing ns suffix, don't pass the bound - of basename -*) -val js_name_of_basename : string -> string + or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` + relevant issues: #1609, #913 -(** [js_name_of_modulename ~little A-Ns] + #1933 when removing ns suffix, don't pass the bound + of basename *) -val js_name_of_modulename : little:bool -> string -> string +val js_name_of_basename : + bool -> + string -> string + +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + (** [js_name_of_modulename ~little A-Ns] + *) +val js_name_of_modulename : file_kind -> string -> string (* TODO handle cases like '@angular/core' @@ -3509,15 +3523,29 @@ let remove_ns_suffix name = if i < 0 then name else String.sub name 0 i - -let js_name_of_basename s = - remove_ns_suffix s ^ Literals.suffix_js - -let js_name_of_modulename ~little s = - if little then - remove_ns_suffix (String.uncapitalize s) ^ Literals.suffix_js - else - remove_ns_suffix s ^ Literals.suffix_js +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + +let suffix_js = ".js" +let bs_suffix_js = ".bs.js" + +let js_name_of_basename bs_suffix s = + remove_ns_suffix s ^ + (if bs_suffix then bs_suffix_js else suffix_js ) + +let js_name_of_modulename little s = + match little with + | Little_js -> + remove_ns_suffix (String.uncapitalize s) ^ suffix_js + | Little_bs -> + remove_ns_suffix (String.uncapitalize s) ^ bs_suffix_js + | Upper_js -> + remove_ns_suffix s ^ suffix_js + | Upper_bs -> + remove_ns_suffix s ^ bs_suffix_js (* https://docs.npmjs.com/files/package.json Some rules: diff --git a/jscomp/bin/bsdep.ml b/jscomp/bin/bsdep.ml index fe14e41020..1ebe1715f4 100644 --- a/jscomp/bin/bsdep.ml +++ b/jscomp/bin/bsdep.ml @@ -25740,7 +25740,7 @@ val syntax_only : bool ref val binary_ast : bool ref - +val bs_suffix : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -25854,7 +25854,7 @@ let dump_js = ref false let syntax_only = ref false let binary_ast = ref false - +let bs_suffix = ref false end module Literals : sig #1 "literals.mli" @@ -25961,7 +25961,7 @@ val suffix_rei : string val suffix_d : string val suffix_mlastd : string val suffix_mliastd : string -val suffix_js : string + val suffix_mli : string val suffix_cmt : string val suffix_cmti : string @@ -26096,7 +26096,7 @@ let suffix_mliast_simple = ".mliast_simple" let suffix_d = ".d" let suffix_mlastd = ".mlast.d" let suffix_mliastd = ".mliast.d" -let suffix_js = ".js" + let commonjs = "commonjs" let amdjs = "amdjs" diff --git a/jscomp/bin/bsppx.ml b/jscomp/bin/bsppx.ml index 32bdaf58c0..891cc81788 100644 --- a/jscomp/bin/bsppx.ml +++ b/jscomp/bin/bsppx.ml @@ -7689,7 +7689,7 @@ val syntax_only : bool ref val binary_ast : bool ref - +val bs_suffix : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -7803,7 +7803,7 @@ let dump_js = ref false let syntax_only = ref false let binary_ast = ref false - +let bs_suffix = ref false end module Literals : sig #1 "literals.mli" @@ -7910,7 +7910,7 @@ val suffix_rei : string val suffix_d : string val suffix_mlastd : string val suffix_mliastd : string -val suffix_js : string + val suffix_mli : string val suffix_cmt : string val suffix_cmti : string @@ -8045,7 +8045,7 @@ let suffix_mliast_simple = ".mliast_simple" let suffix_d = ".d" let suffix_mlastd = ".mlast.d" let suffix_mliastd = ".mliast.d" -let suffix_js = ".js" + let commonjs = "commonjs" let amdjs = "amdjs" diff --git a/jscomp/bin/whole_compiler.d b/jscomp/bin/whole_compiler.d index be99912547..e3e9a6a193 100644 --- a/jscomp/bin/whole_compiler.d +++ b/jscomp/bin/whole_compiler.d @@ -66,7 +66,6 @@ bin/whole_compiler.ml : core/lam_print.ml bin/whole_compiler.ml : core/lam_stats.ml bin/whole_compiler.ml : core/lam_subst.ml bin/whole_compiler.ml : core/lam_util.mli -bin/whole_compiler.ml : core/type_util.ml bin/whole_compiler.ml : ext/ext_array.mli bin/whole_compiler.ml : ext/ext_bytes.mli bin/whole_compiler.ml : ext/ext_format.ml @@ -90,7 +89,6 @@ bin/whole_compiler.ml : core/lam_group.mli bin/whole_compiler.ml : core/lam_print.mli bin/whole_compiler.ml : core/lam_stats.mli bin/whole_compiler.ml : core/lam_subst.mli -bin/whole_compiler.ml : core/type_util.mli bin/whole_compiler.ml : ext/ext_format.mli bin/whole_compiler.ml : ext/ext_option.mli bin/whole_compiler.ml : ext/ext_string.mli @@ -119,6 +117,7 @@ bin/whole_compiler.ml : core/lam_closure.ml bin/whole_compiler.ml : core/lam_compile.ml bin/whole_compiler.ml : core/lam_id_kind.ml bin/whole_compiler.ml : core/ocaml_parse.ml +bin/whole_compiler.ml : core/ocaml_types.ml bin/whole_compiler.ml : ext/ext_filename.ml bin/whole_compiler.ml : ext/ext_js_regex.ml bin/whole_compiler.ml : ext/ext_position.ml @@ -151,6 +150,7 @@ bin/whole_compiler.ml : core/lam_coercion.ml bin/whole_compiler.ml : core/lam_compile.mli bin/whole_compiler.ml : core/lam_id_kind.mli bin/whole_compiler.ml : core/ocaml_parse.mli +bin/whole_compiler.ml : core/ocaml_types.mli bin/whole_compiler.ml : ext/ext_filename.mli bin/whole_compiler.ml : ext/ext_js_regex.mli bin/whole_compiler.ml : ext/ext_namespace.ml @@ -363,7 +363,6 @@ bin/whole_compiler.ml : core/lam_compile_global.ml bin/whole_compiler.ml : core/lam_eta_conversion.ml bin/whole_compiler.ml : core/lam_pass_lets_dce.mli bin/whole_compiler.ml : core/ocaml_stdlib_slots.ml -bin/whole_compiler.ml : core/type_int_to_string.ml bin/whole_compiler.ml : ext/hash_set_ident_mask.ml bin/whole_compiler.ml : super_errors/super_main.ml bin/whole_compiler.ml : super_errors/super_misc.ml diff --git a/jscomp/bin/whole_compiler.ml b/jscomp/bin/whole_compiler.ml index 87ae9c66bc..c717ec8c1d 100644 --- a/jscomp/bin/whole_compiler.ml +++ b/jscomp/bin/whole_compiler.ml @@ -22681,7 +22681,7 @@ val syntax_only : bool ref val binary_ast : bool ref - +val bs_suffix : bool ref end = struct #1 "js_config.ml" (* Copyright (C) 2015-2016 Bloomberg Finance L.P. @@ -22795,7 +22795,7 @@ let dump_js = ref false let syntax_only = ref false let binary_ast = ref false - +let bs_suffix = ref false end module Bs_exception : sig #1 "bs_exception.mli" @@ -24621,7 +24621,7 @@ val suffix_rei : string val suffix_d : string val suffix_mlastd : string val suffix_mliastd : string -val suffix_js : string + val suffix_mli : string val suffix_cmt : string val suffix_cmti : string @@ -24756,7 +24756,7 @@ let suffix_mliast_simple = ".mliast_simple" let suffix_d = ".d" let suffix_mlastd = ".mlast.d" let suffix_mliastd = ".mliast.d" -let suffix_js = ".js" + let commonjs = "commonjs" let amdjs = "amdjs" @@ -24833,7 +24833,9 @@ val chop_extension : ?loc:string -> string -> string val chop_extension_if_any : string -> string - +val chop_all_extensions_if_any : + string -> string + (** {[ get_extension "a.txt" = ".txt" @@ -25002,6 +25004,11 @@ let chop_extension ?(loc="") name = let chop_extension_if_any fname = try Filename.chop_extension fname with Invalid_argument _ -> fname +let rec chop_all_extensions_if_any fname = + match Filename.chop_extension fname with + | x -> chop_all_extensions_if_any x + | exception _ -> fname + let get_extension x = let pos = Ext_string.rindex_neg x '.' in if pos < 0 then "" @@ -59047,25 +59054,32 @@ module Ext_namespace : sig * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) (** [make ~ns "a" ] - A typical example would return "a-Ns" - Note the namespace comes from the output of [namespace_of_package_name] + A typical example would return "a-Ns" + Note the namespace comes from the output of [namespace_of_package_name] *) val make : ns:string -> string -> string (* Note we have to output uncapitalized file Name, - or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` - relevant issues: #1609, #913 - - #1933 when removing ns suffix, don't pass the bound - of basename -*) -val js_name_of_basename : string -> string + or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` + relevant issues: #1609, #913 -(** [js_name_of_modulename ~little A-Ns] + #1933 when removing ns suffix, don't pass the bound + of basename *) -val js_name_of_modulename : little:bool -> string -> string +val js_name_of_basename : + bool -> + string -> string + +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + (** [js_name_of_modulename ~little A-Ns] + *) +val js_name_of_modulename : file_kind -> string -> string (* TODO handle cases like '@angular/core' @@ -59131,15 +59145,29 @@ let remove_ns_suffix name = if i < 0 then name else String.sub name 0 i - -let js_name_of_basename s = - remove_ns_suffix s ^ Literals.suffix_js - -let js_name_of_modulename ~little s = - if little then - remove_ns_suffix (String.uncapitalize s) ^ Literals.suffix_js - else - remove_ns_suffix s ^ Literals.suffix_js +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + +let suffix_js = ".js" +let bs_suffix_js = ".bs.js" + +let js_name_of_basename bs_suffix s = + remove_ns_suffix s ^ + (if bs_suffix then bs_suffix_js else suffix_js ) + +let js_name_of_modulename little s = + match little with + | Little_js -> + remove_ns_suffix (String.uncapitalize s) ^ suffix_js + | Little_bs -> + remove_ns_suffix (String.uncapitalize s) ^ bs_suffix_js + | Upper_js -> + remove_ns_suffix s ^ suffix_js + | Upper_bs -> + remove_ns_suffix s ^ bs_suffix_js (* https://docs.npmjs.com/files/package.json Some rules: @@ -63394,7 +63422,7 @@ val string_of_module_id : module_system -> t -> (Lam_module_ident.t -> - (string * t * bool ) option ) -> + (string * t * Ext_namespace.file_kind) option ) -> Lam_module_ident.t -> string end = struct #1 "js_packages_info.ml" @@ -63582,7 +63610,7 @@ let string_of_module_id (module_system : module_system) (current_package_info : t) (get_package_path_from_cmj : - Lam_module_ident.t -> (string * t * bool) option + Lam_module_ident.t -> (string * t * Ext_namespace.file_kind) option ) (dep_module_id : Lam_module_ident.t) : string = let result = @@ -63609,7 +63637,7 @@ let string_of_module_id | None -> Bs_exception.error (Missing_ml_dependency dep_module_id.id.name) | Some (cmj_path, package_info, little) -> - let js_file = Ext_namespace.js_name_of_modulename ~little id.name in + let js_file = Ext_namespace.js_name_of_modulename little id.name in let dependency_pkg_info = query_package_infos package_info module_system in @@ -70570,13 +70598,12 @@ type cmj_value = { type effect = string option - - +type cmj_case = Ext_namespace.file_kind type t = { values : cmj_value String_map.t; effect : effect; npm_package_path : Js_packages_info.t; - case : bool; + cmj_case : cmj_case; } val single_na : arity @@ -70637,15 +70664,16 @@ type effect = string option let single_na = Single NA (** we don't force people to use package *) - +type cmj_case = Ext_namespace.file_kind + type t = { values : cmj_value String_map.t; effect : effect; npm_package_path : Js_packages_info.t ; - case : bool; (* little case -> true *) + cmj_case : cmj_case; } -let cmj_magic_number = "BUCKLE20170907" +let cmj_magic_number = "BUCKLE20171012" let cmj_magic_number_length = String.length cmj_magic_number @@ -70654,7 +70682,7 @@ let pure_dummy = values = String_map.empty; effect = None; npm_package_path = Js_packages_info.empty; - case = true; + cmj_case = Little_js; } let no_pure_dummy = @@ -70662,7 +70690,7 @@ let no_pure_dummy = values = String_map.empty; effect = Some Ext_string.empty; npm_package_path = Js_packages_info.empty; - case = true; + cmj_case = Little_js; (** TODO: consistent with Js_config.bs_suffix default *) } @@ -74879,6 +74907,153 @@ let debugger : t = comment = None } +end +module Ocaml_types : sig +#1 "ocaml_types.mli" +(* Copyright (C) 2015-2016 Bloomberg Finance L.P. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition to the permissions granted to you by the LGPL, you may combine + * or link a "work that uses the Library" with a publicly distributed version + * of this file to produce a combined library or application, then distribute + * that combined work under the terms of your choosing, with no requirement + * to comply with the obligations normally placed on you by section 4 of the + * LGPL version 3 (or the corresponding section of a later version of the LGPL + * should you choose to use a later version). + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) + + + + + + + + +(** Utilities for quering typing information from {!Env.t}, this part relies + on compiler API +*) + +type t + +val empty : t +val length : t -> int + + + +(* Input path is a global module + TODO: it should be fine for local module*) +val find_serializable_signatures_by_path : + Ident.t -> Env.t -> t option + +val get_name : t -> int -> string + +val map : (string -> 'a) -> t -> 'a list + +end = struct +#1 "ocaml_types.ml" +(* Copyright (C) 2015-2016 Bloomberg Finance L.P. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition to the permissions granted to you by the LGPL, you may combine + * or link a "work that uses the Library" with a publicly distributed version + * of this file to produce a combined library or application, then distribute + * that combined work under the terms of your choosing, with no requirement + * to comply with the obligations normally placed on you by section 4 of the + * LGPL version 3 (or the corresponding section of a later version of the LGPL + * should you choose to use a later version). + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) + +type t = Types.signature + +let empty = [] +let length = List.length +let name_of_signature_item (x : Types.signature_item )= + match x with + | Sig_value (i,_) + | Sig_module (i,_,_) -> i + | Sig_typext (i,_,_) -> i + | Sig_modtype(i,_) -> i + | Sig_class (i,_,_) -> i + | Sig_class_type(i,_,_) -> i + | Sig_type(i,_,_) -> i + + +(** It should be safe to replace Pervasives[], + we should test cases like module aliases if it is serializable or not + {[ module Pervasives = List ]} +*) +let serializable_signature (x : Types.signature_item) = + match x with + | Sig_value(_, {val_kind = Val_prim _}) -> false + | Sig_typext _ + | Sig_module _ + | Sig_class _ + | Sig_value _ -> true + | Sig_modtype _ | Sig_class_type _ | Sig_type _ -> false + + + +let filter_serializable_signatures signature : t = + List.filter serializable_signature signature + +(* Input path is a global module + TODO: it should be fine for local module +*) +let find_serializable_signatures_by_path v (env : Env.t) + : t option = + match Env.find_module (Pident v) env with + | exception Not_found -> None + | {md_type = Mty_signature signature; _} -> + Some (filter_serializable_signatures signature) + (** TODO: refine *) + | _ -> Ext_log.err __LOC__ "@[impossible path %s@]@." + (Ident.name v) ; assert false + +let rec dump_summary fmt (x : Env.summary) = + match x with + | Env_empty -> () + | Env_value(s,id,value_description) -> + dump_summary fmt s ; + Printtyp.value_description id fmt value_description + | _ -> () + +(** Used in [ Lglobal_module] *) +let get_name (serializable_sigs : t) (pos : int) = + Ident.name (name_of_signature_item (List.nth serializable_sigs pos)) + +let map (f : string -> 'a ) (xs : t) = + Ext_list.map + (fun x -> f (name_of_signature_item x ).name ) xs + + + + + + end module Printlambda : sig #1 "printlambda.mli" @@ -83234,179 +83409,6 @@ let reset () = toploop_ident.Ident.flags <- 0; aliased_idents := Ident.empty -end -module Type_int_to_string -= struct -#1 "type_int_to_string.ml" -(* Copyright (C) 2015-2016 Bloomberg Finance L.P. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) - -let name_of_signature_item (x : Types.signature_item )= - match x with - | Sig_value (i,_) - | Sig_module (i,_,_) -> i - | Sig_typext (i,_,_) -> i - | Sig_modtype(i,_) -> i - | Sig_class (i,_,_) -> i - | Sig_class_type(i,_,_) -> i - | Sig_type(i,_,_) -> i - - -(** It should be safe to replace Pervasives[], - we should test cases like module Pervasives = List *) -let serializable_signature = - (fun x -> - match (x : Types.signature_item) with - | Sig_value(_, {val_kind = Val_prim _}) -> false - | Sig_typext _ - | Sig_module _ - | Sig_class _ - | Sig_value _ -> true - | _ -> false) - -let filter_serializable_signatures (signature : Types.signature) - : Types.signature = - List.filter serializable_signature signature - -end -module Type_util : sig -#1 "type_util.mli" -(* Copyright (C) 2015-2016 Bloomberg Finance L.P. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) - - - - - - - - -(** Utilities for quering typing inforaation from {!Env.t}, this part relies - on compiler API -*) - - -val get_name : Types.signature -> int -> string - - -(* Input path is a global module - TODO: it should be fine for local module*) -val find_serializable_signatures_by_path : - Ident.t -> Env.t -> Types.signature option - - -(* val find_name : *) -(* Ident.t -> int -> Env.t -> string option *) - - - - -end = struct -#1 "type_util.ml" -(* Copyright (C) 2015-2016 Bloomberg Finance L.P. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * In addition to the permissions granted to you by the LGPL, you may combine - * or link a "work that uses the Library" with a publicly distributed version - * of this file to produce a combined library or application, then distribute - * that combined work under the terms of your choosing, with no requirement - * to comply with the obligations normally placed on you by section 4 of the - * LGPL version 3 (or the corresponding section of a later version of the LGPL - * should you choose to use a later version). - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) - - - -(* Input path is a global module - TODO: it should be fine for local module -*) -let find_serializable_signatures_by_path v (env : Env.t) - : Types.signature option = - match Env.find_module (Pident v) env with - | exception Not_found -> None - | {md_type = Mty_signature signature; _} -> - Some (Type_int_to_string.filter_serializable_signatures signature) - (** TODO: refine *) - | _ -> Ext_log.err __LOC__ "@[impossible path %s@]@." - (Ident.name v) ; assert false - -let rec dump_summary fmt (x : Env.summary) = - match x with - | Env_empty -> () - | Env_value(s,id,value_description) -> - dump_summary fmt s ; - Printtyp.value_description id fmt value_description - | _ -> () - -(** Used in [ Lglobal_module] *) -let get_name (serializable_sigs : Types.signature) (pos : int) = - Ident.name @@ Type_int_to_string.name_of_signature_item @@ List.nth serializable_sigs pos - -(* let find_name id pos env = *) -(* match find_serializable_signatures_by_path id env with *) -(* | Some signatures -> *) -(* Some (get_name signatures pos) *) -(* | None -> None *) - - - - - - end module Lam_compile_env : sig #1 "lam_compile_env.mli" @@ -83450,7 +83452,7 @@ type path = string type module_info = { - signature : Types.signature ; + signature : Ocaml_types.t ; pure : bool } @@ -83462,7 +83464,7 @@ type _ t = type ident_info = { id : Ident.t; name : string; - signature : Types.signature; + signature : Ocaml_types.t; arity : Js_cmj_format.arity; closed_lambda : Lam.t option } @@ -83524,7 +83526,7 @@ val is_pure_module : Lam_module_ident.t -> bool val get_package_path_from_cmj : Lam_module_ident.t -> - (string * Js_packages_info.t * bool) option + (string * Js_packages_info.t * Js_cmj_format.cmj_case) option @@ -83577,7 +83579,7 @@ module S = Js_stmt_make type path = string type ml_module_info = { - signature : Types.signature ; + signature : Ocaml_types.t; cmj_table : Js_cmj_format.t ; cmj_path : path; } @@ -83601,7 +83603,7 @@ type env_value = type ident_info = { id : Ident.t; name : string; - signature : Types.signature; + signature : Ocaml_types.t; arity : Js_cmj_format.arity; closed_lambda : Lam.t option } @@ -83673,30 +83675,32 @@ let cached_find_ml_id_pos id pos env : ident_info = let cmj_path, cmj_table = Js_cmj_load.find_cmj (id.name ^ Literals.suffix_cmj) in begin match - Type_util.find_serializable_signatures_by_path id env with + Ocaml_types.find_serializable_signatures_by_path id env with | None -> assert false (*TODO: more informative error message *) | Some signature -> oid +> Visit {signature; cmj_table ; cmj_path } ; - let name = Type_util.get_name signature pos in + let name = Ocaml_types.get_name signature pos in let arity, closed_lambda = match String_map.find_opt name cmj_table.values with - | Some {arity ; closed_lambda} -> arity, closed_lambda + | Some {arity ; closed_lambda} -> + arity, + ( if Js_config.get_cross_module_inline () then + closed_lambda + else None + ) | None -> Js_cmj_format.single_na, None in {id; name ; signature ; arity ; - closed_lambda = - if Js_config.get_cross_module_inline () then - closed_lambda - else None + closed_lambda } end | Some (Visit {signature ; cmj_table = { values ; _} } ) -> - let name = Type_util.get_name signature pos in + let name = Ocaml_types.get_name signature pos in let arity , closed_lambda = match String_map.find_opt name values with | Some {arity; closed_lambda;_} -> @@ -83711,7 +83715,7 @@ let cached_find_ml_id_pos id pos env : ident_info = signature; arity; closed_lambda - (* TODO shall we cache the arity ?*) + (* TODO shall we cache the arity ?*) } | Some (Runtime _) -> assert false | Some External -> assert false @@ -83719,7 +83723,7 @@ let cached_find_ml_id_pos id pos env : ident_info = type module_info = { - signature : Types.signature ; + signature : Ocaml_types.t ; pure : bool } (* TODO: it does not make sense to cache @@ -83742,7 +83746,7 @@ let query_and_add_if_not_exist (type u) oid +> Runtime (true,cmj_path,cmj_table) ; begin match env with | Has_env _ -> - found {signature = []; pure = true} + found {signature = Ocaml_types.empty; pure = true} | No_env -> found cmj_info end @@ -83753,11 +83757,11 @@ let query_and_add_if_not_exist (type u) begin match env with | Has_env env -> begin match - Type_util.find_serializable_signatures_by_path ( oid.id) env with + Ocaml_types.find_serializable_signatures_by_path oid.id env with | None -> not_found () (* actually when [not_found] in the call site, we throw... *) | Some signature -> - oid +> Visit {signature = signature; cmj_table;cmj_path } ; - found { signature ; pure = cmj_table.effect = None} + oid +> Visit {signature; cmj_table;cmj_path } ; + found { signature ; pure = (cmj_table.effect = None)} end | No_env -> found cmj_info @@ -83771,7 +83775,7 @@ let query_and_add_if_not_exist (type u) begin match env with | Has_env _ -> - found {signature = []; pure = false} + found {signature = Ocaml_types.empty; pure = false} | No_env -> found (Ext_string.empty, Js_cmj_format.no_pure_dummy) (* FIXME: #154, it come from External, should be okay *) @@ -83781,21 +83785,21 @@ let query_and_add_if_not_exist (type u) | Some (Visit {signature ; cmj_table = cmj_table; cmj_path}) -> begin match env with | Has_env _ -> - found { signature = signature ; pure = (cmj_table.effect = None)} + found { signature; pure = (cmj_table.effect = None)} | No_env -> found (cmj_path,cmj_table) end | Some (Runtime (pure, cmj_path,cmj_table)) -> begin match env with | Has_env _ -> - found {signature = [] ; pure } + found {signature = Ocaml_types.empty; pure } | No_env -> found (cmj_path, cmj_table) end | Some External -> begin match env with | Has_env _ -> - found {signature = [] ; pure = false} + found {signature = Ocaml_types.empty; pure = false} | No_env -> found (Ext_string.empty, Js_cmj_format.no_pure_dummy) (* External is okay *) end @@ -83821,7 +83825,7 @@ let get_package_path_from_cmj ) ~found:(fun (cmj_path,x) -> Some (cmj_path, - x.npm_package_path, x.case ) + x.npm_package_path, x.cmj_case ) ) @@ -87005,7 +87009,7 @@ end = struct - let string_of_module_id +let string_of_module_id ~output_dir module_system id @@ -93250,7 +93254,7 @@ val expand_global_module : Ident.t -> Env.t -> J.expression -val query_lambda : Ident.t -> Env.t -> Lam.t +val expand_global_module_as_lam : Ident.t -> Env.t -> Lam.t end = struct #1 "lam_compile_global.ml" @@ -93296,20 +93300,23 @@ open Js_output.Ops -let query_lambda id env = - Lam_compile_env.query_and_add_if_not_exist (Lam_module_ident.of_ml id) +let expand_global_module_as_lam id env = + Lam_compile_env.query_and_add_if_not_exist + (Lam_module_ident.of_ml id) (Has_env env) ~not_found:(fun id -> assert false) - ~found:(fun {signature = sigs; _} + ~found:(fun {signature ; _} -> Lam.prim ~primitive:(Pmakeblock(0, Blk_module None, Immutable)) ~args:( - List.mapi (fun i _ -> + let len = Ocaml_types.length signature in + Ext_list.init len (fun i -> Lam.prim ~primitive:(Pfield (i, Lambda.Fld_na)) ~args:[ Lam.global_module id ] Location.none) - sigs) Location.none (* FIXME*)) + ) + Location.none (* FIXME*)) (* Given an module name, find its expanded structure *) @@ -93318,12 +93325,10 @@ let expand_global_module id env : J.expression = (Lam_module_ident.of_ml id) (Has_env env) ~not_found:(fun _ -> assert false) - ~found:(fun {signature = sigs; _} -> - let len = List.length sigs in (** TODO: could be optimized *) - Js_of_lam_module.make ~comment:id.name - (Ext_list.init len (fun i -> - E.ml_var_dot id - (Type_util.get_name sigs i )))) + ~found:(fun {signature; _} -> + Js_of_lam_module.make ~comment:id.name + (Ocaml_types.map (fun name -> E.ml_var_dot id name) signature ) + ) @@ -93505,7 +93510,7 @@ let propogate_beta_reduce (* It's not completeness, its to make it sound.. Pass global module as an argument *) - Lam_compile_global.query_lambda ident meta.env + Lam_compile_global.expand_global_module_as_lam ident meta.env (* alias meta param ident (Module (Global ident)) Strict *) | Lprim {primitive = Pmakeblock (_, _, Immutable) ;args ; _} -> Ident_hashtbl.replace meta.ident_tbl param @@ -93564,7 +93569,7 @@ let propogate_beta_reduce_with_map | Lglobal_module ident -> (* It's not completeness, its to make it sound.. *) - Lam_compile_global.query_lambda ident meta.env + Lam_compile_global.expand_global_module_as_lam ident meta.env (* alias meta param ident (Module (Global ident)) Strict *) | Lprim {primitive = Pmakeblock (_, _, Immutable ) ; args} -> Ident_hashtbl.replace meta.ident_tbl param @@ -102163,7 +102168,7 @@ val export_to_cmj : Js_cmj_format.effect -> Lam_module_ident.t list -> Lam.t Ident_map.t -> - bool -> + Js_cmj_format.cmj_case -> Js_cmj_format.t @@ -102330,7 +102335,7 @@ let export_to_cmj maybe_pure external_ids export_map - case + cmj_case : Js_cmj_format.t = let values = values_of_export meta export_map in let () = @@ -102343,7 +102348,7 @@ let export_to_cmj {values; effect ; npm_package_path = Js_packages_state.get_packages_info (); - case ; + cmj_case ; (* FIXME: make sure [-o] would not change its case add test for ns/non-ns *) @@ -102441,7 +102446,18 @@ end = struct module E = Js_exp_make module S = Js_stmt_make - open Js_output.Ops +let get_cmj_case output_prefix : Ext_namespace.file_kind = + let little = + Ext_char.is_lower_case (Filename.basename output_prefix).[0] + in + match little, !Js_config.bs_suffix with + | true, true -> Little_bs + | true, false -> Little_js + | false, true -> Upper_bs + | false, false -> Upper_js + + +open Js_output.Ops let compile_group ({filename = file_name; env;} as meta : Lam_stats.t) (x : Lam_group.t) : Js_output.t = @@ -102736,7 +102752,7 @@ let compile ~filename (output_prefix : string) env _sigs maybe_pure external_module_ids coerced_input.export_map - (Ext_char.is_lower_case (Filename.basename output_prefix).[0]) + (get_cmj_case output_prefix) in (if not @@ !Clflags.dont_write_files then Js_cmj_format.to_file @@ -102759,10 +102775,10 @@ let lambda_as_module let lambda_output = compile ~filename output_prefix env sigs lam in let basename = (* #758, output_prefix is already chopped *) - Ext_namespace.js_name_of_basename (Filename.basename - output_prefix (* -o *) - (* filename *) (* see #757 *) - ) in + (* -o filename, see #757 *) + Ext_namespace.js_name_of_basename !Js_config.bs_suffix + (Filename.basename + output_prefix) in (* #913 only generate little-case js file *) @@ -112824,7 +112840,8 @@ let process_result ppf main_file ast_table result = if not (!Clflags.compile_only) then Sys.command ("node " ^ - Ext_namespace.js_name_of_basename (Filename.chop_extension main_file) + Ext_namespace.js_name_of_basename + !Js_config.bs_suffix (Filename.chop_extension main_file) ) else 0 @@ -115538,6 +115555,11 @@ let buckle_script_flags : (string * Arg.spec * string) list = Arg.Unit Reason_outcome_printer_main.setup, " Print compiler output in Reason syntax" ) + :: + ("-bs-suffix", + Arg.Set Js_config.bs_suffix, + " Set suffix to .bs.js" + ) :: ("-bs-no-implicit-include", Arg.Set Clflags.no_implicit_current_dir , " Don't include current dir implicitly") diff --git a/jscomp/bsb/bsb_build_schemas.ml b/jscomp/bsb/bsb_build_schemas.ml index 568a547c74..58ef519e6a 100644 --- a/jscomp/bsb/bsb_build_schemas.ml +++ b/jscomp/bsb/bsb_build_schemas.ml @@ -77,4 +77,5 @@ let namespace = "namespace" let in_source = "in-source" let warnings = "warnings" let number = "number" -let error = "error" \ No newline at end of file +let error = "error" +let suffix = "suffix" \ No newline at end of file diff --git a/jscomp/bsb/bsb_config_parse.ml b/jscomp/bsb/bsb_config_parse.ml index 775afc0344..d7427e46b5 100644 --- a/jscomp/bsb/bsb_config_parse.ml +++ b/jscomp/bsb/bsb_config_parse.ml @@ -275,7 +275,17 @@ let interpret_json | Some (Obj {map }) -> Bsb_warning.from_map map | Some config -> Bsb_exception.config_error config "expect an object" in + let bs_suffix = + match String_map.find_opt Bsb_build_schemas.suffix map with + | None -> false + | Some (Str {str = ".js"} ) -> false + | Some (Str {str = ".bs.js"}) -> true + | Some config -> + Bsb_exception.config_error config + "expect .bs.js or .js string here" + in { + bs_suffix ; package_name ; namespace ; warning = warning; diff --git a/jscomp/bsb/bsb_config_types.ml b/jscomp/bsb/bsb_config_types.ml index c5e95d2689..194fe77af7 100644 --- a/jscomp/bsb/bsb_config_types.ml +++ b/jscomp/bsb/bsb_config_types.ml @@ -65,4 +65,5 @@ type t = entries : entries_t list ; generators : string String_map.t ; cut_generators : bool; (* note when used as a dev mode, we will always ignore it *) + bs_suffix : bool ; (* true means [.bs.js] we should pass [-bs-suffix] flag *) } diff --git a/jscomp/bsb/bsb_ninja_gen.ml b/jscomp/bsb/bsb_ninja_gen.ml index 4695c18a23..a42e32d508 100644 --- a/jscomp/bsb/bsb_ninja_gen.ml +++ b/jscomp/bsb/bsb_ninja_gen.ml @@ -55,6 +55,7 @@ let output_ninja_and_namespace_map ~bsc_dir ~no_dev ({ + bs_suffix; package_name; external_includes; bsc_flags ; @@ -102,12 +103,14 @@ let output_ninja_and_namespace_map Ext_string.inter2 "-ns" s in let bsc_flags = - Ext_string.inter2 Literals.dash_nostdlib @@ - match built_in_dependency with - | None -> bsc_flags - | Some {package_install_path} -> - Ext_string.inter3 dash_i (Filename.quote package_install_path) bsc_flags - + let result = + Ext_string.inter2 Literals.dash_nostdlib @@ + match built_in_dependency with + | None -> bsc_flags + | Some {package_install_path} -> + Ext_string.inter3 dash_i (Filename.quote package_install_path) bsc_flags + in + if bs_suffix then Ext_string.inter2 "-bs-suffix" result else result in let reason_react_jsx_flag = match reason_react_jsx with @@ -116,7 +119,7 @@ let output_ninja_and_namespace_map Ext_string.inter2 "-ppx" s in let warnings = Bsb_warning.opt_warning_to_string no_dev warning in - + Bsb_ninja_util.output_kvs [| Bsb_ninja_global_vars.bs_package_flags, bs_package_flags ; diff --git a/jscomp/bsb/bsb_package_specs.ml b/jscomp/bsb/bsb_package_specs.ml index 61c8d23d36..fa3b9ad51c 100644 --- a/jscomp/bsb/bsb_package_specs.ml +++ b/jscomp/bsb/bsb_package_specs.ml @@ -181,9 +181,11 @@ let package_output ({format; in_source } : spec) output= *) let get_list_of_output_js package_specs output_file_sans_extension = - Spec_set.fold (fun format acc -> - package_output format - ( Ext_namespace.js_name_of_basename output_file_sans_extension) - :: acc + Spec_set.fold + (fun format acc -> + package_output format + ( Ext_namespace.js_name_of_basename !Js_config.bs_suffix + output_file_sans_extension) + :: acc ) package_specs [] diff --git a/jscomp/build_tests/namespace/bsconfig.json b/jscomp/build_tests/namespace/bsconfig.json index e3034c630a..612e16e194 100644 --- a/jscomp/build_tests/namespace/bsconfig.json +++ b/jscomp/build_tests/namespace/bsconfig.json @@ -15,6 +15,6 @@ "warnings": { "number" : "-40+6+7", "error" : true - } - + }, + "suffix": ".bs.js" } \ No newline at end of file diff --git a/jscomp/build_tests/namespace/input.js b/jscomp/build_tests/namespace/input.js index a9673d43af..3f66fda379 100644 --- a/jscomp/build_tests/namespace/input.js +++ b/jscomp/build_tests/namespace/input.js @@ -5,7 +5,7 @@ var fs = require('fs') var path = require('path') child_process.execSync(`bsb -clean-world && bsb -make-world`, {cwd:__dirname, stdio:[0,1,2]}) -var x = require('./src/demo.js') +var x = require('./src/demo.bs.js') var assert = require('assert') assert.equal(x.v, 3 ) diff --git a/jscomp/build_tests/namespace/node_modules/liba/bsconfig.json b/jscomp/build_tests/namespace/node_modules/liba/bsconfig.json index 866250ebe4..f88a85e32c 100644 --- a/jscomp/build_tests/namespace/node_modules/liba/bsconfig.json +++ b/jscomp/build_tests/namespace/node_modules/liba/bsconfig.json @@ -5,5 +5,6 @@ "namespace": true, "warnings": { "error" : true - } + }, + "suffix": ".bs.js" } \ No newline at end of file diff --git a/jscomp/build_tests/namespace/src/demo.js b/jscomp/build_tests/namespace/src/demo.bs.js similarity index 74% rename from jscomp/build_tests/namespace/src/demo.js rename to jscomp/build_tests/namespace/src/demo.bs.js index 7ad199436a..c027b6b072 100644 --- a/jscomp/build_tests/namespace/src/demo.js +++ b/jscomp/build_tests/namespace/src/demo.bs.js @@ -1,6 +1,6 @@ 'use strict'; -var Demo$Liba = require("liba/src/demo.js"); +var Demo$Liba = require("liba/src/demo.bs.js"); var Demo$Libb = require("libb/src/demo.js"); var v = Demo$Liba.v + Demo$Libb.v | 0; diff --git a/jscomp/build_tests/namespace/src/main.js b/jscomp/build_tests/namespace/src/main.bs.js similarity index 63% rename from jscomp/build_tests/namespace/src/main.js rename to jscomp/build_tests/namespace/src/main.bs.js index d2f36b501d..6db343da39 100644 --- a/jscomp/build_tests/namespace/src/main.js +++ b/jscomp/build_tests/namespace/src/main.bs.js @@ -1,6 +1,6 @@ 'use strict'; -var Demo$Namespace = require("./demo.js"); +var Demo$Namespace = require("./demo.bs.js"); var a = Demo$Namespace.v; diff --git a/jscomp/common/js_config.ml b/jscomp/common/js_config.ml index b4e3f3bc28..0f8bae958f 100644 --- a/jscomp/common/js_config.ml +++ b/jscomp/common/js_config.ml @@ -109,3 +109,4 @@ let dump_js = ref false let syntax_only = ref false let binary_ast = ref false +let bs_suffix = ref false \ No newline at end of file diff --git a/jscomp/common/js_config.mli b/jscomp/common/js_config.mli index 4a69d92f45..3b97403150 100644 --- a/jscomp/common/js_config.mli +++ b/jscomp/common/js_config.mli @@ -100,3 +100,4 @@ val syntax_only : bool ref val binary_ast : bool ref +val bs_suffix : bool ref \ No newline at end of file diff --git a/jscomp/core/cmjdump_main.ml b/jscomp/core/cmjdump_main.ml index 82986f7786..70e496eb4e 100644 --- a/jscomp/core/cmjdump_main.ml +++ b/jscomp/core/cmjdump_main.ml @@ -24,13 +24,25 @@ let p = Format.fprintf +let pp_cmj_case fmt (cmj_case : Js_cmj_format.cmj_case) = + match cmj_case with + | Little_js -> + p fmt "@[case : little, .js @]@." + | Little_bs -> + p fmt "@[case : little, .bs.js @]@." + | Upper_js -> + p fmt "@[case: upper, .js @]@." + | Upper_bs -> + p fmt "@[case: upper, .bs.js @]@." + let pp_cmj fmt - ({ values ; effect; npm_package_path ; case} :Js_cmj_format.t) = + ({ values ; effect; npm_package_path ; cmj_case} :Js_cmj_format.t) = p fmt "@[package info: %a@]@." Js_packages_info.dump_packages_info npm_package_path - ; - p fmt "@[case:%b@]@." case ; + ; + pp_cmj_case fmt cmj_case; + p fmt "@[effect: %a@]@." (fun fmt o -> match o with None -> () diff --git a/jscomp/core/js_cmj_format.ml b/jscomp/core/js_cmj_format.ml index 4a73434d9d..dd6a8c21fe 100644 --- a/jscomp/core/js_cmj_format.ml +++ b/jscomp/core/js_cmj_format.ml @@ -43,15 +43,16 @@ type effect = string option let single_na = Single NA (** we don't force people to use package *) - +type cmj_case = Ext_namespace.file_kind + type t = { values : cmj_value String_map.t; effect : effect; npm_package_path : Js_packages_info.t ; - case : bool; (* little case -> true *) + cmj_case : cmj_case; } -let cmj_magic_number = "BUCKLE20170907" +let cmj_magic_number = "BUCKLE20171012" let cmj_magic_number_length = String.length cmj_magic_number @@ -60,7 +61,7 @@ let pure_dummy = values = String_map.empty; effect = None; npm_package_path = Js_packages_info.empty; - case = true; + cmj_case = Little_js; } let no_pure_dummy = @@ -68,7 +69,7 @@ let no_pure_dummy = values = String_map.empty; effect = Some Ext_string.empty; npm_package_path = Js_packages_info.empty; - case = true; + cmj_case = Little_js; (** TODO: consistent with Js_config.bs_suffix default *) } diff --git a/jscomp/core/js_cmj_format.mli b/jscomp/core/js_cmj_format.mli index 1a7e8edbea..798fc70d1f 100644 --- a/jscomp/core/js_cmj_format.mli +++ b/jscomp/core/js_cmj_format.mli @@ -64,13 +64,12 @@ type cmj_value = { type effect = string option - - +type cmj_case = Ext_namespace.file_kind type t = { values : cmj_value String_map.t; effect : effect; npm_package_path : Js_packages_info.t; - case : bool; + cmj_case : cmj_case; } val single_na : arity diff --git a/jscomp/core/js_main.ml b/jscomp/core/js_main.ml index e1ba6c3338..1f7d048977 100644 --- a/jscomp/core/js_main.ml +++ b/jscomp/core/js_main.ml @@ -106,6 +106,11 @@ let buckle_script_flags : (string * Arg.spec * string) list = Arg.Unit Reason_outcome_printer_main.setup, " Print compiler output in Reason syntax" ) + :: + ("-bs-suffix", + Arg.Set Js_config.bs_suffix, + " Set suffix to .bs.js" + ) :: ("-bs-no-implicit-include", Arg.Set Clflags.no_implicit_current_dir , " Don't include current dir implicitly") diff --git a/jscomp/core/js_name_of_module_id.ml b/jscomp/core/js_name_of_module_id.ml index 153f3693d9..eef1a5584b 100644 --- a/jscomp/core/js_name_of_module_id.ml +++ b/jscomp/core/js_name_of_module_id.ml @@ -36,7 +36,7 @@ let string_of_module_id id = string_of_module_id_in_browser id #else - let string_of_module_id +let string_of_module_id ~output_dir module_system id diff --git a/jscomp/core/js_packages_info.ml b/jscomp/core/js_packages_info.ml index 5b6d9c0219..6416529ddb 100644 --- a/jscomp/core/js_packages_info.ml +++ b/jscomp/core/js_packages_info.ml @@ -182,7 +182,7 @@ let string_of_module_id (module_system : module_system) (current_package_info : t) (get_package_path_from_cmj : - Lam_module_ident.t -> (string * t * bool) option + Lam_module_ident.t -> (string * t * Ext_namespace.file_kind) option ) (dep_module_id : Lam_module_ident.t) : string = let result = @@ -209,7 +209,7 @@ let string_of_module_id | None -> Bs_exception.error (Missing_ml_dependency dep_module_id.id.name) | Some (cmj_path, package_info, little) -> - let js_file = Ext_namespace.js_name_of_modulename ~little id.name in + let js_file = Ext_namespace.js_name_of_modulename little id.name in let dependency_pkg_info = query_package_infos package_info module_system in diff --git a/jscomp/core/js_packages_info.mli b/jscomp/core/js_packages_info.mli index ec1aed5c23..1628ee415f 100644 --- a/jscomp/core/js_packages_info.mli +++ b/jscomp/core/js_packages_info.mli @@ -67,5 +67,5 @@ val string_of_module_id : module_system -> t -> (Lam_module_ident.t -> - (string * t * bool ) option ) -> + (string * t * Ext_namespace.file_kind) option ) -> Lam_module_ident.t -> string \ No newline at end of file diff --git a/jscomp/core/lam_compile_env.ml b/jscomp/core/lam_compile_env.ml index a27a487815..82325cb3d1 100644 --- a/jscomp/core/lam_compile_env.ml +++ b/jscomp/core/lam_compile_env.ml @@ -139,17 +139,19 @@ let cached_find_ml_id_pos id pos env : ident_info = let name = Ocaml_types.get_name signature pos in let arity, closed_lambda = match String_map.find_opt name cmj_table.values with - | Some {arity ; closed_lambda} -> arity, closed_lambda + | Some {arity ; closed_lambda} -> + arity, + ( if Js_config.get_cross_module_inline () then + closed_lambda + else None + ) | None -> Js_cmj_format.single_na, None in {id; name ; signature ; arity ; - closed_lambda = - if Js_config.get_cross_module_inline () then - closed_lambda - else None + closed_lambda } end | Some (Visit {signature ; cmj_table = { values ; _} } ) @@ -169,7 +171,7 @@ let cached_find_ml_id_pos id pos env : ident_info = signature; arity; closed_lambda - (* TODO shall we cache the arity ?*) + (* TODO shall we cache the arity ?*) } | Some (Runtime _) -> assert false | Some External -> assert false @@ -211,11 +213,11 @@ let query_and_add_if_not_exist (type u) begin match env with | Has_env env -> begin match - Ocaml_types.find_serializable_signatures_by_path ( oid.id) env with + Ocaml_types.find_serializable_signatures_by_path oid.id env with | None -> not_found () (* actually when [not_found] in the call site, we throw... *) | Some signature -> - oid +> Visit {signature = signature; cmj_table;cmj_path } ; - found { signature ; pure = cmj_table.effect = None} + oid +> Visit {signature; cmj_table;cmj_path } ; + found { signature ; pure = (cmj_table.effect = None)} end | No_env -> found cmj_info @@ -239,7 +241,7 @@ let query_and_add_if_not_exist (type u) | Some (Visit {signature ; cmj_table = cmj_table; cmj_path}) -> begin match env with | Has_env _ -> - found { signature = signature ; pure = (cmj_table.effect = None)} + found { signature; pure = (cmj_table.effect = None)} | No_env -> found (cmj_path,cmj_table) end @@ -279,7 +281,7 @@ let get_package_path_from_cmj ) ~found:(fun (cmj_path,x) -> Some (cmj_path, - x.npm_package_path, x.case ) + x.npm_package_path, x.cmj_case ) ) diff --git a/jscomp/core/lam_compile_env.mli b/jscomp/core/lam_compile_env.mli index 274069ac8e..6714f26585 100644 --- a/jscomp/core/lam_compile_env.mli +++ b/jscomp/core/lam_compile_env.mli @@ -112,7 +112,7 @@ val is_pure_module : Lam_module_ident.t -> bool val get_package_path_from_cmj : Lam_module_ident.t -> - (string * Js_packages_info.t * bool) option + (string * Js_packages_info.t * Js_cmj_format.cmj_case) option diff --git a/jscomp/core/lam_compile_main.ml b/jscomp/core/lam_compile_main.ml index 7e6894d92a..f398548b54 100644 --- a/jscomp/core/lam_compile_main.ml +++ b/jscomp/core/lam_compile_main.ml @@ -32,7 +32,18 @@ module E = Js_exp_make module S = Js_stmt_make - open Js_output.Ops +let get_cmj_case output_prefix : Ext_namespace.file_kind = + let little = + Ext_char.is_lower_case (Filename.basename output_prefix).[0] + in + match little, !Js_config.bs_suffix with + | true, true -> Little_bs + | true, false -> Little_js + | false, true -> Upper_bs + | false, false -> Upper_js + + +open Js_output.Ops let compile_group ({filename = file_name; env;} as meta : Lam_stats.t) (x : Lam_group.t) : Js_output.t = @@ -355,7 +366,7 @@ let compile ~filename (output_prefix : string) env _sigs maybe_pure external_module_ids coerced_input.export_map - (Ext_char.is_lower_case (Filename.basename output_prefix).[0]) + (get_cmj_case output_prefix) in (if not @@ !Clflags.dont_write_files then Js_cmj_format.to_file @@ -380,10 +391,10 @@ let lambda_as_module let lambda_output = compile ~filename output_prefix env sigs lam in let basename = (* #758, output_prefix is already chopped *) - Ext_namespace.js_name_of_basename (Filename.basename - output_prefix (* -o *) - (* filename *) (* see #757 *) - ) in + (* -o filename, see #757 *) + Ext_namespace.js_name_of_basename !Js_config.bs_suffix + (Filename.basename + output_prefix) in (* #913 only generate little-case js file *) diff --git a/jscomp/core/lam_stats_export.ml b/jscomp/core/lam_stats_export.ml index 27ec7b6245..3546172070 100644 --- a/jscomp/core/lam_stats_export.ml +++ b/jscomp/core/lam_stats_export.ml @@ -159,7 +159,7 @@ let export_to_cmj maybe_pure external_ids export_map - case + cmj_case : Js_cmj_format.t = let values = values_of_export meta export_map in let () = @@ -172,7 +172,7 @@ let export_to_cmj {values; effect ; npm_package_path = Js_packages_state.get_packages_info (); - case ; + cmj_case ; (* FIXME: make sure [-o] would not change its case add test for ns/non-ns *) diff --git a/jscomp/core/lam_stats_export.mli b/jscomp/core/lam_stats_export.mli index 4c24c217d4..732aaca0b5 100644 --- a/jscomp/core/lam_stats_export.mli +++ b/jscomp/core/lam_stats_export.mli @@ -32,6 +32,6 @@ val export_to_cmj : Js_cmj_format.effect -> Lam_module_ident.t list -> Lam.t Ident_map.t -> - bool -> + Js_cmj_format.cmj_case -> Js_cmj_format.t diff --git a/jscomp/core/ocaml_batch_compile.ml b/jscomp/core/ocaml_batch_compile.ml index 4ef48becad..ac9f101e52 100644 --- a/jscomp/core/ocaml_batch_compile.ml +++ b/jscomp/core/ocaml_batch_compile.ml @@ -44,7 +44,8 @@ let process_result ppf main_file ast_table result = if not (!Clflags.compile_only) then Sys.command ("node " ^ - Ext_namespace.js_name_of_basename (Filename.chop_extension main_file) + Ext_namespace.js_name_of_basename + !Js_config.bs_suffix (Filename.chop_extension main_file) ) else 0 diff --git a/jscomp/ext/ext_namespace.ml b/jscomp/ext/ext_namespace.ml index e214e172f0..e556e08a46 100644 --- a/jscomp/ext/ext_namespace.ml +++ b/jscomp/ext/ext_namespace.ml @@ -48,15 +48,29 @@ let remove_ns_suffix name = if i < 0 then name else String.sub name 0 i +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs -let js_name_of_basename s = - remove_ns_suffix s ^ Literals.suffix_js +let suffix_js = ".js" +let bs_suffix_js = ".bs.js" -let js_name_of_modulename ~little s = - if little then - remove_ns_suffix (String.uncapitalize s) ^ Literals.suffix_js - else - remove_ns_suffix s ^ Literals.suffix_js +let js_name_of_basename bs_suffix s = + remove_ns_suffix s ^ + (if bs_suffix then bs_suffix_js else suffix_js ) + +let js_name_of_modulename little s = + match little with + | Little_js -> + remove_ns_suffix (String.uncapitalize s) ^ suffix_js + | Little_bs -> + remove_ns_suffix (String.uncapitalize s) ^ bs_suffix_js + | Upper_js -> + remove_ns_suffix s ^ suffix_js + | Upper_bs -> + remove_ns_suffix s ^ bs_suffix_js (* https://docs.npmjs.com/files/package.json Some rules: diff --git a/jscomp/ext/ext_namespace.mli b/jscomp/ext/ext_namespace.mli index fa95f532f1..d222c597fe 100644 --- a/jscomp/ext/ext_namespace.mli +++ b/jscomp/ext/ext_namespace.mli @@ -23,25 +23,32 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) (** [make ~ns "a" ] - A typical example would return "a-Ns" - Note the namespace comes from the output of [namespace_of_package_name] + A typical example would return "a-Ns" + Note the namespace comes from the output of [namespace_of_package_name] *) val make : ns:string -> string -> string (* Note we have to output uncapitalized file Name, - or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` - relevant issues: #1609, #913 - - #1933 when removing ns suffix, don't pass the bound - of basename -*) -val js_name_of_basename : string -> string + or at least be consistent, since by reading cmi file on Case insensitive OS, we don't really know it is `list.cmi` or `List.cmi`, so that `require (./list.js)` or `require(./List.js)` + relevant issues: #1609, #913 -(** [js_name_of_modulename ~little A-Ns] + #1933 when removing ns suffix, don't pass the bound + of basename *) -val js_name_of_modulename : little:bool -> string -> string +val js_name_of_basename : + bool -> + string -> string + +type file_kind = + | Upper_js + | Upper_bs + | Little_js + | Little_bs + (** [js_name_of_modulename ~little A-Ns] + *) +val js_name_of_modulename : file_kind -> string -> string (* TODO handle cases like '@angular/core' diff --git a/jscomp/ext/ext_path.ml b/jscomp/ext/ext_path.ml index c5ab6d2b87..6628bb6ae1 100644 --- a/jscomp/ext/ext_path.ml +++ b/jscomp/ext/ext_path.ml @@ -127,6 +127,11 @@ let chop_extension ?(loc="") name = let chop_extension_if_any fname = try Filename.chop_extension fname with Invalid_argument _ -> fname +let rec chop_all_extensions_if_any fname = + match Filename.chop_extension fname with + | x -> chop_all_extensions_if_any x + | exception _ -> fname + let get_extension x = let pos = Ext_string.rindex_neg x '.' in if pos < 0 then "" diff --git a/jscomp/ext/ext_path.mli b/jscomp/ext/ext_path.mli index 036d034152..e9f07e8d02 100644 --- a/jscomp/ext/ext_path.mli +++ b/jscomp/ext/ext_path.mli @@ -44,7 +44,9 @@ val chop_extension : ?loc:string -> string -> string val chop_extension_if_any : string -> string - +val chop_all_extensions_if_any : + string -> string + (** {[ get_extension "a.txt" = ".txt" diff --git a/jscomp/ext/literals.ml b/jscomp/ext/literals.ml index 3f9500f685..47ce84cccd 100644 --- a/jscomp/ext/literals.ml +++ b/jscomp/ext/literals.ml @@ -103,7 +103,7 @@ let suffix_mliast_simple = ".mliast_simple" let suffix_d = ".d" let suffix_mlastd = ".mlast.d" let suffix_mliastd = ".mliast.d" -let suffix_js = ".js" + let commonjs = "commonjs" let amdjs = "amdjs" diff --git a/jscomp/ext/literals.mli b/jscomp/ext/literals.mli index 078742a1e2..d3e3de6980 100644 --- a/jscomp/ext/literals.mli +++ b/jscomp/ext/literals.mli @@ -101,7 +101,7 @@ val suffix_rei : string val suffix_d : string val suffix_mlastd : string val suffix_mliastd : string -val suffix_js : string + val suffix_mli : string val suffix_cmt : string val suffix_cmti : string diff --git a/jscomp/index.html b/jscomp/index.html deleted file mode 100644 index e8459f0f19..0000000000 --- a/jscomp/index.html +++ /dev/null @@ -1,17 +0,0 @@ -
-