yeoman / grunt-usemin

[UNMAINTAINED] Replaces references to non-optimized scripts or stylesheets into a set of HTML files (or any templates/views)
BSD 2-Clause "Simplified" License
1.22k stars 338 forks source link

Support for importScripts() #499

Open alippai opened 9 years ago

alippai commented 9 years ago

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: ... -->?

stephanebachelier commented 9 years ago

@alippai can you show me some configuration ?

stephanebachelier commented 9 years ago

ping @alippai

arthurvr commented 9 years ago

ping @alippai

pulkit110 commented 9 years ago

@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'
}
nevcos commented 8 years ago

:+1:

hacker112 commented 8 years ago

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 + '");';
                    }
                }
            }
        }
    }
...
});
solgarius commented 8 years ago

: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?

hacker112 commented 8 years ago

@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.

solgarius commented 8 years ago

@hacker112 Thanks, greatly appreciate this work around.