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

Add multiple-file support to recipe gulp.browserify #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
3 changes: 2 additions & 1 deletion recipes/gulp.browserify/app/js/dist/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.js
*.js
*.js.map
15 changes: 0 additions & 15 deletions recipes/gulp.browserify/app/js/dist/bundle.js.map

This file was deleted.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK an empty object as the second object is optional.

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use asynchronous mkdirp.


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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use merge-stream instead.

});
});

/**
* 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watchify -> 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use merge-stream instead.

});
});

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

```