Reference bundles on requirejs

南笙酒味 提交于 2019-12-10 19:24:47

问题


I have an old web application that already uses bundling and minification of ASP.Net MVC.

Now I'm starting to use requirejs and I have a problem! How can I reference bundle files from requirejs?

I know that there is another similar question, but it does not answer to my question!

How to reference the bundled js files (asp.net mvc4) in Require.js?


回答1:


I'm hoping that I understand your question correctly.

If your bundle contains requireJS modules and you're loading it on the page, you can still call those modules as long as you've defined them like I have below:

Let's say your existing bundle is being rendered like this:

@Scripts.Render("~/bundles/aCustomBundle")

The bundle contains jQuery, React and a aLibrary module. The aLibrary is defined as follows in that bundle.

// Define custom modules
define('aLibrary', [], function() {
   return {
      aFunction: function() {
           console.log('a function within a library');
      }
   }
});

In your page, you can call that module and define the third-party libraries like this.

<script>
    // Third party libraries
    define('jquery', [], function () { return jQuery; });
    define('react', [], function () { return React; });

    // Use the libs from the bundle
    require(['aLibrary', 'jquery', 'react'], function(a, j, r) {

       // Execute the function from the aLibrary
       a.aFunction();
    });
</script>


来源:https://stackoverflow.com/questions/21838793/reference-bundles-on-requirejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!