I have a dependent classic JavaScript file which I cannot edit and I need to import. It looks like this:
function DependencyInterface()
{
}
DependencyInterface.Foo = 'bar';
DependencyInterface.prototype.one = 1;
DependencyInterface.prototype.two = 1;
DependencyInterface.prototype.doSomething = function(dependencyPayload) {
// ...
}
function DependencyPayload(value) {
this.value = value;
}
I would like to use it, like this:
import '@/scripts/DependencyInterface.js'
let dependencyInterface = new DependencyInterface();
dependencyInterface.doSomething(new DependencyPayload(3));
Unfortunately I am getting errors like:
- 'DependencyInterface' is not defined (no-undef)
- 'DependencyPayload' is not defined (no-undef)
I read from this post (ES6 import equivalent of require() without exports) that import statements like this are block scoped, and the only thing through are what is exported.
The only way around this I have found is to the dependency file in a public static location and do this in my root HTML:
<script type="text/javascript" src="<%= BASE_URL %>js/DependencyInterface.js"></script>
<script type="text/javascript">
window.createDependencyInterface = function() { return new DependencyInterface() };
window.createDependencyPayload = function(value) { return new DependencyPayload(value); }
</script>
But I really hate this solution. It isn't clean or scalable.
Is there any way to import this classic JavaScript file? Thanks!