@@ -39,13 +39,6 @@ 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
-
49
42
/// Plans the build for packaging.
50
43
struct PackagingPlanner {
51
44
/// The options for packaging
@@ -194,30 +187,41 @@ struct PackagingPlanner {
194
187
}
195
188
packageInputs. append ( wasm)
196
189
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)
209
210
}
210
- // Copy files
211
+ packageInputs. append ( packageJSON)
212
+
213
+ // Copy the template files
211
214
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 " ) ,
212
219
( " Sources/JavaScriptKit/Runtime/index.mjs " , " runtime.js " ) ,
213
220
] {
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
+ ) )
221
225
}
222
226
return ( packageInputs, outputDirTask)
223
227
}
@@ -241,13 +245,13 @@ struct PackagingPlanner {
241
245
allTasks. append ( binDirTask)
242
246
243
247
// 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 " ) ,
248
252
] {
249
253
allTasks. append ( planCopyTemplateFile (
250
- make: & make, template : template , output: output, outputDirTask: outputDirTask,
254
+ make: & make, file : file , output: output, outputDirTask: outputDirTask,
251
255
inputs: [ binDirTask]
252
256
) )
253
257
}
@@ -259,20 +263,22 @@ struct PackagingPlanner {
259
263
260
264
private func planCopyTemplateFile(
261
265
make: inout MiniMake ,
262
- template : KeyPath < TemplateContext , String > ,
266
+ file : String ,
263
267
output: String ,
264
268
outputDirTask: MiniMake . TaskKey ,
265
269
inputs: [ MiniMake . TaskKey ]
266
270
) -> 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
+ ]
271
275
return make. addTask (
272
- inputFiles: [ selfPath] , inputTasks: [ outputDirTask] + inputs,
276
+ inputFiles: [ selfPath, inputPath . path ] , inputTasks: [ outputDirTask] + inputs,
273
277
output: outputDir. appending ( path: output) . path
274
278
) {
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)
276
282
try content. write ( toFile: $0. output, atomically: true , encoding: . utf8)
277
283
}
278
284
}
0 commit comments