Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating rev'ed file reference in JS files #17

Open
ghost opened this issue Oct 23, 2013 · 6 comments
Open

Updating rev'ed file reference in JS files #17

ghost opened this issue Oct 23, 2013 · 6 comments

Comments

@ghost
Copy link

ghost commented Oct 23, 2013

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
Copy link

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
Copy link
Author

ghost commented Nov 27, 2013

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
Copy link

+1

1 similar comment
@velochy
Copy link

velochy commented Jan 25, 2014

+1

@unkstar
Copy link

unkstar commented Feb 26, 2014

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
Copy link

paoloyx commented May 30, 2014

+1, unkstar: you saved my day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants