Recommand · November 3, 2021 0

How to import an uploaded JS file?

I have an Angular application using a Plugin- Architecture from this article
In this article the build bundle.js file is exported and imported in the application from assets folder using .import(filepath). What I am trying to do is instead of importing the saved file, I want to upload a file and import that file.

I know how to read and evaluate a JS file by uploading using the below code. But, I am not sure how to make it work with an Angular build file. Any idea?

function handleFileInput(files) {
  var fileContent = document.getElementById('contnt');
  var fileToUpload = files.item(0);
  var textType = /text.*/;

  if (fileToUpload.type.match(textType)) {
    var reader = new FileReader();

    reader.onload = function(e) {
      const x = eval(reader.result)
      fileContent.innerText = x;
    }

    reader.readAsText(fileToUpload);
  } else {
    fileContent.innerText = "File not supported!"
  }
}
<input id="fileInput" type="file" name="file" (change)="handleFileInput($event.target.files)" />
<pre id="fileContent"></pre>

<div id="contnt"></div>