-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
REPL: import statement inside the Node.js REPL #48084
Comments
Static imports are valid syntax only at the top-level body of a module, so that wouldn't be suited for REPL IIUC.
It should be noted that Chrome DevTools still don't let you use static imports in its console.
That sounds like a more reasonable proposal, would you like to submit a PR? |
Sure @aduh95 I can do a PR for this. |
@aduh95 Wondering if we should handle all variations of static imports and convert that to dynamic imports or just show a generic example! 🤷 |
There are three types on static import, you don’t have to treat them all the same. If you can show the actual replacement for the user, that’s of course more valuable, but if that’s too difficult it’s ok to use a generic example. |
These four types should be good? Named import: import { export1, export2 } from "module-name"; But then, do we have AST capabilities baked in the source to covert this or do we depend on a robust RE? |
We have acorn |
Added a sample to acron explorer.
|
@aduh95 It was easier than expected with const toDynamicImport = (codeLine) => {
let dynamicImportStatement = ''
const ast = acorn.parse(codeSnippet, { sourceType: 'module' });
walk.ancestor(ast, {
ImportDeclaration(node, ancestors) {
const importedModules = node.source.value;
let importedSpecifiers = node.specifiers.map(specifier => specifier.local.name);
if (importedSpecifiers.length > 1) {
importedSpecifiers = `{${importedSpecifiers.join(" ")}}`
}
dynamicImportStatement = `const ${importedSpecifiers} = await import('${importedModules}')`;
},
});
return dynamicImportStatement;
}
|
We already import it: Lines 104 to 107 in 05693ac
|
Can't the REPL just be started as a module body instead of current CJS body? Being able to use regular |
Modules are strict mode only, I think that'd be a not-so-nice experience for a REPL. |
I guess strictness exceptions could be made for the REPL. It's meant as a user interface. Either run as a "lax" module or transform the import to dynamic under the hood. |
Anything in the REPL that isn't the same in node code is a potential footgun, or could cause confusion. There's already a few of these things like core modules being implicitly available. |
Maybe the REPL could have two modes, a strict one without implicit core module globals, no Though I don't see a use case for strict REPL. If I want strict I run a script instead. |
I like @silverwind's idea. I'm happy to change the PR if we have consensus here. |
Default should be non-strict to not break existing use cases. A |
I very much agree with that, introducing a different kind of REPL seems hardly worth it. |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment. For more information on how the project manages feature requests, please consult the feature request management document. |
|
yeah, I tried the latest canary windows version of bun, bun repl is still not working. I looking forward the official windows release. |
I think this issue may be a duplicate of #33369, even though this one has a clearer description, so maybe close the other. |
Fix for this was implemented and merged at #48129 |
What is the problem this feature will solve?
Allow
import
statements in REPLWhat is the feature you are proposing to solve the problem?
Instead of:
Can we convert them to dynamic import and execute? Just in the REPL.
Like how top level await was supported in Chrome before TLA was an accepted TC39 proposal.
What alternatives have you considered?
At the least we change the message.
from:
to:
The text was updated successfully, but these errors were encountered: