Right now we can only support dynamic import in the repl as the repl is running in the script goal (rather than module). Currently there is experimental support in place for Top-Level Await, which is not supported in the script goal either. This is accomplished by using acorn to transpile the submitted code that is read, prior to evaluate, and wrapping stuff in an async function.
In theory we could utilize the same pipeline to transpile a static import into an await import()
.
Thoughts?
/cc @BridgeAR, @devsnek, and @nodejs/modules who I know are going to have thoughts about this.
I had been looking into adding native support for static imports to V8's repl mode (https://docs.google.com/document/d/1WTMuQ6Ra_-hO7ebgkp_9XFDh_gaSt4eSqqPRHGlWQMg/edit#). Hoping to get around to it at some point.
The semantics of how variable shadowing works needs to be agreed upon in various places. The semi-stalled https://github.com/bmeck/js-repl-goal does not agree with V8's impl, but the V8 repl also was not going through standards.
I would like to get V8's repl mode into Node.js and get support for static import in there.
We could in theory support static import while using the await flag as it'll have similar down sides but it's not an ideal solution.
The main question for me is how long would it take to get V8's repl mode to where we want it to be.
@BridgeAR interestingly i just checked again in the prototype repl and it seems like replMode: true
isn't hanging anymore, at least on latest node. Issue might've been fixed?
One alternative would be to expose an importSync
(only in the repl not as part of the API)
import isn't sync though
I'm not a fan of offering a special / custom syntax, especially if await import()
can more or less accomplish the same thing.
The case I would like to solve for is "I copied some code with static import and it doesn't work"
Is it acceptable that that copy-pasted code would only be able to run once per repl invocation?
@ljharb it can run multiple times, repl mode also allows lexical redeclarations
@devsnek
> const f = 3;
undefined
> const f = 4;
Uncaught SyntaxError: Identifier 'f' has already been declared
> let x = 3;
undefined
> let x = 4;
Uncaught SyntaxError: Identifier 'x' has already been declared
@ljharb "repl mode" here meaning v8's debug repl mode, not node's repl module. although fwiw, repl mode doesn't support const redeclaration yet, only let. but anyway, within the repl mode we have leeway to provide this functionality.
Gotcha - isn't the request tho about node's actual repl?
@ljharb yes. the idea is that we should be using v8's repl mode in node's repl (btw I've been prototyping with all sorts of fun stuff in this vein in https://github.com/nodejs/repl, y'all should try it out)
Most helpful comment
@BridgeAR interestingly i just checked again in the prototype repl and it seems like
replMode: true
isn't hanging anymore, at least on latest node. Issue might've been fixed?