@@ -24,12 +24,24 @@ public LoadedModuleDependencies(Settings settings, List<ModuleDependency> depend
2424 final ObjectMapper objectMapper = Utils .getObjectMapper ();
2525 final Map <String , ModuleDependency > importFromMap = new LinkedHashMap <>();
2626 final Map <String , ModuleDependency > importAsMap = new LinkedHashMap <>();
27+ final Map <String , ModuleDependency > globalTypeNames = new LinkedHashMap <>();
2728 for (ModuleDependency dependency : dependencies ) {
2829 try {
2930 final Function <String , String > reportNullParameter = parameterName ->
3031 String .format ("Missing required configuration parameter '%s' in module dependency: %s" , parameterName , dependency );
31- Objects .requireNonNull (dependency .importFrom , () -> reportNullParameter .apply ("importFrom" ));
32- Objects .requireNonNull (dependency .importAs , () -> reportNullParameter .apply ("importAs" ));
32+ if (dependency .global ) {
33+ if (dependency .importFrom != null ) {
34+ throw new RuntimeException (String .format (
35+ "'importFrom' parameter is only applicable when 'global' is not set to 'true' (at module dependency %s)." , dependency ));
36+ }
37+ if (dependency .importAs != null ) {
38+ throw new RuntimeException (String .format (
39+ "'importAs' parameter is only applicable when 'global' is not set to 'true' (at module dependency %s)." , dependency ));
40+ }
41+ } else {
42+ Objects .requireNonNull (dependency .importFrom , () -> reportNullParameter .apply ("importFrom" ));
43+ Objects .requireNonNull (dependency .importAs , () -> reportNullParameter .apply ("importAs" ));
44+ }
3345 Objects .requireNonNull (dependency .infoJson , () -> reportNullParameter .apply ("infoJson" ));
3446 if (settings .generateNpmPackageJson ) {
3547 Objects .requireNonNull (dependency .npmPackageName , () -> reportNullParameter .apply ("npmPackageName" ));
@@ -46,27 +58,36 @@ public LoadedModuleDependencies(Settings settings, List<ModuleDependency> depend
4658 }
4759
4860 TypeScriptGenerator .getLogger ().info (String .format (
49- "Loading '%s' module info from: %s" , dependency .importFrom , dependency .infoJson ));
61+ "Loading %s module info from: %s" , dependency .toShortString () , dependency .infoJson ));
5062
51- final ModuleDependency importFromConflict = importFromMap .put (dependency .importFrom , dependency );
52- if (importFromConflict != null ) {
53- throw new RuntimeException (String .format ("Duplicate module '%s'" , dependency .importFrom ));
54- }
63+ if (!dependency .global ) {
64+ final ModuleDependency importFromConflict = importFromMap .put (dependency .importFrom , dependency );
65+ if (importFromConflict != null ) {
66+ throw new RuntimeException (String .format ("Duplicate module '%s'" , dependency .importFrom ));
67+ }
5568
56- final ModuleDependency importAsConflict = importAsMap .put (dependency .importAs , dependency );
57- if (importAsConflict != null ) {
58- throw new RuntimeException (String .format ("Import identifier '%s' already used for module '%s'" , dependency .importAs , importAsConflict .importFrom ));
69+ final ModuleDependency importAsConflict = importAsMap .put (dependency .importAs , dependency );
70+ if (importAsConflict != null ) {
71+ throw new RuntimeException (String .format ("Import identifier '%s' already used for module '%s'" , dependency .importAs , importAsConflict .importFrom ));
72+ }
5973 }
6074
6175 final InfoJson infoJson = objectMapper .readValue (dependency .infoJson , InfoJson .class );
6276 for (InfoJson .ClassInfo classInfo : infoJson .classes ) {
6377 final Pair <ModuleDependency , String > presentMapping = classMappings .get (classInfo .javaClass );
6478 if (presentMapping != null ) {
6579 TypeScriptGenerator .getLogger ().warning (String .format (
66- "Java class '%s' already present in module '%s'" , classInfo .javaClass , presentMapping .getValue1 ().importFrom ));
80+ "Java class '%s' already present in '%s'" , classInfo .javaClass , presentMapping .getValue1 ().infoJson ));
6781 } else {
6882 classMappings .put (classInfo .javaClass , Pair .of (dependency , classInfo .typeName ));
6983 }
84+ final ModuleDependency presentTypeName = globalTypeNames .get (classInfo .typeName );
85+ if (presentTypeName != null ) {
86+ throw new RuntimeException (String .format (
87+ "Duplicate TypeScript global name '%s', declared in '%s' and also '%s'" , classInfo .typeName , presentTypeName .infoJson , dependency .infoJson ));
88+ } else {
89+ globalTypeNames .put (classInfo .typeName , dependency );
90+ }
7091 }
7192 } catch (IOException e ) {
7293 throw new RuntimeException (e );
0 commit comments