Skip to content

Commit

Permalink
Add glob support to recipe gulp.browserify
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisxue815 committed Aug 12, 2015
1 parent 2c4c04d commit ccd5e89
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 66 deletions.
3 changes: 2 additions & 1 deletion recipes/gulp.browserify/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>Browsersync Browserify Example</h1>

<div id="example"></div>

<script src="js/dist/bundle.js"></script>
<script src="js/dist/app.js"></script>
<script src="js/dist/worker/worker.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion recipes/gulp.browserify/app/js/app.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let app = 'awesome';
let app = 'awesome';
1 change: 1 addition & 0 deletions recipes/gulp.browserify/app/js/worker/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const worker = 'brilliant';
80 changes: 51 additions & 29 deletions recipes/gulp.browserify/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,69 @@ var watchify = require('watchify');
var exorcist = require('exorcist');
var browserify = require('browserify');
var browserSync = require('browser-sync').create();
var glob = require('glob');
var path = require('path');
var mkdirp = require('mkdirp');
var eventStream = require('event-stream');

// Input file.
watchify.args.debug = true;
var bundler = watchify(browserify('./app/js/app.js', watchify.args));
/**
* Creating multiple bundles with Watchify
*/
gulp.task('bundle', function (done) {
glob('./app/js/{app.js,worker/worker.js}', {}, function (err, files) {
if (err) {
done(err);
}

// Babel transform
bundler.transform(babelify.configure({
sourceMapRelative: 'app/js'
}));
var tasks = files.map(function (entry) {
var relative = path.relative('./app/js', entry);

// On updates recompile
bundler.on('update', bundle);
// A workaround for exorcist issue,
// where exorcist fails silently when the output dir does not exist
// see https://github.com/thlorenz/exorcist/issues/18
// and https://github.com/thlorenz/exorcist/pull/19
var sourceMapPath = './app/js/dist/' + relative + '.map';
mkdirp.sync(path.dirname(sourceMapPath));

function bundle() {
// Input file.
watchify.args.debug = true;
var bundler = watchify(browserify(entry, watchify.args));

gutil.log('Compiling JS...');
// Babel transform
bundler.transform(babelify.configure({
sourceMapRelative: 'app/js'
}));

return bundler.bundle()
.on('error', function (err) {
gutil.log(err.message);
browserSync.notify("Browserify Error!");
this.emit("end");
})
.pipe(exorcist('app/js/dist/bundle.js.map'))
.pipe(source('bundle.js'))
.pipe(gulp.dest('./app/js/dist'))
.pipe(browserSync.stream({once: true}));
}
var bundle = function () {
gutil.log('Compiling ' + entry + '...');

/**
* Gulp task alias
*/
gulp.task('bundle', function () {
return bundle();
return bundler.bundle()
.on('error', function (err) {
gutil.log(err.message);
browserSync.notify('Browserify Error!');
this.emit('end');
})
.pipe(exorcist(sourceMapPath))
.pipe(source(relative))
.pipe(gulp.dest('./app/js/dist'))
.pipe(browserSync.stream({once: true}));
}

// On updates recompile
bundler.on('update', bundle);

return bundle();
});

eventStream.merge(tasks).on('end', done);
});
});

/**
* First bundle, then serve from the ./app directory
*/
gulp.task('default', ['bundle'], function () {
browserSync.init({
server: "./app"
server: './app'
});
});
});
5 changes: 4 additions & 1 deletion recipes/gulp.browserify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
},
"license": "MIT",
"devDependencies": {
"exorcist": "^0.4.0",
"babelify": "^6.1.2",
"browser-sync": "^2.2.1",
"browserify": "^8.1.3",
"event-stream": "^3.3.1",
"exorcist": "^0.4.0",
"glob": "^5.0.14",
"gulp": "^3.8.11",
"gulp-util": "^3.0.3",
"mkdirp": "^0.5.1",
"vinyl-source-stream": "^1.0.0",
"watchify": "^2.3.0"
}
Expand Down
90 changes: 56 additions & 34 deletions recipes/gulp.browserify/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Installation/Usage:

To try this example, follow these 4 simple steps.
To try this example, follow these 4 simple steps.

**Step 1**: Clone this entire repo
```bash
Expand Down Expand Up @@ -41,49 +41,71 @@ var watchify = require('watchify');
var exorcist = require('exorcist');
var browserify = require('browserify');
var browserSync = require('browser-sync').create();

// Input file.
watchify.args.debug = true;
var bundler = watchify(browserify('./app/js/app.js', watchify.args));

// Babel transform
bundler.transform(babelify.configure({
sourceMapRelative: 'app/js'
}));

// On updates recompile
bundler.on('update', bundle);

function bundle() {

gutil.log('Compiling JS...');

return bundler.bundle()
.on('error', function (err) {
gutil.log(err.message);
browserSync.notify("Browserify Error!");
this.emit("end");
})
.pipe(exorcist('app/js/dist/bundle.js.map'))
.pipe(source('bundle.js'))
.pipe(gulp.dest('./app/js/dist'))
.pipe(browserSync.stream({once: true}));
}
var glob = require('glob');
var path = require('path');
var mkdirp = require('mkdirp');
var eventStream = require('event-stream');

/**
* Gulp task alias
* Creating multiple bundles with Watchify
*/
gulp.task('bundle', function () {
return bundle();
gulp.task('bundle', function (done) {
glob('./app/js/{app.js,worker/worker.js}', {}, function (err, files) {
if (err) {
done(err);
}

var tasks = files.map(function (entry) {
var relative = path.relative('./app/js', entry);

// A workaround for exorcist issue,
// where exorcist fails silently when the output dir does not exist
// see https://github.com/thlorenz/exorcist/issues/18
// and https://github.com/thlorenz/exorcist/pull/19
var sourceMapPath = './app/js/dist/' + relative + '.map';
mkdirp.sync(path.dirname(sourceMapPath));

// Input file.
watchify.args.debug = true;
var bundler = watchify(browserify(entry, watchify.args));

// Babel transform
bundler.transform(babelify.configure({
sourceMapRelative: 'app/js'
}));

var bundle = function () {
gutil.log('Compiling ' + entry + '...');

return bundler.bundle()
.on('error', function (err) {
gutil.log(err.message);
browserSync.notify('Browserify Error!');
this.emit('end');
})
.pipe(exorcist(sourceMapPath))
.pipe(source(relative))
.pipe(gulp.dest('./app/js/dist'))
.pipe(browserSync.stream({once: true}));
}

// On updates recompile
bundler.on('update', bundle);

return bundle();
});

eventStream.merge(tasks).on('end', done);
});
});

/**
* First bundle, then serve from the ./app directory
*/
gulp.task('default', ['bundle'], function () {
browserSync.init({
server: "./app"
server: './app'
});
});
```

```

0 comments on commit ccd5e89

Please sign in to comment.