@@ -7,19 +7,19 @@ import Foundation
77final class ImportedJSModuleRegistry {
88 /// A JavaScript module that imported declarations are read from.
99 ///
10- /// A `local ` reference is a file inside a Swift target, which packaging copies
11- /// into the generated output. A `bare ` reference is a specifier resolved by the
12- /// JavaScript host (a bundler, an import map, or Node's `node_modules` lookup),
13- /// so it has no file and nothing to copy. Because a bare specifier names the
14- /// same module no matter which Swift module mentions it — and ECMAScript caches
15- /// module instances — it is keyed by specifier alone and shared across targets.
10+ /// A `snippet ` reference is a file inside a Swift target, which packaging copies
11+ /// into the generated output. A `module ` reference is a bare specifier resolved by
12+ /// the JavaScript host (a bundler, an import map, or Node's `node_modules` lookup),
13+ /// so it has no file and nothing to copy. Because a bare specifier names the same
14+ /// module no matter which Swift module mentions it — and ECMAScript caches module
15+ /// instances — it is keyed by specifier alone and shared across targets.
1616 enum Reference : Hashable {
17- case local ( swiftModuleName: String , path: String )
18- case bare ( specifier: String )
17+ case snippet ( swiftModuleName: String , path: String )
18+ case module ( specifier: String )
1919 }
2020
21- /// A target-local JavaScript file that packaging must copy into the output.
22- struct LocalModule : Hashable {
21+ /// A JavaScript file shipped in a Swift target that packaging must copy into the output.
22+ struct SnippetFile : Hashable {
2323 let swiftModuleName : String
2424 let path : String
2525
@@ -40,11 +40,11 @@ final class ImportedJSModuleRegistry {
4040 private var bindings : [ Reference : Binding ] = [ : ]
4141 private( set) var references : [ Reference ] = [ ]
4242
43- /// The target-local files packaging must copy, in deterministic order.
44- var localModules : [ LocalModule ] {
43+ /// The snippet files packaging must copy, in deterministic order.
44+ var snippetFiles : [ SnippetFile ] {
4545 references. compactMap { reference in
46- guard case . local ( let swiftModuleName, let path) = reference else { return nil }
47- return LocalModule ( swiftModuleName: swiftModuleName, path: path)
46+ guard case . snippet ( let swiftModuleName, let path) = reference else { return nil }
47+ return SnippetFile ( swiftModuleName: swiftModuleName, path: path)
4848 }
4949 }
5050
@@ -82,18 +82,18 @@ final class ImportedJSModuleRegistry {
8282 return references. sorted ( by: isOrderedBefore)
8383 }
8484
85- static func collectLocalModules ( skeletons: [ BridgeJSSkeleton ] ) -> [ LocalModule ] {
85+ static func collectSnippetFiles ( skeletons: [ BridgeJSSkeleton ] ) -> [ SnippetFile ] {
8686 collectReferences ( skeletons: skeletons) . compactMap { reference in
87- guard case . local ( let swiftModuleName, let path) = reference else { return nil }
88- return LocalModule ( swiftModuleName: swiftModuleName, path: path)
87+ guard case . snippet ( let swiftModuleName, let path) = reference else { return nil }
88+ return SnippetFile ( swiftModuleName: swiftModuleName, path: path)
8989 }
9090 }
9191
9292 /// Visits every module origin mentioned by the skeleton, whether or not code
9393 /// generation looks a member up on it.
9494 ///
95- /// This is what decides which modules are imported at all, and for local paths
96- /// which files packaging copies. It stays broader than `forEachMemberLookup` so
95+ /// This is what decides which modules are imported at all, and for snippets which
96+ /// files packaging copies. It stays broader than `forEachMemberLookup` so
9797 /// that a module mentioned only by a wrapper-only `@JSClass` is still imported,
9898 /// preserving its side effects.
9999 private static func forEachOrigin(
@@ -146,22 +146,25 @@ final class ImportedJSModuleRegistry {
146146 }
147147
148148 private static func reference( swiftModuleName: String , from: JSImportFrom ? ) -> Reference ? {
149- guard let specifier = from? . moduleSpecifier else { return nil }
150- if let path = from? . localModulePath {
151- return . local( swiftModuleName: swiftModuleName, path: path)
149+ switch from {
150+ case . snippet( let path) :
151+ return . snippet( swiftModuleName: swiftModuleName, path: path)
152+ case . module( let specifier) :
153+ return . module( specifier: specifier)
154+ case . global, nil :
155+ return nil
152156 }
153- return . bare( specifier: specifier)
154157 }
155158
156159 private static func isOrderedBefore( _ lhs: Reference , _ rhs: Reference ) -> Bool {
157160 switch ( lhs, rhs) {
158- case ( . local ( let lhsModule, let lhsPath) , . local ( let rhsModule, let rhsPath) ) :
161+ case ( . snippet ( let lhsModule, let lhsPath) , . snippet ( let rhsModule, let rhsPath) ) :
159162 return ( lhsModule, lhsPath) < ( rhsModule, rhsPath)
160- case ( . bare ( let lhsSpecifier) , . bare ( let rhsSpecifier) ) :
163+ case ( . module ( let lhsSpecifier) , . module ( let rhsSpecifier) ) :
161164 return lhsSpecifier < rhsSpecifier
162- case ( . local , . bare ) :
165+ case ( . snippet , . module ) :
163166 return true
164- case ( . bare , . local ) :
167+ case ( . module , . snippet ) :
165168 return false
166169 }
167170 }
@@ -185,12 +188,13 @@ final class ImportedJSModuleRegistry {
185188 objectExpr: " globalThis " ,
186189 propertyName: memberName
187190 )
188- case . module ( let specifier ) :
191+ case . snippet , . module :
189192 guard let reference = Self . reference ( swiftModuleName: swiftModuleName, from: from) ,
190193 let binding = bindings [ reference]
191194 else {
192195 throw BridgeJSLinkError (
193- message: " Missing JavaScript module \( swiftModuleName) \( specifier) "
196+ message:
197+ " Missing JavaScript module \( swiftModuleName) \( from? . snippetPath ?? from? . moduleSpecifier ?? " " ) "
194198 )
195199 }
196200 if binding. usesNamedImports {
@@ -216,11 +220,11 @@ final class ImportedJSModuleRegistry {
216220 guard let binding = bindings [ reference] else { return nil }
217221 let specifier : String
218222 switch reference {
219- case . local ( let swiftModuleName, let path) :
220- let output = LocalModule ( swiftModuleName: swiftModuleName, path: path) . relativeOutputPath
223+ case . snippet ( let swiftModuleName, let path) :
224+ let output = SnippetFile ( swiftModuleName: swiftModuleName, path: path) . relativeOutputPath
221225 specifier = " ./ " + BridgeJSLink. escapeForJavaScriptStringLiteral ( output)
222- case . bare ( let bareSpecifier ) :
223- specifier = BridgeJSLink . escapeForJavaScriptStringLiteral ( bareSpecifier )
226+ case . module ( let moduleSpecifier ) :
227+ specifier = BridgeJSLink . escapeForJavaScriptStringLiteral ( moduleSpecifier )
224228 }
225229 guard binding. usesNamedImports else {
226230 return " import * as \( Self . namespaceAlias ( index: binding. index) ) from \" \( specifier) \" ; "
0 commit comments