Requirejs - build a module when it is defined, not when it is required

I have a set of requirejs named modules with constructor-as-function.

define('myModule', [ 'import1', 'import2' ], function(i1, i2) { ... }); 

I want to be able

  • attach modules to HTML in the order in which you build the code DO NOT USE the requirejs API through script tags
  • attach and use modules USING the requirejs API

Now I add a script call with define to the document, the script is loaded successfully, but the module does not receive the definition, the constructor is not called.

Is this normal behavior? Is there a workaround to fix this?

+6
source share
1 answer

The script loaded successfully, but the constructor is not called. Is this normal behavior?

Yes. They are executed only if necessary; define can be renamed to register . In addition, in any case, you may have to wait for your dependencies.

Is there a workaround to fix this?

If you want to execute it, just put a require() call on it ( not in the same file, though ).

I have legacy code that knows nothing about modules and is dependent on file attachments.

While the script attachment successfully loads them (synchronously!), They will execute asynchronously. You'd better wrap the legacy code in require (which shouldn't break anything).

+6
source

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