@@ -39,6 +39,13 @@ struct PackageToJSError: Swift.Error, CustomStringConvertible {
3939 }
4040}
4141
42+ struct TemplateContext {
43+ let packageId : String
44+ let packageName : String
45+ let wasmFilename : String
46+ let useSharedMemory : Bool
47+ }
48+
4249/// Plans the build for packaging.
4350struct PackagingPlanner {
4451 /// The options for packaging
@@ -207,19 +214,30 @@ struct PackagingPlanner {
207214 }
208215 packageInputs. append ( packageJSON)
209216
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 " ) ,
217223 ] {
218224 packageInputs. append ( planCopyTemplateFile (
219- make: & make, file : file , output: output, outputDirTask: outputDirTask,
225+ make: & make, template : template , output: output, outputDirTask: outputDirTask,
220226 inputs: [ ]
221227 ) )
222228 }
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+ }
223241 return ( packageInputs, outputDirTask)
224242 }
225243
@@ -242,13 +260,13 @@ struct PackagingPlanner {
242260 allTasks. append ( binDirTask)
243261
244262 // 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 " ) ,
249267 ] {
250268 allTasks. append ( planCopyTemplateFile (
251- make: & make, file : file , output: output, outputDirTask: outputDirTask,
269+ make: & make, template : template , output: output, outputDirTask: outputDirTask,
252270 inputs: [ binDirTask]
253271 ) )
254272 }
@@ -260,22 +278,20 @@ struct PackagingPlanner {
260278
261279 private func planCopyTemplateFile(
262280 make: inout MiniMake ,
263- file : String ,
281+ template : KeyPath < TemplateContext , String > ,
264282 output: String ,
265283 outputDirTask: MiniMake . TaskKey ,
266284 inputs: [ MiniMake . TaskKey ]
267285 ) -> 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+ )
272290 return make. addTask (
273- inputFiles: [ selfPath, inputPath . path ] , inputTasks: [ outputDirTask] + inputs,
291+ inputFiles: [ selfPath] , inputTasks: [ outputDirTask] + inputs,
274292 output: outputDir. appending ( path: output) . path
275293 ) {
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]
279295 try content. write ( toFile: $0. output, atomically: true , encoding: . utf8)
280296 }
281297 }
0 commit comments