@@ -32,14 +32,19 @@ func relativeFilePath(a, b string) (r string, e error) {
32
32
return
33
33
}
34
34
35
- func commonJSImports (conf * config.Config , c * code , tsFilename string ) {
35
+ func commonJSImports (conf * config.Config , c * code , tsFilename string , code string ) {
36
36
packageNames := make ([]string , 0 , len (conf .Mappings ))
37
37
for packageName := range conf .Mappings {
38
38
packageNames = append (packageNames , packageName )
39
39
}
40
40
sort .Strings (packageNames )
41
41
for _ , packageName := range packageNames {
42
42
importMapping := conf .Mappings [packageName ]
43
+
44
+ if len (code ) > 0 && ! strings .Contains (code , importMapping .TypeScriptModule + "." ) {
45
+ continue
46
+ }
47
+
43
48
relativePath , relativeErr := relativeFilePath (tsFilename , importMapping .Out )
44
49
if relativeErr != nil {
45
50
fmt .Println ("can not derive a relative path between" , tsFilename , "and" , importMapping .Out , relativeErr )
@@ -141,10 +146,12 @@ func Build(conf *config.Config, goPath string) { //nolint:maintidx
141
146
_ , _ = fmt .Fprintln (os .Stderr , " could not generate ts code" , err )
142
147
os .Exit (3 )
143
148
}
144
- tsClientCode := newCode (" " )
145
- commonJSImports (conf , tsClientCode , target .Out )
146
- tsClientCode .l ("" ).l ("" )
147
- ts = tsClientCode .string () + ts
149
+
150
+ // workaround to remove unneeded imports
151
+ importsCode := newCode (" " )
152
+ commonJSImports (conf , importsCode , target .Out , ts )
153
+ importsCode .l ("" ).l ("" )
154
+ ts = importsCode .string () + ts
148
155
149
156
// _, _ = fmt.Fprintln(os.Stdout, ts)
150
157
updateErr := updateCode (target .Out , getTSHeaderComment ()+ ts )
@@ -238,8 +245,6 @@ func Build(conf *config.Config, goPath string) { //nolint:maintidx
238
245
moduleCode := newCode (" " )
239
246
structIndent := - 3
240
247
241
- commonJSImports (conf , moduleCode , mapping .Out )
242
-
243
248
var structNames []string
244
249
245
250
for structName := range mappedStructsMap {
@@ -254,7 +259,14 @@ func Build(conf *config.Config, goPath string) { //nolint:maintidx
254
259
}
255
260
256
261
moduleCode .l ("// end of common js" )
257
- updateErr := updateCode (mapping .Out , getTSHeaderComment ()+ moduleCode .string ())
262
+
263
+ // workaround to remove unneeded imports
264
+ importsCode := newCode (" " )
265
+ commonJSImports (conf , importsCode , mapping .Out , moduleCode .string ())
266
+ importsCode .l ("" ).l ("" )
267
+ ts := importsCode .string () + moduleCode .string ()
268
+
269
+ updateErr := updateCode (mapping .Out , getTSHeaderComment ()+ ts )
258
270
if updateErr != nil {
259
271
_ , _ = fmt .Fprintln (os .Stderr , " failed to update code in" , mapping .Out , updateErr )
260
272
}
0 commit comments