BridgeJS: support importing from external ECMAScript modules - #795
Draft
kateinoigakukun wants to merge 2 commits into
Draft
BridgeJS: support importing from external ECMAScript modules#795kateinoigakukun wants to merge 2 commits into
kateinoigakukun wants to merge 2 commits into
Conversation
Extend `from: .module(...)` to accept bare specifiers like `node:path` or an npm package, add `jsName: .default` for default exports, and emit named imports so a wrong export name now fails at module-link time instead of at call time.
Do not require a module export for a wrapper-only `@JSClass`, since a named import is a link-time requirement and nothing looks that name up; accept `jsName: nil` and the explicit `.name(...)` spelling; and validate the tagged `from` form like the plain-string form.
Contributor
|
I like the idea, but I am not sure I like "losing" the diagnostic guidance to use "/my-file.js" in there - now "anything goes" and there is no way to find out until things don't work at runtime. how about we use separate cases? eg: |
Member
Author
|
@sliemeobn Yeah, I agree that it makes more sense to have different How about |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
from: .module(...)currently only reaches JavaScript files that live inside your own Swift target. This extends it to modules you did not write: Node builtins and installed npm packages.A leading
/still means "a file rooted at this Swift target". Anything else goes to the JavaScript module resolver verbatim.Two smaller additions ride along.
jsName: .defaultnames a module's default export, which is how many npm packages expose their main value. Reaching it used to require a local re-export file.Generated glue also switches from a namespace object plus property lookup to named ECMAScript imports. A misspelled export name is now reported by the engine at module-link time, rather than surfacing as
undefined is not a functionat call time. Existing local-file users get that improvement too.Design notes
No build-time resolution check. Resolution is host-defined. A bundler alias, an import map, or Deno will resolve specifiers that Node rejects, so any check we wrote would reject working setups. Validation is limited to rejecting the empty string and relative specifiers (use the
/-prefixed form for your own files). URL and#importsspecifiers pass through untouched.Static imports, which is already the precedent here.
Plugins/PackageToJS/Templates/platforms/node.jsstatically importsnode:url,node:worker_threads, and an npm package today.Skeleton format churn is minimal.
"from"stays a plain"global"or a plain path string; the tagged object{"kind": "module", "specifier": "..."}appears only for a bare specifier. No existing checked-inBridgeJS.jsonor JSON snapshot changed shape. The tagged form is needed becauseglobalis a real npm package name and would otherwise collide with the sentinel.Named imports fall back per origin. If any export name for one specifier is not a valid JavaScript identifier, that specifier reverts to
import * as. This avoids depending on ES2022 string-literal import names, which matters because TS2Swift emits names likejsName: "property-with-dashes".