[英]How do I `import blah from 'http://bar.org/some-esm-module.js'` in the Chrome Devtools JS console?
When I try to use an ESM import from the Chrome devtools / javascript console, it complains:当我尝试从 Chrome devtools / javascript 控制台使用 ESM 导入时,它抱怨:
> import Confetti from 'https://cdn.skypack.dev/canvas-confetti'
Uncaught SyntaxError: Cannot use import statement outside a module
Is there any way to convince the chrome console to execute as type='module'
so I can use module statements?有什么方法可以说服 chrome 控制台以type='module'
执行,以便我可以使用模块语句? Or another way to workaround this that still permits interactive imports of ESM modules for interactive development?或者另一种解决方法仍然允许交互式导入 ESM 模块以进行交互式开发?
I've been switching to heavily using ESM modules, in particular the large library of NPM ESM modules auto-converted by Skypack .我一直在大量使用 ESM 模块,特别是由Skypack自动转换的大型 NPM ESM 模块库。 This has worked great in static development, but I'm a heavy REPL / browser devtools user.这在静态开发中效果很好,但我是一个重度 REPL/浏览器 devtools 用户。
Even finding a third-party fakeImport(urlToESM)
function I could use to load ESM modules would be a huge productivity improvement for me.即使找到一个我可以用来加载 ESM 模块的第三方fakeImport(urlToESM)
函数对我来说也是一个巨大的生产力提升。
动态import()
和顶级await
应该可以工作:
const { default: Confetti } = await import('https://cdn.skypack.dev/canvas-confetti');
Dynamic import might come in handy.动态导入可能会派上用场。
import('/modules/my-module.js')
.then((module) => {
// Do something with the module.
});
You can use await with it.您可以将 await 与它一起使用。
let module = await import('/modules/my-module.js');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.