Skip to content

Commit

Permalink
Add JavascriptCore polyfill for require()
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesramsay authored and johnste committed Jan 3, 2024
1 parent acbc3c1 commit b358845
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Finicky/Finicky/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,21 @@ open class FinickyConfig {
FinickyAPI.setContext(ctx)
ctx.setObject(FinickyAPI.self, forKeyedSubscript: "finickyInternalAPI" as NSCopying & NSObjectProtocol)
ctx.evaluateScript("var finicky = finickyConfigApi.createAPI();")

// Add polyfills
let require: @convention(block) (String) -> (JSValue?) = { path in
let expandedPath = NSString(string: path).expandingTildeInPath

// Return void or throw an error here.
guard FileManager.default.fileExists(atPath: expandedPath)
else { debugPrint("Require: filename \(expandedPath) does not exist")
return nil }

guard let fileContent = try? String(contentsOfFile: expandedPath)
else { return nil }

return self.ctx.evaluateScript(fileContent)
}
ctx.setObject(require, forKeyedSubscript: "require" as NSString)
}
}

0 comments on commit b358845

Please sign in to comment.