@@ -39,13 +39,6 @@ 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-
4942/// Plans the build for packaging.
5043struct PackagingPlanner {
5144 /// The options for packaging
@@ -194,30 +187,41 @@ struct PackagingPlanner {
194187 }
195188 packageInputs. append ( wasm)
196189
197- // Instantiate the template files
198- for (template, output) in [
199- ( \TemplateContext . package_json, " package.json " ) ,
200- ( \TemplateContext . index_js, " index.js " ) ,
201- ( \TemplateContext . index_d_ts, " index.d.ts " ) ,
202- ( \TemplateContext . instantiate_js, " instantiate.js " ) ,
203- ( \TemplateContext . instantiate_d_ts, " instantiate.d.ts " ) ,
204- ] {
205- packageInputs. append ( planCopyTemplateFile (
206- make: & make, template: template, output: output, outputDirTask: outputDirTask,
207- inputs: [ ]
208- ) )
190+ // Write package.json
191+ let packageJSON = make. addTask (
192+ inputFiles: [ selfPath] , inputTasks: [ outputDirTask] ,
193+ output: outputDir. appending ( path: " package.json " ) . path
194+ ) {
195+ let packageJSON = """
196+ {
197+ " name " : " \( options. packageName ?? packageId. lowercased ( ) ) " ,
198+ " version " : " 0.0.0 " ,
199+ " type " : " module " ,
200+ " exports " : {
201+ " . " : " ./index.js " ,
202+ " ./wasm " : " ./ \( wasmFilename) "
203+ },
204+ " dependencies " : {
205+ " @bjorn3/browser_wasi_shim " : " ^0.4.1 "
206+ }
207+ }
208+ """
209+ try packageJSON. write ( toFile: $0. output, atomically: true , encoding: . utf8)
209210 }
210- // Copy files
211+ packageInputs. append ( packageJSON)
212+
213+ // Copy the template files
211214 for (file, output) in [
215+ ( " Plugins/PackageToJS/Templates/index.js " , " index.js " ) ,
216+ ( " Plugins/PackageToJS/Templates/index.d.ts " , " index.d.ts " ) ,
217+ ( " Plugins/PackageToJS/Templates/instantiate.js " , " instantiate.js " ) ,
218+ ( " Plugins/PackageToJS/Templates/instantiate.d.ts " , " instantiate.d.ts " ) ,
212219 ( " Sources/JavaScriptKit/Runtime/index.mjs " , " runtime.js " ) ,
213220 ] {
214- let inputPath = selfPackageDir. appending ( path: file)
215- packageInputs. append ( make. addTask (
216- inputFiles: [ selfPath, inputPath. path] , inputTasks: [ outputDirTask] ,
217- output: outputDir. appending ( path: output) . path
218- ) {
219- try Self . syncFile ( from: inputPath. path, to: $0. output)
220- } )
221+ packageInputs. append ( planCopyTemplateFile (
222+ make: & make, file: file, output: output, outputDirTask: outputDirTask,
223+ inputs: [ ]
224+ ) )
221225 }
222226 return ( packageInputs, outputDirTask)
223227 }
@@ -241,13 +245,13 @@ struct PackagingPlanner {
241245 allTasks. append ( binDirTask)
242246
243247 // Copy the template files
244- for (template , output) in [
245- ( \ TemplateContext . index_js , " test.js " ) ,
246- ( \ TemplateContext . index_d_ts , " test.d.ts " ) ,
247- ( \ TemplateContext . bin_test_js , " bin/test.js " ) ,
248+ for (file , output) in [
249+ ( " Plugins/PackageToJS/Templates/test.js " , " test.js " ) ,
250+ ( " Plugins/PackageToJS/Templates/test.d.ts " , " test.d.ts " ) ,
251+ ( " Plugins/PackageToJS/Templates/bin/test.js " , " bin/test.js " ) ,
248252 ] {
249253 allTasks. append ( planCopyTemplateFile (
250- make: & make, template : template , output: output, outputDirTask: outputDirTask,
254+ make: & make, file : file , output: output, outputDirTask: outputDirTask,
251255 inputs: [ binDirTask]
252256 ) )
253257 }
@@ -259,20 +263,22 @@ struct PackagingPlanner {
259263
260264 private func planCopyTemplateFile(
261265 make: inout MiniMake ,
262- template : KeyPath < TemplateContext , String > ,
266+ file : String ,
263267 output: String ,
264268 outputDirTask: MiniMake . TaskKey ,
265269 inputs: [ MiniMake . TaskKey ]
266270 ) -> MiniMake . TaskKey {
267- let context = TemplateContext (
268- packageId : packageId , packageName : options . packageName ?? packageId . lowercased ( ) ,
269- wasmFilename : wasmFilename, useSharedMemory : false
270- )
271+ let inputPath = selfPackageDir . appending ( path : file )
272+ let substitutions = [
273+ " @PACKAGE_TO_JS_MODULE_PATH@ " : wasmFilename
274+ ]
271275 return make. addTask (
272- inputFiles: [ selfPath] , inputTasks: [ outputDirTask] + inputs,
276+ inputFiles: [ selfPath, inputPath . path ] , inputTasks: [ outputDirTask] + inputs,
273277 output: outputDir. appending ( path: output) . path
274278 ) {
275- let content = context [ keyPath: template]
279+ var content = try String ( contentsOf: inputPath, encoding: . utf8)
280+ let options = PreprocessOptions ( substitutions: substitutions)
281+ content = try preprocess ( source: content, options: options)
276282 try content. write ( toFile: $0. output, atomically: true , encoding: . utf8)
277283 }
278284 }
0 commit comments