@@ -1668,37 +1668,61 @@ let generate js_gen com =
16681668 Gctx. map_source_header com.defines (fun s -> print ctx " // %s\n " s);
16691669
16701670 let import_statements = ref [] in
1671- let () =
1672- List. iter (fun mt -> match mt with
1673- | TClassDecl c when (has_class_flag c CExtern ) && Meta. has Meta. JsImport c.cl_meta && is_directly_used ctx.com c.cl_meta ->
1674- let _, args, mp = Meta. get Meta. JsImport c.cl_meta in
1675- let id = snd c.cl_path in
1676- let flat_path = Path. flat_path (get_generated_class_path c) in
1677- let alias = try
1678- fst (Native. get_native_name c.cl_meta)
1679- with Not_found ->
1680- c.cl_meta < - (Meta. Native ,[EConst (String (flat_path,SDoubleQuotes )),null_pos],null_pos) :: c.cl_meta;
1681- Native. apply_native_paths mt;
1682- flat_path in
1683- (match args with
1671+ List. iter (fun mt -> match mt with
1672+ | TClassDecl c when (has_class_flag c CExtern ) && Meta. has Meta. JsImport c.cl_meta && is_directly_used ctx.com c.cl_meta ->
1673+ let _, args, mp = Meta. get Meta. JsImport c.cl_meta in
1674+ let id = snd c.cl_path in
1675+ let flat_path = Path. flat_path (get_generated_class_path c) in
1676+ let alias = try
1677+ fst (Native. get_native_name c.cl_meta)
1678+ with Not_found ->
1679+ c.cl_meta < - (Meta. Native ,[EConst (String (flat_path,SDoubleQuotes )),null_pos],null_pos) :: c.cl_meta;
1680+ Native. apply_native_paths mt;
1681+ flat_path
1682+ in
1683+ let err () =
1684+ abort (" Unsupported @:js.import format. Use:\n " ^
1685+ " @:js.import('mylib.js')\n " ^
1686+ " @:js.import('mylib.js', 'name')\n " ^
1687+ " @:js.import(@default 'mylib.js')\n " ^
1688+ " @:js.import(@star 'mylib.js')\n " ^
1689+ " 'mylib.js' can also be replaced with mylibjs_path custom define,\n if you set it as -D mylibjs_path=\" mylib.js\" " )
1690+ mp
1691+ in
1692+ let get_module_name expr =
1693+ match expr with
1694+ | EConst (String(module_name ,_ )),_ -> module_name
1695+ | EConst (Ident(define )),_ ->
1696+ begin try
1697+ let s = Define. raw_defined_value com.defines define in
1698+ Helper. unquote s
1699+ with Not_found ->
1700+ abort (" Define " ^ define ^ " with js library path is not specified" ) mp;
1701+ end
1702+ | _ -> err()
1703+ in
1704+ (match args with
16841705 (* @:js.import(@star "module") - namespace import * )
1685- | [EMeta ((Meta. Custom " star" ,[] ,_),(EConst (String (module_name,_)),_)),_] ->
1706+ | [EMeta ((Meta. Custom " star" ,[] ,_),ename),_] ->
1707+ let module_name = get_module_name ename in
16861708 import_statements := (Printf. sprintf " import * as %s from \" %s\" ;" alias module_name) :: ! import_statements
16871709 (* @:js.import(@default "module") - default import *)
1688- | [EMeta ((Meta. Custom " default" ,[] ,_),(EConst (String (module_name,_)),_)),_] ->
1710+ | [EMeta ((Meta. Custom " default" ,[] ,_),ename),_] ->
1711+ let module_name = get_module_name ename in
16891712 import_statements := (Printf. sprintf " import %s from \" %s\" ;" alias module_name) :: ! import_statements
16901713 (* @:js.import("module") - named import using class name *)
1691- | [(EConst (String (module_name,_)),_)] ->
1714+ | [ename] ->
1715+ let module_name = get_module_name ename in
16921716 import_statements := (Printf. sprintf " import { %s as %s } from \" %s\" ;" id alias module_name) :: ! import_statements
16931717 (* @:js.import("module", "exportName") - named import with alias *)
1694- | [(EConst (String (module_name,_)),_); (EConst (String (export_name,_)),_)] ->
1718+ | [ename; (EConst (String (export_name,_)),_)] ->
1719+ let module_name = get_module_name ename in
16951720 import_statements := (Printf. sprintf " import { %s as %s } from \" %s\" ;" export_name alias module_name) :: ! import_statements
16961721 | exprs ->
1697- abort " Unsupported @:js.import format. Use: @:js.import('module'), @:js.import('module', 'name'), @:js.import(@default 'module'), or @:js.import(@star 'module')" mp
1698- )
1699- | _ -> ()
1700- ) com.types;
1701- in
1722+ err ()
1723+ )
1724+ | _ -> ()
1725+ ) com.types;
17021726 (match ! import_statements with
17031727 | [] -> ()
17041728 | lines ->
0 commit comments