How to Use Assetic for requireJs

放肆的年华 提交于 2019-12-12 09:41:54

问题


I am trying to use require.js in synfony2 project.

Here the code in the main twig file:

<script 
        data-main="{{ asset('bundles/web/js/main.js') }}" 
        src="{{ asset('vendor/js/require.js') }}">
</script>

The file vendor/js/require.js is correctly loaded but for the file bundles/web/js/main.js I get the message:

Uncaught Error: Load timeout for modules: mainApp.js?201205021855 http://requirejs.org/docs/errors.html#timeout

I am using RequireJS 1.0.8. Any idea how to fix the problem? Thanks.


If I look to the source page it looks like:

<script 
      data-main="/~myName/myProject/web/bundles/web/js/main.js?101205021855"       
      src="/~myName/myProject/web/vendor/js/require.js?101205021855">
</script>

So the paths are rights, but on javascript console I get the following message:

GET http://localhost/~myName/myProject/web/app_dev.php/main.js?201205021855 404 (Not Found)


回答1:


Adding to @dgabriel response. You can do it using slice filter,

<script 
        data-main="{{ asset('bundles/web/js/main.js') | slice(0, -3) }}" 
        src="{{ asset('vendor/js/require.js') }}">
</script>



回答2:


  1. Make sure to leave off the '.js' file extension from your assets. It should be data-main="{{ asset('bundles/web/js/main') }}"

  2. Make sure your path to the file is correct.

  3. Make sure there are no javascript errors in main.js




回答3:


That 404 looks like it's coming as a result of 'main' being required somewhere else in your application - it doesn't match the path in your data-main. In that case, it's most likely an issue with your baseUrl or paths config (both of which are different ways to tell RequireJS where your modules are located). In your example, your baseUrl would need to be '/~myName/myProject/web/bundles/web/js'.

You may want to try using https://github.com/hearsayit/HearsayRequireJSBundle. It handles those configuration details for you automatically, and integrates nicely with Assetic.



来源:https://stackoverflow.com/questions/10523273/how-to-use-assetic-for-requirejs

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