@@ -1677,6 +1677,43 @@ let generate js_gen com =
16771677
16781678 let ctx = alloc_ctx com es_version in
16791679 Gctx. map_source_header com.defines (fun s -> print ctx " // %s\n " s);
1680+
1681+ let import_statements = ref [] in
1682+ let () =
1683+ List. iter (fun mt -> match mt with
1684+ | TClassDecl c when (has_class_flag c CExtern ) && Meta. has Meta. JsImport c.cl_meta && is_directly_used ctx.com c.cl_meta ->
1685+ let _, args, mp = Meta. get Meta. JsImport c.cl_meta in
1686+ let id = s_path ctx (get_generated_class_path c) in
1687+ (match args with
1688+ (* @:js.import(@star "module") - namespace import * )
1689+ | [EMeta ((Meta. Custom " star" ,[] ,_),(EConst (String (module_name,_)),_)),_] ->
1690+ import_statements := (Printf. sprintf " import * as %s from \" %s\" ;" id module_name) :: ! import_statements
1691+ (* @:js.import(@default "module") - default import *)
1692+ | [EMeta ((Meta. Custom " default" ,[] ,_),(EConst (String (module_name,_)),_)),_] ->
1693+ import_statements := (Printf. sprintf " import %s from \" %s\" ;" id module_name) :: ! import_statements
1694+ (* @:js.import("module") - named import using class name *)
1695+ | [(EConst (String (module_name,_)),_)] ->
1696+ import_statements := (Printf. sprintf " import { %s } from \" %s\" ;" id module_name) :: ! import_statements
1697+ (* @:js.import("module", "exportName") - named import with alias *)
1698+ | [(EConst (String (module_name,_)),_); (EConst (String (export_name,_)),_)] ->
1699+ if export_name = id then
1700+ import_statements := (Printf. sprintf " import { %s } from \" %s\" ;" id module_name) :: ! import_statements
1701+ else
1702+ import_statements := (Printf. sprintf " import { %s as %s } from \" %s\" ;" export_name id module_name) :: ! import_statements
1703+ | exprs ->
1704+ abort " Unsupported @:js.import format. Use: @:js.import('module'), @:js.import('module', 'name'), @:js.import(@default 'module'), or @:js.import(@star 'module')" mp
1705+ )
1706+ | _ -> ()
1707+ ) com.types;
1708+ in
1709+ (match ! import_statements with
1710+ | [] -> ()
1711+ | lines ->
1712+ List. iter (fun line ->
1713+ print ctx " %s\n " line
1714+ ) (List. rev lines)
1715+ );
1716+
16801717 if has_feature ctx " Class" || has_feature ctx " Type.getClassName" then add_feature ctx " js.Boot.isClass" ;
16811718 if has_feature ctx " Enum" || has_feature ctx " Type.getEnumName" then add_feature ctx " js.Boot.isEnum" ;
16821719
0 commit comments