grunt-usemin icon indicating copy to clipboard operation
grunt-usemin copied to clipboard

Support for importScripts()

Open alippai opened this issue 11 years ago • 9 comments

As only HTML comment blocks are supported you can't process blocks in JS Web Workers. Can you add support for /* build: ... */ blocks next to the HTML's <!-- build: ... -->?

alippai avatar Dec 12 '14 10:12 alippai

@alippai can you show me some configuration ?

stephanebachelier avatar Feb 21 '15 23:02 stephanebachelier

ping @alippai

stephanebachelier avatar Apr 15 '15 22:04 stephanebachelier

ping @alippai

arthurvr avatar May 17 '15 13:05 arthurvr

@arthurvr, @stephanebachelier: Here is a sample block inside a worker (say worker.js)

/* build:js js/app.js */
importScripts("js/app.js")
importScripts("js/controllers/thing-controller.js")
importScripts("js/models/thing-model.js", "js/views/thing-view.js")
/* endbuild */

Grunt task configuration

useminPrepare: {
  js: 'worker.js'
}

pulkit110 avatar Jul 28 '15 12:07 pulkit110

:+1:

nevcos avatar Nov 25 '15 20:11 nevcos

I made a very ugly workaround that makes it possible to use usemin for WebWorker. I hope that there will be a nicer way to do this in the future. Hope someone can have use of it.

worker.js

// It is important that worker is in the root folder (like index.html)
// for the usemin build script to work properly
// <!-- build:js js/appAndWorker.js -->
var sources = ['src="bower_components/moment/moment.js"',
               'src="bower_components/bluebird/js/browser/bluebird.js"'];

sources.forEach(function (source) {
    source = /src="(.+)"/.exec(source)[1];
    importScripts(source);
});
// <!-- endbuild -->

Gruntfile:

grunt.initConfig({
...
    filerev: {
        files: {
            src: 'dist/js/appAndWorker.js'
        }
    },
    useminPrepare: {
        js: 'app/worker.js'
    },
    usemin: {
        html: ['dist/index.html'],
        js: {
            src: 'dist/worker.js',
            options: {
                patterns: {
                    js: [
                        [/importScripts\(['"]([^"']+)["']/gm,
                            'Update the Worker with the new revved filename'
                        ]
                    ]
                },
                blockReplacements: {
                    js: function (block) {
                        return 'importScripts("' + block.dest + '");';
                    }
                }
            }
        }
    }
...
});

hacker112 avatar Jan 13 '16 14:01 hacker112

:thumbsup: @hacker112 I assume that work around is only for the filename replacement part, have you had any success/worked with adding the files inside sources to the concat part and/or worked with the filerev? I assume that wouldn't work either?

nickbeckenham avatar Jan 27 '16 05:01 nickbeckenham

@solgarius I missed the filerev and useminPrepare in my example code. It is now updated. The concatenation of files and filerev works for me also.

hacker112 avatar Feb 01 '16 13:02 hacker112

@hacker112 Thanks, greatly appreciate this work around.

nickbeckenham avatar Feb 02 '16 08:02 nickbeckenham