Fairly new to JS modules of any kind. I have a simple vanilla JS script that is intended to be loaded into an html document via the script tag. Basically...
// invoke main function when DOM is ready
document.addEventListener("DOMContentLoaded", function() {
MainFunction();
});
var MainFunction = function() {
// check for specific DOM nodes upon invocation...
}
Works great. But I'd like to also be able to make MainFunction available as a module export so I can import it into a webpack bundle like:
import MainFunction from 'myclassicscript';
And then I can invoke it as necessary in the main bundle with:
MainFunction();
How the same script can be used both ways. I can't seem to find any info on how to do it, but I might have totally the wrong idea.
Something like...?
// check if this is a browser environment...
if (typeof window !== 'undefined') {
// invoke main function when DOM is ready
document.addEventListener("DOMContentLoaded", function() {
MainFunction();
});
}
var MainFunction = function() {
// check for specific DOM nodes upon invocation...
}
// ???
export default MainFunction;
importis an ES6 spec which hasn't been implemented yet in any browser