Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In RequireJs, how do I instruct the optimizer to to produce hash output files names?

I run the optimizer like this

sudo /usr/local/bin/node /tmp/r.j/r.js -o name=main out=test.js baseUrl=. 

for test
Now, how do I tell the optimizer to output the filename as a hash of the content (obviously to set max expires) and then rename the dependency in the relevant require calls?

An example situation will be something like this

require({
        baseUrl: '{{ STATIC_URL }}js',
        paths: {
            jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min',
            jqueryui: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min'
        }
    }, ['order!jquery','order!jqueryui','order!main']);  

requirejs should be pulling something like 13KUJAW78M.js

Based on Miller Medeiros’s suggestion I am planning to put all the require calls into a single master file. This will also mean that all such calls will also need to be optimized.

e.g

switch(document.location.pathName){
        case '/foo':
            require(['sections/foo/main']);
            break;
        case '/foo/bar':
            require(['sections/foo/main', 'core/bar']);
            break;
        default:
            require('sections/home');
    }

require(['sections/foo/main']);

should be optimized to a hash file.

Can someone help?

like image 219
Quintin Par Avatar asked Sep 05 '11 03:09

Quintin Par


1 Answers

RequireJS optimizer doesn't have this option, but you could rename the files and use the paths config to set alias to the renamed files, see this thread for more info.

On your example for instance, if you rename the files to: 'sections/foo/main.123QWERT.js', 'sections/home.4567ASDFG.js', 'core/bar.0284ZXCV.js' you could just add a paths config like this:

require.config({
  paths : {
    //alias to new files without JS extension
    'core/bar': 'core/bar.0284ZXCV',
    'sections/home' : 'sections/home.4567ASDFG',
    'sections/foo/main' : 'sections/foo/main.123QWERT'
  }
});

The paths config should be on a file that won't be cached, maybe just keep the config on the HTML.

like image 63
Miller Medeiros Avatar answered Oct 20 '22 19:10

Miller Medeiros