Is it possible to check if JavaScript file is being run directly or if it was required as part of an es6 module import.
for example a main script is included.
// main.js
import './other';
if (mainTest){
console.log('This should run');
}
which imports a dependency.
// other.js
if (mainTest){
console.log('This should never run');
}
including <script src=main.js></script>
should result in the console message from main.js
but not other.js.
I found the answer to this question with regards to node, but I am interested specifically for es6 imports
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…