Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle inline code with RequireJS Optimization

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?

like image 532
Peter Han Avatar asked Jun 21 '13 21:06

Peter Han


People also ask

Is RequireJS synchronous?

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.

What is Shim RequireJS?

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.

What is the use of RequireJS?

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.

How do I use require config?

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.


1 Answers

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

like image 124
Simon Avatar answered Oct 21 '22 12:10

Simon