@@ -39,6 +39,13 @@ struct PackageToJSError: Swift.Error, CustomStringConvertible {
39
39
}
40
40
}
41
41
42
+ struct TemplateContext {
43
+ let packageId : String
44
+ let packageName : String
45
+ let wasmFilename : String
46
+ let useSharedMemory : Bool
47
+ }
48
+
42
49
/// Plans the build for packaging.
43
50
struct PackagingPlanner {
44
51
/// The options for packaging
@@ -207,19 +214,30 @@ struct PackagingPlanner {
207
214
}
208
215
packageInputs. append ( packageJSON)
209
216
210
- // Copy the template files
211
- for (file, output) in [
212
- ( " Plugins/PackageToJS/Templates/index.js " , " index.js " ) ,
213
- ( " Plugins/PackageToJS/Templates/index.d.ts " , " index.d.ts " ) ,
214
- ( " Plugins/PackageToJS/Templates/instantiate.js " , " instantiate.js " ) ,
215
- ( " Plugins/PackageToJS/Templates/instantiate.d.ts " , " instantiate.d.ts " ) ,
216
- ( " Sources/JavaScriptKit/Runtime/index.mjs " , " runtime.js " ) ,
217
+ // Instantiate the template files
218
+ for (template, output) in [
219
+ ( \TemplateContext . index_js, " index.js " ) ,
220
+ ( \TemplateContext . index_d_ts, " index.d.ts " ) ,
221
+ ( \TemplateContext . instantiate_js, " instantiate.js " ) ,
222
+ ( \TemplateContext . instantiate_d_ts, " instantiate.d.ts " ) ,
217
223
] {
218
224
packageInputs. append ( planCopyTemplateFile (
219
- make: & make, file : file , output: output, outputDirTask: outputDirTask,
225
+ make: & make, template : template , output: output, outputDirTask: outputDirTask,
220
226
inputs: [ ]
221
227
) )
222
228
}
229
+ // Copy files
230
+ for (file, output) in [
231
+ ( " Sources/JavaScriptKit/Runtime/index.mjs " , " runtime.js " ) ,
232
+ ] {
233
+ let inputPath = selfPackageDir. appending ( path: file)
234
+ packageInputs. append ( make. addTask (
235
+ inputFiles: [ selfPath, inputPath. path] , inputTasks: [ outputDirTask] ,
236
+ output: outputDir. appending ( path: output) . path
237
+ ) {
238
+ try Self . syncFile ( from: inputPath. path, to: $0. output)
239
+ } )
240
+ }
223
241
return ( packageInputs, outputDirTask)
224
242
}
225
243
@@ -242,13 +260,13 @@ struct PackagingPlanner {
242
260
allTasks. append ( binDirTask)
243
261
244
262
// Copy the template files
245
- for (file , output) in [
246
- ( " Plugins/PackageToJS/Templates/test.js " , " test.js " ) ,
247
- ( " Plugins/PackageToJS/Templates/test.d.ts " , " test.d.ts " ) ,
248
- ( " Plugins/PackageToJS/Templates/bin/test.js " , " bin/test.js " ) ,
263
+ for (template , output) in [
264
+ ( \ TemplateContext . index_js , " test.js " ) ,
265
+ ( \ TemplateContext . index_d_ts , " test.d.ts " ) ,
266
+ ( \ TemplateContext . bin_test_js , " bin/test.js " ) ,
249
267
] {
250
268
allTasks. append ( planCopyTemplateFile (
251
- make: & make, file : file , output: output, outputDirTask: outputDirTask,
269
+ make: & make, template : template , output: output, outputDirTask: outputDirTask,
252
270
inputs: [ binDirTask]
253
271
) )
254
272
}
@@ -260,22 +278,20 @@ struct PackagingPlanner {
260
278
261
279
private func planCopyTemplateFile(
262
280
make: inout MiniMake ,
263
- file : String ,
281
+ template : KeyPath < TemplateContext , String > ,
264
282
output: String ,
265
283
outputDirTask: MiniMake . TaskKey ,
266
284
inputs: [ MiniMake . TaskKey ]
267
285
) -> MiniMake . TaskKey {
268
- let inputPath = selfPackageDir . appending ( path : file )
269
- let substitutions = [
270
- " @PACKAGE_TO_JS_MODULE_PATH@ " : wasmFilename
271
- ]
286
+ let context = TemplateContext (
287
+ packageId : packageId , packageName : options . packageName ?? packageId . lowercased ( ) ,
288
+ wasmFilename : wasmFilename, useSharedMemory : false
289
+ )
272
290
return make. addTask (
273
- inputFiles: [ selfPath, inputPath . path ] , inputTasks: [ outputDirTask] + inputs,
291
+ inputFiles: [ selfPath] , inputTasks: [ outputDirTask] + inputs,
274
292
output: outputDir. appending ( path: output) . path
275
293
) {
276
- var content = try String ( contentsOf: inputPath, encoding: . utf8)
277
- let options = PreprocessOptions ( substitutions: substitutions)
278
- content = try preprocess ( source: content, options: options)
294
+ let content = context [ keyPath: template]
279
295
try content. write ( toFile: $0. output, atomically: true , encoding: . utf8)
280
296
}
281
297
}
0 commit comments