2

is there a way, to load some external code during execution of my script? I need something like webpack's require(), but I can't load all the scripts at once (at "compile time"), because there is way too many of them and it would cause enormous performance issues.

In other words, let's say I have some modules:

//modules/module1.js
var Module1 = {
    run: function(){...},
    data: "SomeData"
}
//modules/module2.js
var Module2 = {
    run: function(){...},
    data: "SomeData"
}

and then main script which is supposed to load one of them

if(load_module_1){
    var module = load("modules/module1.js")
    console.log(module.data)
    module.run()
    ...
}

Can it be done? (I want to be able to load "module" into a variable and to keep it isolated from other context.)

Norman One
  • 21
  • 2
  • 2
    Possible duplicate of [How do I include a JavaScript file in another JavaScript file?](http://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file) – gus27 Feb 25 '17 at 17:28
  • @gus27 I wonder whether it can be done exactly the way I want it to be done -> so that it works like a module, so I can do var module = load("some/module") and it will load and assign that module into my variable and it will be isolated from the other code, not included into global namespace. – Norman One Feb 25 '17 at 17:38
  • You are looking for AMD, e.g. [require.js](http://requirejs.org/). – Bergi Feb 25 '17 at 18:06
  • @guest271314 No, that makes no sense – Bergi Feb 25 '17 at 18:07
  • @guest271314 And exactly that's the reason why it makes no sense to involve a worker. – Bergi Feb 25 '17 at 18:22
  • @Bergi Too many workarounds necessary to pass function to main thread. – guest271314 Feb 25 '17 at 18:58
  • are you using webpack? – Hosar Feb 25 '17 at 19:27
  • Yes, I am using webpack. – Norman One Feb 27 '17 at 11:42

0 Answers0