sebdeckers / grunt-rev

:punch: Asset revving for Grunt.js
MIT License
240 stars 54 forks source link

Updating rev'ed file reference in JS files #17

Open ghost opened 11 years ago

ghost commented 11 years ago

I'm making web workers, and using grunt (yeoman setup) to copy and uglify and version my JS. The versioning works in the HTML index because it has the build comments. But the web workers are referenced inside a JS file. Is there a way to get these URL references in my JS to update with the rev'ed paths... or can I not use rev on the web workers because of this?

altschuler commented 10 years ago

I'm looking for the exact same thing. Without it one needs to do some nasty hacks to work around it. The usemin task does not allow js files to be parsed, which makes me believe this is not something that's going to be an easy fix. Maybe a separate task would be the optimal solution.

ghost commented 10 years ago

I've moved the reference to the file (a Web Worker) to within the index file, inside a script tag I have the code:

var tweetsWorker = new Worker('/scripts/tweetsWorker.js');

Now all the other JS files are merged and minified using the syntax like:

<!-- build:js /scripts/plugins.js -->
....list of script files in the usual HTML manor...
<!-- endbuild -->

Now, in my Gruntfile in order to copy the Worker js file(s) I added this:

copy: {
  dist: {
    files: [{
      expand: true,
      dot: true,
      cwd: '<%= yeoman.app %>',
      dest: '<%= yeoman.dist %>',
      src: [
        'scripts/*Worker.js',
        //....

But the way I have rev setup it would version the Worker.js files with a hash prefixed to the name, BUT the reference in the JS of the index file was NOT updated. My current workaround is to tell rev NOT to version *Worker.js files.... BUT considering everything else works so beautifully, I don't want to run into cache issues in this one place.

BTW, here's the code to ignore the *Worker.js files (as you can probably guessed):

rev: {
  dist: {
    files: {
      src: [
        '<%= yeoman.dist %>/scripts/{,*/}*.js',
        '!<%= yeoman.dist %>/scripts/*Worker.js',
        //....

Anyway, I made it far enough to get all the references to version-ed code to the index.html file, but still can't figure out how to get this to work automatically, which would be amazing.

palominoz commented 10 years ago

+1

velochy commented 10 years ago

+1

unkstar commented 10 years ago

by adding following snippet in your Gruntfile.js, you can update the versioned worker file:

        // Performs rewrites based on rev and the useminPrepare configuration
        usemin: {
            options: {
                assetsDirs: ['<%= yeoman.dist %>'],
                //add pattern for worker
                patterns: {
                    worker: [
                        [/new Worker\(['"]([^"']+)['"]/gm,
                        'Update the Workers to reference our concat/min/revved script files'
                        ]
                    ]
                }
            }, 
           //...add a worker target
           worker: ['<%= yeoman.dist %>/scripts/*.js']
        },
paoloyx commented 10 years ago

+1, unkstar: you saved my day