M
M
Max2014-07-09 16:59:37
JavaScript
Max, 2014-07-09 16:59:37

How to make r.js optimizer work in conjunction with Symfony2 and HearsayRequireJsBundle?

Question, directly to those who worked with this bunch.
Used by Symfony2 and the HearsayRequireJsBundle bundle . There is practically no documentation on optimizing scripts using r.js in it, except for adding a filter.
The only thing that googled succeeded was this solution: labs.apt.no/how-i-got-the-hearsayrequirejsbundle-w...
So the problem is: when specifying a filter

{% javascripts
    filter='?requirejs'
    '@AcmeDemoBundle/Resources/public/js/src/main.js'
%}
    {{ require_js_initialize({ 'main' : asset_url }) }}
{% endjavascripts %}

and running the asset dump command, they are optimized based on the config, and all plug-ins are collected in 1 file. So, even according to the proposed method from the above article, scripts fall with an error in the dev environment.
The question is - is there a specific example of successfully configuring requirejs along with symfony?
Thank you.
UPD:
Configuration:
hearsay_require_js:
    require_js_src: //cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js
    initialize_template: HearsayRequireJSBundle::initialize.html.twig
    base_url: js
    base_dir: %kernel.root_dir%/scripts
    optimizer:
        path: %kernel.root_dir%/../r.js
        declare_module_name: true
        hide_unoptimized_assets: false
        options:
            name: main
    paths:
        main: @TestCoreBundle/Resources/public/js/main

Calling the code in the template:
{% javascripts
            filter='?requirejs'
            '@TestCoreBundle/Resources/public/js/main.js'
        %}
        <script type='text/javascript'>
            var require = {
                deps: [],
                callback: function() {
                    require(['main'], function() {
                    });
                }
            };
        </script>
            {{ require_js_initialize({ 'main' : asset_url, 'configure' : false })}}
        {% endjavascripts %}

main.js file:
define([
], function(){
    alert(1)
});

Here's what happens in the dev environment: alert(1) fires, after that the /js/main.js file is also loaded - this causes a 404 error.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-07-09
Protko @Fesor

The easiest option is to abandon Assetic as a builder, or build via gulp / grunt. Assetic is not the best tool for building a large frontend. Or write your own filters that will already pull what you need.
what kind of mistake? In theory, in a dev environment, you should not work out this filter and require.js should be connected, no?

D
DroniC, 2014-08-06
@DroniC

Make this setting

optimizer:
        path: %kernel.root_dir%/../r.js
        hide_unoptimized_assets: true
        options:
            name: main

And in main js file do like this
define("main",
           [], function(){
               alert(1)
});
require(['main']);

It works great for prod, but for dev you need to write the following in config_dev.yml
hearsay_require_js:
   optimizer:
        hide_unoptimized_assets: false

Even if this parameter is true, for some reason it still does not optimize files in dev mode, but simply uploads the main file not compressed, which in turn causes an error about the absence of files that are specified in define. Maybe if someone understands why, tell me then.

Q
Quber, 2014-10-13
@Quber

Well, what did you get at work? can you post the solution?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question