简体   繁体   English

如何确定 javascript 模块是否通过脚本 src 导入或加载?

[英]how to determine if javascript module was imported or loaded via script src?

Suppose I have a module named module.js :假设我有一个名为module.js的模块:

export default function greet() { console.info( 'hello' ); }

Within module.js (either inside or outside of function greet ), how can I determine whether the module was loaded using:module.js (在 function greet内部或外部),我如何确定模块是否已使用:

<script type=module src="./module.js">

versus:相对:

<script type=module>
import greet from './module.js';
</script>

Either way, import.meta is the same, document.currentScript is null , and NodeJS's require (and therefore require.main ) and module are both undefined .无论哪种方式, import.meta是相同的, document.currentScriptnull ,并且 NodeJS 的require (因此require.main )和module都是undefined

Thanks!谢谢!

When you import any module (either using import or <script type="module"> ), the body of the module will be evaluated only once, even if you import that module from multiple places.当您导入任何模块(使用import<script type="module"> )时,模块的主体将仅被评估一次,即使您从多个位置导入该模块也是如此。

So, the following:因此,以下内容:

//module.js
console.log('I was imported')
<script type="module">
  import './module.js'
</script>
<script type="module" src="./module.js"></script>

...will log I was imported only once. ...将记录I was imported一次。

From that, we can clearly see that a module may be imported multiple ways at the same time , so there can not be any way to determine the way how a module was imported...由此我们可以清楚地看到一个模块可能同时被多种方式导入,因此无法确定模块的导入方式......

...as long as you don't use exports. ...只要您不使用导出。

Including a module using a script tag is identical to the import 'modulepath' syntax, that is, in MDN's words , importing a module for its side effects only .使用脚本标签包含一个模块与import 'modulepath'语法相同,也就是说, 用 MDN 的话来说导入一个模块只是为了它的副作用

That means, that no exports are taken, the module is just evaluated.这意味着,不进行任何导出,只评估模块。

However, if one of the exports is used (called, for example) you can rule out the possibility that the module's instance that used the export was imported by a script tag.但是,如果使用了其中一个导出(例如调用),您可以排除使用导出的模块实例是由脚本标记导入的可能性。

The following scenario is still possible, though:但是,以下情况仍然是可能的:

//module.js
export default function greet() { 
  console.info('hello'); //<-- This must have been called from an `import` import
}
<script type="module">
  import greet from './module.js';
  greet()  //imported by `import`
</script>
<script type="module" src="./module.js"></script> <!-- imported by script tag -->

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在模块内以编程方式确定 javascript 模块是否通过脚本 src 加载(未导入) - how to programmatically determine, within the module, if javascript module was loaded via script src (not imported) 正在通过以下方式加载 JavaScript ES6 模块<script src=“”> and importing all exports in a <script> tag the same? - Are loading a JavaScript ES6 module via <script src=“”> and importing all exports in a <script> tag the same? 如何通过脚本元素访问由javascript加载的外部脚本 - How to access to external scripts loaded by javascript via script element 读取通过src属性加载的脚本的js源代码 - Read js source code of a script loaded via a src attribute 如何等待直到使用javascript在脚本src标签中加载函数 - How to wait until functions to be loaded inside script src tag using javascript 如果http状态代码为200或301,如何告诉Javascript加载的脚本src? - How to tell Javascript loaded script src if http status code 200 or 301? HTML/Javascript:如何访问在带有 src 集的脚本标签中加载的 JSON 数据 - HTML/Javascript: how to access JSON data loaded in a script tag with src set 如何确定何时加载和执行脚本 - how to determine when a script has loaded and executed 动态图片未通过:src加载 - Dynamic image is not loaded via :src 如何在JavaScript中访问通过<script type =“text / plain”src = ...>检索的纯文本内容? - How to access plain text content retrieved via <script type=“text/plain” src=…> in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM