Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

source mapping with assetic in symfony2

there is this cool feature Source Maps in html5. In my Symfony2 project I use jQuery mobile which uses this feature (I use the BmatznerJQueryMobileBundle for integration).

In my <head> i do following:

{% javascripts
    '@BmatznerJQueryBundle/Resources/public/js/jquery.min.js'
    '@BmatznerJQueryMobileBundle/Resources/public/js/jquery.mobile.min.js'
%}
    <script src="{{ asset_url }}"></script>
{% endjavascripts %}

This works fine for the js files, but Chrome gets an 404 error trying to get the source maping file. Does anybody know how to solve this?

The Source Mapping in the jquery.mobile.min.js file looks like this and is also in the same directory.

//# sourceMappingURL=jquery.mobile-1.4.2.min.map

error:

404 in chrome

like image 496
Tuxes3 Avatar asked Jun 11 '14 20:06

Tuxes3


People also ask

How does assetic work with Symfony?

In the dev environment, Assetic generates paths to CSS and JavaScript files that don't physically exist on your computer. But they render nonetheless because an internal Symfony controller opens the files and serves back the content (after running any filters).

What is assetic and how do I use it?

Fortunately, Assetic provides a way to dump your assets to real files, instead of being generated dynamically. In the prod environment, your JS and CSS files are represented by a single tag each. In other words, instead of seeing each JavaScript file you're including in your source, you'll likely just see something like this:

How do I implement cache busting in Symfony?

Symfony provides various cache busting implementations via the version , version_format, and json_manifest_path configuration options. In the dev environment, Assetic generates paths to CSS and JavaScript files that don't physically exist on your computer.


Video Answer


1 Answers

The problem with your example is that you are combining two sources into one, and from two different locations. If you look at the generated script tags in your dev environment, you'll see that the two routes are something like:

<script src="/js/ed5a632_jquery.min_1.js"></script>
<script src="/js/ed5a632_jquery.mobile.min_2.js"></script>

Assetic does not create matching routes for source maps, so these files now have invalid sourceMappingURL values - hence your 404.

One solution is to export the source maps (and sources) to a location relative to the dynamic routes using the assetic.assets configuration:

ref: https://github.com/symfony/symfony/pull/848

e.g. config.yml

assetic:
  assets:
    map1:
      input: "%kernel.root_dir%/../src/path/to/jquery.min.map"
      output: js/jquery.min.map
    src1:
      input: "%kernel.root_dir%/../src/path/to/jquery.js"
      output: js/jquery.js

This will ensure the sourceMappingURL is hit, but it's a bit of a mess having all this separate from your template code.

If you can live with two separate assets in your production site, a much simpler solution is to just reference the two assets individually, e.g.

<script src="{{ asset('bundles/bmatznerjquery/js/jquery.min.js') }}"></script>
<script src="{{ asset('bundles/bmatznerjquerymobile/js/jquery.mobile.min.js') }}"></script>

After running the assets:install console command, these scripts should link just fine to the source maps and source files.

like image 70
Tim Avatar answered Sep 21 '22 13:09

Tim