How should I handle inline code when using RequireJS Optimization?
Along with bunch of separate JavaScript files that are being loaded via RequireJS, I have an object defined within my ASPX file.
<script>
define('PageData', function(){
return {
target: <%=_target%>,
permissions: <%=_permissions%>
};
});
</script>
This object contains page load data and is required by many of my files.
Of course, when I try to use Optimization, it complains because it cannot find neither a module defined as PageData nor a file called PageData.js.
Is there a way to ignore this dependency?
Is RequireJS synchronous? So, RequireJS doesn't support it. From your use case it seems that you don't need synchronous RequireJS, you need to return result asynchronously. AMD pattern allows to define dependencies and load them asynchronously, but module's factory function must return result synchronously.
shim: Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value. Here is an example. It requires RequireJS 2.1. 0+, and assumes backbone. js, underscore.
RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.
RequireJS can be initialized by passing the main configuration in the HTML template through the data-main attribute. It is used by RequireJS to know which module to load in your application. To include the Require. js file, you need to add the script tag in the html file.
Got answer from jrburke and it works for me.
https://github.com/jrburke/requirejs/issues/867
Use the "empty:" paths config option in the build to allow the optimization to complete:
http://requirejs.org/docs/optimization.html#empty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With