add JS-snippet support using from: .module("/path/to/js-module.js") syntax - #792
add JS-snippet support using from: .module("/path/to/js-module.js") syntax#792sliemeobn wants to merge 10 commits into
from: .module("/path/to/js-module.js") syntax#792Conversation
|
closes #508 |
There was a problem hiding this comment.
Nice feature — the design fits the existing patterns well and test coverage is strong. Details in the inline comments.
Two tiny things not worth their own threads: the registry error messages render a double slash (TestModule//Modules/x.mjs — path already starts with /), and a runtime test for a throwing module export would pin down the setException path too.
| for skeleton in skeletons { | ||
| for module in skeleton.imported?.modules ?? [] { | ||
| guard module.path.hasPrefix("/") else { | ||
| throw BridgeJSLinkError( |
There was a problem hiding this comment.
relativePath is spliced straight into outputDir.appending(path:) in PackagingPlanner. makeJavaScriptModuleFileLookup guarantees no traversal for locally generated skeletons, so this is safe today — but skeletons also arrive from dependency packages' committed BridgeJS.json, and a path like /../../evil.js in one of those would write outside the output directory (and emit an escaping import line). Cheap to close here since we're already validating:
guard module.path.hasPrefix("/"),
!module.path.split(separator: "/").contains("..")
else {
throw BridgeJSLinkError(
message: "JavaScript module path must start with '/' and must not contain '..': \(module.path)"
)
}There was a problem hiding this comment.
makes sense, will add that
|
|
||
| You can bring JavaScript into Swift in two ways: | ||
| You can bring JavaScript into Swift in three ways: | ||
|
|
There was a problem hiding this comment.
Tiny one: the intro at line 11 of this article still says imports work in "two ways", so the article now disagrees with itself:
You can import JavaScript APIs into Swift in three ways:There was a problem hiding this comment.
actually, IIUC, this refers to "either via macros, or via bridge-js.d.ts file.
I'll change it to You can define JavaScript bindings for Swift in two ways: to fit the rest of the doc better.
kateinoigakukun
left a comment
There was a problem hiding this comment.
IIUC the current approach requires developers to run the build plugins and link steps to reflect their changes against snippet js files into the final output, right? This is another challenge I wanted to explore to have faster iterations.
|
@krodak @kateinoigakukun I reworked the whole thing to not copy anything to the skeletons and only use the "from" paths during the PackageToJS / link run. I think this is better ultimately. the only thing that is a bit worse is that I always load and decode all skeletons up-front to extract all js-files as mini-make inputs. I though about adding some caching or smarter loading, but I wanted to keep this PR more straight forward. maybe a separate PR "optimize PackageToJS" would be a good idea anyway eventually - I am sure there are milliseconds to be found here and there. |
|
any idea what causes the diff? I ran the generate stuff locally and get no changes - but CI has this error |
cac4b67 to
936b5c5
Compare
allow to ship JS code as the source for imports (in addition to ".global" and explicitly provided imports)
I noticed that currently there is no diagnostic when
from:is used in places that do not support it, I did not add these checks (eg: class members, setters) to this PR more focussed. (#793)