Search code examples
javascriptphpnode.jssymfonyuglifyjs

UglifyJS not working in Symfony2 production environment


I believe I configured it properly -- here is my config.yml:

assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    node: "/usr/local/bin/node"
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        uglifyjs2:
            # the path to the uglifyjs executable
            bin: "%kernel.root_dir%/Resources/node_modules/.bin/uglifyjs"
        cssrewrite: ~

I'm storing uglifyjs locally in the project, just to make it easier for distribution.

I am doing the following to generate all the JS files, using uglifyjs:

{% javascripts 
    'vendor/bower_components/jquery/dist/jquery.min.js'
    'vendor/bower_components/bootstrap-sass/assets/javascripts/bootstrap.min.js'
    'vendor/bower_components/angular-route/angular-route.min.js'
    'vendor/bower_components/underscore/underscore-min.js'
    'vendor/bower_components/angular-cookies/angular-cookies.min.js'
    'js/test1.js'
    'js/test2.js'
    filter='?uglifyjs2' %}

    <script src="{{ asset_url }}"></script>

{% endjavascripts %}

If I load the page in dev mode (/app_dev.php), it works fine, and just loads the files individually, but minified by uglifyjs; but if I try to load it in production, it combines all of those js files into one js file (ie. a07da66.js). The problem I'm facing, is that it gets a 404 not found error when trying to load that file (a07da66.js). It tries to access it at js/a07da66.js - seems to create them under js/ by default.

Why does it work fine in app_dev.php, when it creates the new files under js/ (js/a07da66_jquery.min_1.js), but not in production?


Solution

  • You have to run app/console --env=prod assetic:dump to generate the assets for the prod environment.

    Rule of thumb:

    • While in dev mode, keep app/console assetic:watch --force running in the background (at least while working on JS/CSS files). It will regenerate dev assets whenever a file changes.
    • When deploying/building, run app/console --env=prod assetic:dump once in order to have the combined assets for prod generated.

    The idea of the prod mode is that you generate some sort of build or snapshot of your current development. Therefore, you would use app/console --env=prod assetic:dump only when creating the build.

    Of course, you can switch your dev environment to prod for a brief test, but then you must perform the build procedure, too (which includes generating the prod assets).