Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var gulp = require('gulp');
- var browserSync = require('browser-sync').create();
- var path = require('path');
- var fs = require('fs');
- var sourcemaps = require('gulp-sourcemaps');
- var source = require('vinyl-source-stream');
- var buffer = require('vinyl-buffer');
- var browserify = require('browserify');
- var watchify = require('watchify');
- var babel = require('babelify');
- var exit = require('gulp-exit');
- //
- // Vars
- var _publicDir = './public';
- var _sourceDir = './source';
- var _scriptSource = _sourceDir + '/js';
- var _scriptPublic = _publicDir + '/js';
- //
- // Babel
- function compile(watch) {
- var bundler = bundler = watchify(
- browserify({
- // Define the entry point for our application
- entries: [_scriptSource + '/main.js'],
- // Debugging is nice
- debug: true,
- // Allow importing from the following extensions
- extensions: [' ', 'js']
- }).transform(babel.configure({
- // Use all of the ES2015 spec
- presets: ["es2015"]
- }))
- );
- function rebundle() {
- return bundler.bundle()
- .on('error', function(err) { console.error(err); this.emit('end'); })
- .pipe(source('main.js'))
- .pipe(buffer())
- .pipe(sourcemaps.init({ loadMaps: true }))
- .pipe(sourcemaps.write('./'))
- .pipe(gulp.dest(_scriptPublic))
- .pipe(browserSync.stream());
- }
- if (watch) {
- bundler.on('update', function() {
- console.log('-> re-bundling...');
- rebundle();
- });
- rebundle()
- }else {
- rebundle().pipe(exit());
- }
- }
- function watch() {
- return compile(true);
- }
- gulp.task('build',function () {
- // compile JS
- compile(false);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement