Skip to content

Instantly share code, notes, and snippets.

@Raynos
Last active September 19, 2024 18:16
Show Gist options
  • Select an option

  • Save Raynos/8313682 to your computer and use it in GitHub Desktop.

Select an option

Save Raynos/8313682 to your computer and use it in GitHub Desktop.

Revisions

  1. Raynos renamed this gist Jan 8, 2014. 1 changed file with 37 additions and 4 deletions.
    41 changes: 37 additions & 4 deletions javascript.js → taskFile.js
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,62 @@
    var uglify = require('uglify');
    var uglifyJS = require('uglify-js');
    var glob = require('glob-stream').create;
    var watch = require('glob-watcher');
    var through = require('through');
    var fs = require('fs');
    var path = require('path');

    // uglify -.- wtf. make stream kind of.
    // gulp-uglify should be like this instead!
    function uglify() {
    var stream = through()

    // mikeal style pipe hack because uglify -.-
    stream.on('pipe', function (src) {
    var fileName = src.fileName
    fs.readFile(fileName, function (err, file) {
    if (err) {
    return stream.emit('error', err)
    }

    var payload

    try {
    payload = uglifyJS.minify(String(file), { fromString: true }).code
    } catch (err) {
    return stream.emit('error', err)
    }

    stream.end(payload)
    })
    })

    return stream
    }

    // gulp.src should be like this!
    function src(patterns) {
    return glob(patterns)
    .pipe(through(function (file) {
    var stream = fs.createReadStream(file.path)
    stream.fileName = path.filename(file.path)
    stream.fileName = file.path
    return stream
    }))
    }

    // gulp.dest should be like this!
    function dest(baseDir) {
    return through(function (fileStream) {
    fileStream.pipe(fs.createWriteStream(
    path.join(baseDir, fileStream.fileName)
    path.join(baseDir, path.filename(fileStream.fileName))
    ))
    })
    }

    // gulp.task is not needed, just use functions!
    var tasks = {
    'scripts': function () {
    src(['client/js/**/*.js', '!client/js/vendor/**'])
    .pipe(through(function (fileStream) {
    // assume uglify has a clean streaming interface -.-
    return fileStream.pipe(uglify());
    }))
    .pipe(dest('build/js'))
    @@ -58,5 +88,8 @@ var tasks = {
    }
    }

    // you don't need a special gulp command
    // just node taskFile.js scripts to run scripts.
    // or `alias gulp = node taskFile.js`
    var cmd = process.argv[3] || 'default';
    tasks[cmd]();
  2. Raynos revised this gist Jan 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion javascript.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ function src(patterns) {
    return glob(patterns)
    .pipe(through(function (file) {
    var stream = fs.createReadStream(file.path)
    stream.fileName = file.path
    stream.fileName = path.filename(file.path)
    return stream
    }))
    }
  3. Raynos revised this gist Jan 8, 2014. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions javascript.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,3 @@
    /*
    This is an EXAMPLE gulpfile.js
    You'll want to change it to match your project.
    Find plugins at https://npmjs.org/browse/keyword/gulpplugin
    */
    var uglify = require('uglify');
    var glob = require('glob-stream').create;
    var watch = require('glob-watcher');
  4. Raynos revised this gist Jan 8, 2014. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions javascript.js
    Original file line number Diff line number Diff line change
    @@ -6,12 +6,9 @@
    var uglify = require('uglify');
    var glob = require('glob-stream').create;
    var watch = require('glob-watcher');
    var argv = require('optimist').argv
    var through = require('through')
    var cmd = argv._[0] || 'default';
    var concat = require('some-concat-stream')
    var fs = require('fs')
    var path = require('path')
    var through = require('through');
    var fs = require('fs');
    var path = require('path');

    function src(patterns) {
    return glob(patterns)
    @@ -34,6 +31,7 @@ var tasks = {
    'scripts': function () {
    src(['client/js/**/*.js', '!client/js/vendor/**'])
    .pipe(through(function (fileStream) {
    // assume uglify has a clean streaming interface -.-
    return fileStream.pipe(uglify());
    }))
    .pipe(dest('build/js'))
    @@ -65,4 +63,5 @@ var tasks = {
    }
    }

    tasks[cmd]()
    var cmd = process.argv[3] || 'default';
    tasks[cmd]();
  5. Raynos revised this gist Jan 8, 2014. 1 changed file with 31 additions and 18 deletions.
    49 changes: 31 additions & 18 deletions javascript.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,28 @@

    /*
    This is an EXAMPLE gulpfile.js
    You'll want to change it to match your project.
    Find plugins at https://npmjs.org/browse/keyword/gulpplugin
    */
    var uglify = require('uglify');
    var glob = require('some-glob');
    var globWatch = require('glob-watch-thing');
    var glob = require('glob-stream').create;
    var watch = require('glob-watcher');
    var argv = require('optimist').argv
    var through = require('through')
    var cmd = argv._[0] || 'default';
    var concat = require('some-concat-stream')
    var fs = require('fs')
    var path = require('path')

    function sinkFileStream(baseDir) {
    function src(patterns) {
    return glob(patterns)
    .pipe(through(function (file) {
    var stream = fs.createReadStream(file.path)
    stream.fileName = file.path
    return stream
    }))
    }

    function dest(baseDir) {
    return through(function (fileStream) {
    fileStream.pipe(fs.createWriteStream(
    path.join(baseDir, fileStream.fileName)
    @@ -17,39 +32,37 @@ function sinkFileStream(baseDir) {

    var tasks = {
    'scripts': function () {
    glob(['client/js/**/*.js', '!client/js/vendor/**'])
    src(['client/js/**/*.js', '!client/js/vendor/**'])
    .pipe(through(function (fileStream) {
    return fileStream.pipe(uglify());
    }))
    .pipe(concat())
    .pipe(fs.createWriteStream('build/js'))
    .pipe(dest('build/js'))

    glob('client/js/vendor/**')
    .pipe(sinkFileStream('build/js/vendor'))
    src('client/js/vendor/**')
    .pipe(dest('build/js/vendor'))
    },
    'copy': function () {
    glob('client/img/**')
    .pipe(sinkFileStream('build/img'))
    src('client/img/**')
    .pipe(dest('build/img'))

    glob('client/css/**')
    .pipe(sinkFileStream('build/css'))
    src('client/css/**')
    .pipe(dest('build/css'))

    glob('client/*.html')
    .pipe(sinkFileStream('build'))
    src('client/*.html')
    .pipe(dest('build'))
    },
    'default': function () {
    tasks.scripts()
    tasks.copy()

    globWatch('client/js/**', tasks.scripts)
    watch('client/js/**', tasks.scripts)

    globWatch([
    watch([
    'client/img/**',
    'client/css/**',
    'client/*.html'
    ], tasks.copy)
    }
    }

    var cmd = process.argv[2] || 'default';
    tasks[cmd]()
  6. Raynos revised this gist Jan 8, 2014. 1 changed file with 2 additions and 7 deletions.
    9 changes: 2 additions & 7 deletions javascript.js
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,8 @@
    /*
    This is an EXAMPLE gulpfile.js
    You'll want to change it to match your project.
    Find plugins at https://npmjs.org/browse/keyword/gulpplugin
    */

    var uglify = require('uglify');
    var glob = require('some-glob');
    var globWatch = require('glob-watch-thing');
    var argv = require('optimist').argv
    var through = require('through')
    var cmd = argv._[0] || 'default';
    var concat = require('some-concat-stream')
    var fs = require('fs')
    var path = require('path')
    @@ -57,4 +51,5 @@ var tasks = {
    }
    }

    var cmd = process.argv[2] || 'default';
    tasks[cmd]()
  7. Raynos created this gist Jan 8, 2014.
    48 changes: 48 additions & 0 deletions gulpfile.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    /*
    This is an EXAMPLE gulpfile.js
    You'll want to change it to match your project.
    Find plugins at https://npmjs.org/browse/keyword/gulpplugin
    */
    var gulp = require('gulp');
    var uglify = require('gulp-uglify');

    gulp.task('scripts', function() {
    // Minify and copy all JavaScript (except vendor scripts)
    gulp.src(['client/js/**/*.js', '!client/js/vendor/**'])
    .pipe(uglify())
    .pipe(gulp.dest('build/js'));

    // Copy vendor files
    gulp.src('client/js/vendor/**')
    .pipe(gulp.dest('build/js/vendor'));
    });

    // Copy all static assets
    gulp.task('copy', function() {
    gulp.src('client/img/**')
    .pipe(gulp.dest('build/img'));

    gulp.src('client/css/**')
    .pipe(gulp.dest('build/css'));

    gulp.src('client/*.html')
    .pipe(gulp.dest('build'));
    });

    // The default task (called when you run `gulp`)
    gulp.task('default', function() {
    gulp.run('scripts', 'copy');

    // Watch files and run tasks if they change
    gulp.watch('client/js/**', function(event) {
    gulp.run('scripts');
    });

    gulp.watch([
    'client/img/**',
    'client/css/**',
    'client/*.html'
    ], function(event) {
    gulp.run('copy');
    });
    });
    60 changes: 60 additions & 0 deletions javascript.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    /*
    This is an EXAMPLE gulpfile.js
    You'll want to change it to match your project.
    Find plugins at https://npmjs.org/browse/keyword/gulpplugin
    */
    var uglify = require('uglify');
    var glob = require('some-glob');
    var globWatch = require('glob-watch-thing');
    var argv = require('optimist').argv
    var through = require('through')
    var cmd = argv._[0] || 'default';
    var concat = require('some-concat-stream')
    var fs = require('fs')
    var path = require('path')

    function sinkFileStream(baseDir) {
    return through(function (fileStream) {
    fileStream.pipe(fs.createWriteStream(
    path.join(baseDir, fileStream.fileName)
    ))
    })
    }

    var tasks = {
    'scripts': function () {
    glob(['client/js/**/*.js', '!client/js/vendor/**'])
    .pipe(through(function (fileStream) {
    return fileStream.pipe(uglify());
    }))
    .pipe(concat())
    .pipe(fs.createWriteStream('build/js'))

    glob('client/js/vendor/**')
    .pipe(sinkFileStream('build/js/vendor'))
    },
    'copy': function () {
    glob('client/img/**')
    .pipe(sinkFileStream('build/img'))

    glob('client/css/**')
    .pipe(sinkFileStream('build/css'))

    glob('client/*.html')
    .pipe(sinkFileStream('build'))
    },
    'default': function () {
    tasks.scripts()
    tasks.copy()

    globWatch('client/js/**', tasks.scripts)

    globWatch([
    'client/img/**',
    'client/css/**',
    'client/*.html'
    ], tasks.copy)
    }
    }

    tasks[cmd]()