Automatically export everything from an ES5 file to be used in ES6

I have a pretty simple ES6 ( init.js) code :

import App from '../vendor/app';

window.init = function() {
  let app = new App();
  app.hello(); // prints "hello" to console
}

appmodule ( app.js) is ES5 and I do not want to modify this file:

function App() {
}

App.prototype.hello = function() {
  console.log('hello from vendor module');
}

The code above will not work because I need to add

export default App;

to the end app.js. But I want the vendor files to be clean and without any changes as much as possible. The hard part is that it app.jsdepends on other ES5 modules, for example:

App.prototype.hello2 = function() {
  dialog.show('hello from vendor app module, but using another vendor "dialog" module');
}

dialogThe variable is defined in ES5 dialog.js.

It works well when you add script -tags to your html page. I also want to avoid this. Ideally, I only want to modify init.js. Use some kind of import, so it automatically connects all the modules of the provider to mine init.js.

+4

Source: https://habr.com/ru/post/1622966/


All Articles