问题
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