Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Gulp to wrap javascript files with a IIFE

I have an angular app that has a lot of .js files. It's boring to add an IIFE to each file and then add 'use strict'.

Is there any way to automate this? I use gulp to run tasks.

like image 291
Papzord Avatar asked Aug 26 '14 14:08

Papzord


1 Answers

Use the gulp-wrap plugin with a simple template:

var wrap = require("gulp-wrap");

gulp.src("./src/*.js")
    .pipe(wrap('(function(){\n"use strict";\n<%= contents %>\n})();'))
    .pipe(gulp.dest("./dist"));

This will wrap each file's contents with the template:

(function(){
"use strict";
//contents here…
})();

You can also store the template on the filesystem, rather than embedding it in your gulpfile, and call gulp-wrap using wrap({src: 'path/to/template'})

like image 96
OverZealous Avatar answered Oct 21 '22 18:10

OverZealous