@@ -3,42 +3,41 @@ import BridgeJSSkeleton
33#endif
44
55final class ImportedJSModuleRegistry {
6- private struct Key : Hashable {
6+ struct Reference : Hashable {
77 let swiftModuleName : String
88 let path : String
9+
10+ var relativeOutputPath : String {
11+ " bridge-js-modules/ \( swiftModuleName) \( path) "
12+ }
913 }
1014
11- private var aliases : [ Key : String ] = [ : ]
12- private( set) var artifacts : [ BridgeJSLinkOutput . Module ] = [ ]
15+ private var aliases : [ Reference : String ] = [ : ]
16+ private( set) var references : [ Reference ] = [ ]
1317
14- func configure( skeletons: [ BridgeJSSkeleton ] ) throws {
18+ func configure( skeletons: [ BridgeJSSkeleton ] ) {
1519 aliases. removeAll ( keepingCapacity: true )
16- artifacts. removeAll ( keepingCapacity: true )
20+ references = Self . collectReferences ( skeletons: skeletons)
21+ for (index, reference) in references. enumerated ( ) {
22+ aliases [ reference] = " __bjs_imported_module_ \( index) "
23+ }
24+ }
1725
18- var sources : [ Key : String ] = [ : ]
26+ static func collectReferences( skeletons: [ BridgeJSSkeleton ] ) -> [ Reference ] {
27+ var references = Set < Reference > ( )
1928 for skeleton in skeletons {
20- for module in skeleton. imported? . modules ?? [ ] {
21- guard module. path. hasPrefix ( " / " ) else {
22- throw BridgeJSLinkError (
23- message: " JavaScript module path must start with '/': \( module. path) "
24- )
25- }
26- let key = Key ( swiftModuleName: skeleton. moduleName, path: module. path)
27- if let existing = sources [ key] , existing != module. source {
28- throw BridgeJSLinkError (
29- message: " Conflicting JavaScript module contents for \( skeleton. moduleName) / \( module. path) "
30- )
29+ for file in skeleton. imported? . children ?? [ ] {
30+ let origins =
31+ file. functions. compactMap ( \. from)
32+ + file. globalGetters. compactMap ( \. from)
33+ + file. types. compactMap ( \. from)
34+ for case . module( let path) in origins {
35+ references. insert ( Reference ( swiftModuleName: skeleton. moduleName, path: path) )
3136 }
32- sources [ key] = module. source
3337 }
3438 }
35-
36- for (index, entry) in sources. sorted ( by: {
37- ( $0. key. swiftModuleName, $0. key. path) < ( $1. key. swiftModuleName, $1. key. path)
38- } ) . enumerated ( ) {
39- let relativePath = " bridge-js-modules/ \( entry. key. swiftModuleName) \( entry. key. path) "
40- aliases [ entry. key] = " __bjs_imported_module_ \( index) "
41- artifacts. append ( . init( relativePath: relativePath, source: entry. value) )
39+ return references. sorted {
40+ ( $0. swiftModuleName, $0. path) < ( $1. swiftModuleName, $1. path)
4241 }
4342 }
4443
@@ -49,19 +48,19 @@ final class ImportedJSModuleRegistry {
4948 case . global:
5049 return " globalThis "
5150 case . module( let path) :
52- let key = Key ( swiftModuleName: swiftModuleName, path: path)
53- guard let alias = aliases [ key ] else {
51+ let reference = Reference ( swiftModuleName: swiftModuleName, path: path)
52+ guard let alias = aliases [ reference ] else {
5453 throw BridgeJSLinkError (
55- message: " Missing embedded JavaScript module for \( swiftModuleName) / \( path) "
54+ message: " Missing JavaScript module \( swiftModuleName) \( path) "
5655 )
5756 }
5857 return alias
5958 }
6059 }
6160
6261 var importLines : [ String ] {
63- artifacts . enumerated ( ) . map { index, artifact in
64- let path = BridgeJSLink . escapeForJavaScriptStringLiteral ( artifact . relativePath )
62+ references . enumerated ( ) . map { index, reference in
63+ let path = BridgeJSLink . escapeForJavaScriptStringLiteral ( reference . relativeOutputPath )
6564 return " import * as __bjs_imported_module_ \( index) from \" ./ \( path) \" ; "
6665 }
6766 }
0 commit comments