Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

Commit

Permalink
Fix injecting CSS changes into browser
Browse files Browse the repository at this point in the history
Injecting CSS changes wouldn't work with the split tasks for BrowserSync
and SASS, so I combined all the asset generating into a assets.js task
and injecting works as it should now.

Fixes sondr3/generator-jekyllized#143.
  • Loading branch information
sondr3 committed Jul 28, 2016
1 parent 5ccc069 commit fc7bda9
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 176 deletions.
110 changes: 110 additions & 0 deletions generators/gulp/templates/tasks/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
'use strict';
const argv = require('yargs').argv;
const autoprefixer = require('autoprefixer');<% if (babel) { %>
const babel = require('gulp-babel');<% } %>
const browserSync = require('browser-sync').create();
const concat = require('gulp-concat');
const cssnano = require('gulp-cssnano');
const gulp = require('gulp');
const gzip = require('gulp-gzip');
const newer = require('gulp-newer');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const rev = require('gulp-rev');
const sass = require('gulp-sass');
const size = require('gulp-size');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const when = require('gulp-if');

// 'gulp scripts' -- creates a index.js file from your JavaScript files and
// creates a Sourcemap for it
// 'gulp scripts --prod' -- creates a index.js file from your JavaScript files,
// minifies, gzips and cache busts it. Does not create a Sourcemap
gulp.task('scripts', () =>
// NOTE: The order here is important since it's concatenated in order from
// top to bottom, so you want vendor scripts etc on top
gulp.src([
'src/assets/javascript/vendor.js',
'src/assets/javascript/main.js'
])
.pipe(newer('.tmp/assets/javascript/index.js', {dest: '.tmp/assets/javascript', ext: '.js'}))
.pipe(when(!argv.prod, sourcemaps.init()))<% if (babel) { %>
.pipe(babel({
presets: ['es2015']
}))<% } %>
.pipe(concat('index.js'))
.pipe(size({
showFiles: true
}))
.pipe(when(argv.prod, rename({suffix: '.min'})))
.pipe(when(argv.prod, when('*.js', uglify({preserveComments: 'some'}))))
.pipe(when(argv.prod, size({
showFiles: true
})))
.pipe(when(argv.prod, rev()))
.pipe(when(!argv.prod, sourcemaps.write('.')))
.pipe(when(argv.prod, gulp.dest('.tmp/assets/javascript')))
.pipe(when(argv.prod, when('*.js', gzip({append: true}))))
.pipe(when(argv.prod, size({
gzip: true,
showFiles: true
})))
.pipe(gulp.dest('.tmp/assets/javascript'))
);

// 'gulp styles' -- creates a CSS file from your SASS, adds prefixes and
// creates a Sourcemap
// 'gulp styles --prod' -- creates a CSS file from your SASS, adds prefixes and
// then minwhenies, gzips and cache busts it. Does not create a Sourcemap
gulp.task('styles', () =>
gulp.src('src/assets/scss/style.scss')
.pipe(when(!argv.prod, sourcemaps.init()))
.pipe(sass({
precision: 10
}).on('error', sass.logError))
.pipe(postcss([
autoprefixer({browsers: 'last 1 version'})
]))
.pipe(size({
showFiles: true
}))
.pipe(when(argv.prod, rename({suffix: '.min'})))
.pipe(when(argv.prod, when('*.css', cssnano({autoprefixer: false}))))
.pipe(when(argv.prod, size({
showFiles: true
})))
.pipe(when(argv.prod, rev()))
.pipe(when(!argv.prod, sourcemaps.write('.')))
.pipe(when(argv.prod, gulp.dest('.tmp/assets/stylesheets')))
.pipe(when(argv.prod, when('*.css', gzip({append: true}))))
.pipe(when(argv.prod, size({
gzip: true,
showFiles: true
})))
.pipe(gulp.dest('.tmp/assets/stylesheets'))
.pipe(when(!argv.prod, browserSync.stream()))
);

// Function to properly reload your browser
function reload(done) {
browserSync.reload();
done();
}
// 'gulp serve' -- open up your website in your browser and watch for changes
// in all your files and update them when needed
gulp.task('serve', (done) => {
browserSync.init({
// tunnel: true,
// open: false,
server: ['.tmp', 'dist']
});
done();

// Watch various files for changes and do the needful
gulp.watch(['src/**/*.md', 'src/**/*.html', 'src/**/*.yml'], gulp.series('build:site', reload));
gulp.watch(['src/**/*.xml', 'src/**/*.txt'], gulp.series('site', reload));
gulp.watch('src/assets/javascript/**/*.js', gulp.series('scripts', reload));
gulp.watch('src/assets/scss/**/*.scss', gulp.series('styles'));
gulp.watch('src/assets/images/**/*', gulp.series('images', reload));
});
26 changes: 0 additions & 26 deletions generators/gulp/templates/tasks/browsersync.js

This file was deleted.

49 changes: 0 additions & 49 deletions generators/gulp/templates/tasks/scripts.js

This file was deleted.

45 changes: 0 additions & 45 deletions generators/gulp/templates/tasks/style.js

This file was deleted.

4 changes: 1 addition & 3 deletions test/gulp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ test('creates comment about creation', () => {

test('creates gulp task files', () => {
assert.file([
'gulp/tasks/browsersync.js',
'gulp/tasks/assets.js',
'gulp/tasks/clean.js',
'gulp/tasks/copy.js',
'gulp/tasks/fonts.js',
'gulp/tasks/html.js',
'gulp/tasks/images.js',
'gulp/tasks/inject.js',
'gulp/tasks/scripts.js',
'gulp/tasks/style.js',
'gulp/tasks/uploading.js'
]);
});
Expand Down
17 changes: 11 additions & 6 deletions test/gulp/browsersync.js → test/gulp/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ test.before(() => {
.toPromise();
});

test('creates browsersync.js', () => {
assert.file('gulp/tasks/browsersync.js');
test('creates assets.js', () => {
assert.file('gulp/tasks/assets.js');
});

test('contains correct tasks', () => {
[
'function reload',
'gulp.task(\'serve'
].forEach(field => {
assert.fileContent('gulp/tasks/browsersync.js', field);
'scripts',
'styles',
'serve'
].forEach(function (task) {
assert.fileContent('gulp/tasks/assets.js', 'gulp.task(\'' + task);
});
});

test('contains reload function', () => {
assert.fileContent('gulp/tasks/assets.js', 'function reload');
});
6 changes: 3 additions & 3 deletions test/gulp/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ test('creates gulpfile.js', () => {
assert.file('gulpfile.js');
});

test('creates scripts.js', () => {
assert.file('gulp/tasks/scripts.js');
test('creates assets.js', () => {
assert.file('gulp/tasks/assets.js');
});

test('contains babel', () => {
[
'const babel',
'.pipe(babel({'
].forEach(field => {
assert.fileContent('gulp/tasks/scripts.js', field);
assert.fileContent('gulp/tasks/assets.js', field);
});
});

Expand Down
22 changes: 0 additions & 22 deletions test/gulp/scripts.js

This file was deleted.

22 changes: 0 additions & 22 deletions test/gulp/style.js

This file was deleted.

0 comments on commit fc7bda9

Please sign in to comment.