Running 'gulp-preprocess' as part of 'gulp serve' task - gulp

I am basing my project off Google Web Starter Kit and am looking to integrate gulp-preprocess into the gulp pipeline.
I have managed to get it to work for the gulp serve:dist task, the relevant code is:
gulp.task('htmlIncludes', function() {
gulp.src('app/*.html')
.pipe(preprocess({context: { NODE_ENV: 'production', DEBUG: true}}))
.pipe(gulp.dest('./dist/'))
});
gulp.task('default', ['clean'], function (cb) {
runSequence('styles', ['jshint', 'html', 'images', 'fonts', 'copy', 'htmlIncludes'], cb);
});
However, I am having trouble getting it to work for the gulp:serve task which includes browser sync:
gulp.task('serve', ['styles'], function () {
browserSync({
notify: false,
server: ['.tmp', 'app']
});
I would like to add the htmlIncludes task, so that it is re-run when files are updated while running gulp:serve. However, simply adding it to the list which currently includes 'styles' does not have the desired effect. Any idea what I need to change?

You are totally right, you have to add it to the dependencies of this task to be run at least once. However, this is just half of the story. You have to run your task each time your HTML files have changed, so add it to the respective watch process:
gulp.task('serve', ['styles', 'htmlIncludes'], function () {
browserSync({
notify: false,
logPrefix: 'WSK',
server: ['.tmp', 'app']
});
// here's the change
gulp.watch(['.tmp/**/*.html', 'app/**/*.html'], ['htmlIncludes', reload]);
gulp.watch(['app/styles/**/**/**/**/*.{scss,css}'], ['styles', reload]);
gulp.watch(['app/scripts/**/*.js'], ['jshint']);
gulp.watch(['app/images/**/*'], reload);
});
You also see that this browserSync call just serves .tmp and app folders, while your output is stored in dist. So you have to change your htmlIncludes task, too:
gulp.task('htmlIncludes', function() {
return gulp.src('app/*.html')
.pipe(preprocess({context: { NODE_ENV: 'production', DEBUG: true}}))
.pipe(gulp.dest('./.tmp/'))
.pipe(gulp.dest('./dist/'))
});
If you need separate configurations for each output, we have to tackle it again. But for now it should work as planned.
It also might be possible that you have to run the 'html' task in sequence first, but I'm not that fit in the WSK Gulpfile to answer you that ;-)

Related

gulpfile.js - version 3 to 4 migration

Years back I setup vs code to somewhat replicate the current methods I was using to design my sites (using standalone apps). I decided at the time I would just stick to what I was using. Since those apps are no longer maintained I am coming across compiling issues now - the time has come to make the jump.
I am having trouble with my gulpfile.js which is from back when I originally tried this all out. I saved it in case I needed to return to using vs code. Problem is apparently this format no longer works because gulp has updated. All of this is basically foreign to me right now and while I understand what things are doing I don't understand enough to modify this to the current method for gulp 4^.
Any chance someone can help me out with this one? I've looked at the guides about series and parallel and so on. I guess it's easier for me to understand by looking at a working example.
my old gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
var cleanCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');
//processes the scss files in this folder
//minimizes them
gulp.task('sass', function () {
return gulp.src('_config/scss/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(cleanCSS())
.pipe(gulp.dest('assets/css'));
});
//minifies all js files in this folder
gulp.task('js', function () {
return gulp.src('_config/js/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('assets/js'));
});
//minifies all js files in this folder
gulp.task('scripts', function () {
return gulp.src('_config/scripts/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('assets/scripts'));
});
//creates 'watchers' that run tasks on specific activities
gulp.task('watch', function () {
gulp.watch('_config/scss/**/*.scss', ['sass']);
gulp.watch('_config/js/**/*.js', ['js']);
gulp.watch('_config/scripts/**/*.js', ['scripts']);
gulp.watch('_config/img/**/*', ['img']);
});
//this is the default task that runs everything
gulp.task('default', ['sass', 'js', 'scripts', 'watch']);
You are not that far from where you need to be. Change this code:
gulp.task('watch', function () {
gulp.watch('_config/scss/**/*.scss', ['sass']);
gulp.watch('_config/js/**/*.js', ['js']);
gulp.watch('_config/scripts/**/*.js', ['scripts']);
gulp.watch('_config/img/**/*', ['img']);
});
gulp.task('default', ['sass', 'js', 'scripts', 'watch']);
to
gulp.task('watch', function () {
gulp.watch('_config/scss/**/*.scss', gulp.series('sass'));
gulp.watch('_config/js/**/*.js', gulp.series('js'));
gulp.watch('_config/scripts/**/*.js', gulp.series('scripts'));
gulp.watch('_config/img/**/*', gulp.series('img'));
});
gulp.task('default', gulp.series('sass', 'js', 'scripts', 'watch'));
gulp.task now has this signature: gulp.task([taskName], taskFunction)
Before gulp v3 used an array of tasks as the second argument. gulp v4 uses a function, like gulp.series() or gulp.parallel(), as the second argument. And gulp.series() takes a list of tasks as its arguments. Since you used the gulp.task() method to create your tasks, the task names in series should appear as strings, like 'sass', 'js', etc.
Note: The preferred way to create tasks in v4 is as functions like:
function scripts() {
return gulp.src('_config/scripts/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('assets/scripts'));
});
Then you would use those function names in series as gulp.series(scripts, js) - not as strings. You should look into using this form of tasks.
gulp.watch() signature: gulp.watch(globs, [options], [task])
The [task] can be a single task name, like your 'sass' or a composed task, which just means one generated using series or parallel.
In your case, you are running only one task in each watch statement, so
gulp.watch('_config/scss/**/*.scss', 'sass');
should suffice. I showed them as composed tasks like:
gulp.watch('_config/scss/**/*.scss', gulp.series('sass'));
in case in the future you want to run more than one task upon a file change. In which case you could use something like:
gulp.watch('_config/scss/**/*.scss', gulp.series('sass', 'serve'));
for example.
Finally switch out gulp-uglify for gulp-terser. gulp-terser will handle es6 syntax that gulp-uglify cannot. gulp-terser

gulp stops server on error even with jshint included in gulpfile.js

I don't know why the server still stops whenever there's an error in my js files even though I have jshint in my gulpfile. I installed jshint and included it in my project because it reports errors in js files, but it's still failing. How can I fix this?
gulp.task('scripts', () => {
return gulp.src('assets/js/src/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish', {beep: true}))
.pipe(concat('main.js'))
.pipe(gulp.dest('assets/js/build/'))
.pipe(uglify())
.pipe(gulp.dest('assets/js/'))
.pipe(browserSync.stream({stream: true}));
});
gulp-jshint does what you says it does: it reports errors in JavaScript files. Nothing more, nothing less. It doesn't prevent defective JavaScript files from reaching later pipe stages like uglify() (which throws up and thus stops your server if there's any error in a JavaScript file).
If you want to prevent defective JavaScript files from wrecking your server, you need to put all the jshint stuff into it's own task and make sure that task fails when any JavaScript file has an error:
gulp.task('jshint', () => {
return gulp.src('assets/js/src/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish', {beep: true}))
.pipe(jshint.reporter('fail'))
});
Then you need to make your scripts task depend on that jshint task:
gulp.task('scripts', ['jshint'], () => {
return gulp.src('assets/js/src/*.js')
.pipe(concat('main.js'))
.pipe(gulp.dest('assets/js/build/'))
.pipe(uglify())
.pipe(gulp.dest('assets/js/'))
.pipe(browserSync.stream({stream: true}));
});
Now your scripts task will only run when the jshint task was successful. If any JavaScript file was defective jshint will output the error to the console while your server continues to run using the last good version of your JavaScript.
The simplest fix would be to use gulp-plumber to handle the error a little more gracefully:
var plumber = require("gulp-plumber");
gulp.task('scripts', () => {
return gulp.src('assets/js/src/*.js')
.pipe(plumber())
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish', {beep: true}))
.pipe(concat('main.js'))
.pipe(gulp.dest('assets/js/build/'))
.pipe(uglify())
.pipe(gulp.dest('assets/js/'))
.pipe(browserSync.stream({stream: true}));
});
Personally, I don't like that solution because it will prevent your minified file from being updated. Here's what I would recommend:
var jshintSuccess = function (file) {
return file.jshint.success;
}
gulp.task('scripts', () => {
return gulp.src('assets/js/src/*.js')
.pipe(sourcemaps.init())
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish', {
beep: true
}))
.pipe(gulpif(jshintSuccess, uglify()))
.pipe(concat('main.js'))
.pipe(sourcemaps.write('maps'))
.pipe(gulp.dest('assets/js/'))
.pipe(browserSync.stream({
stream: true
}));
});
First, notice that I'm not writing to multiple destinations. Instead, I'm using sourcemaps so that you don't need unminified code. Second, I'm using gulp-if to conditionally pipe your code through uglify based on the results of jshint. Code with errors will bypass uglify so that it still makes it into to your destination file.
Now, you can inspect and debug it with the developer tools.
Note: I recommend this for local development only. I wouldn't connect this to a continuous integration pipeline because you'll only want good code to make it into production. Either set up a different task for that or add another gulp-if condition to prevent broken code from building based on environment variables.

Gulp not watching correctly

I'm new to using gulp and I think I have it setup correctly, but it does not seem to be doing what it should be doing.
My gulpfile.js has
gulp.task('compass', function() {
return gulp.src('sites/default/themes/lsl_theme/sass/**/*.scss')
.pipe(compass({
config_file: 'sites/default/themes/lsl_theme/config.rb',
css: 'css',
sass: 'scss'
}))
.pipe(gulp.dest('./sites/default/themes/lsl_theme/css'))
.pipe(notify({
message: 'Compass task complete.'
}))
.pipe(livereload());
});
with
gulp.task('scripts', function() {
return gulp.src([
'sites/default/themes/lsl_theme/js/**/*.js'
])
.pipe(plumber())
.pipe(concat('lsl.js'))
.pipe(gulp.dest('sites/default/themes/lsl_theme/js'))
// .pipe(stripDebug())
.pipe(uglify('lsl.js'))
.pipe(rename('lsl.min.js'))
.pipe(gulp.dest('sites/default/themes/lsl_theme/js'))
.pipe(sourcemaps.write())
.pipe(notify({
message: 'Scripts task complete.'
}))
.pipe(filesize())
.pipe(livereload());
});
and the watch function
gulp.task('watch', function() {
livereload.listen();
gulp.watch('./sites/default/themes/lsl_theme/js/**/*.js', ['scripts']);
gulp.watch('./sites/default/themes/lsl_theme/sass/**/*.scss', ['compass']);
});
when I run gulp, the result is
[16:14:36] Starting 'compass'...
[16:14:36] Starting 'scripts'...
[16:14:36] Starting 'watch'...
[16:14:37] Finished 'watch' after 89 ms
and no changes are registered.
for file structure, my gulpfile.js is in the root directory and the sass, css, and js are all in root/sites/default/themes/lsl_theme with the sass folder containing the folder 'components' full of partials.
My assumption is that you are on windows? Correct me if I'm wrong.
There is this problem that gulp-notify tends to break the gulp.watch functions. Try commenting out
// .pipe(notify({
// message: 'Scripts task complete.'
// }))
and see if the problem still exists.
If that does fix the issue, a solution from this thread may be helpful.
You can use the gulp-if
plugin in combination with
the os node module
to determine if you are on Windows, then exclude gulp-notify, like
so:
var _if = require('gulp-if');
//...
// From https://stackoverflow.com/questions/8683895/variable-to-detect-operating-system-in-node-scripts
var isWindows = /^win/.test(require('os').platform());
//...
// use like so:
.pipe(_if(!isWindows, notify('Coffeescript compile successful')))
It turns out that a large part of my issue was just simply being a rookie with Gulp. When I removed 'scripts' from my gulp watch it started working.
I then made the connection that it was watching the same directory that it was placing the new concatenated and minified js files in so it was putting the new file, checking that file, and looping over and over causing memory issues as well as not allowing 'compass' to run.
After creating a 'dest' folder to hold the new js everything started working just peachy.

Gulp "watch" is not running the sub task "sass" on file change

I am using Gulp for watch and sass complier. When I start "watch" first time then "sass" complier runs and its create the css files as per given path. However when I change the .scss files then it doesn't call "sass" complier again. Following is is my these two tasks and variables.
gulp.task('sass', function () {
gulp.src(config.sassPath)
.pipe(sass())
.pipe(gulp.dest(config.cssPath))
.pipe(livereload());
});
gulp.task('watch', false, function () {
livereload.listen(8189);
gulp.src(config.watchPaths)
.pipe(watch(config.watchPaths, function (event) {
gulp.start( 'sass', 'js-hint', 'server','test');
livereload();
}))
.pipe(livereload());
});
Following command i use to run "watch" task
gulp watch
I do see "watch" is reloading when I am changing the .scss file. Following is log for this.
[19:49:30] public/sass/html-controls.scss was changed
[19:49:30] /Users/dkuma204/Desktop/Dilip/Projects/OPEN/SourceCode/AWF/OPENApp/application/public/sass/html-controls.scss reloaded.
Not sure what I am missing here. Please help.
Why it is so complicated? Try this:
gulp.task('watch', false, function () {
livereload.listen(8189);
gulp.watch(config.watchPaths,['sass', 'js-hint', 'server', 'test'])
});
And your every task which requires livereload should have .pipe(livereload()) at the end.
You shouldn't use gulp start. Here is one of comment from github discussion:
gulp.start is undocumented on purpose because it can lead to
complicated build files and we don't want people using it

How to temporarily disable browsersync?

How can I temporarily disable browsersync, so that it doesn't inject/modify HTML pages? (For testing and debugging.)
There doesn't seem to be a configuration option to do just this, but one hacky workaround is to use snippetOptions to specify a string that will will never be found in the HTML:
snippetOptions: {
rule: {
match: /qqqqqqqqq/
}
}
If this string cannot be found in the HTML, the snippet will never be injected, and browsersync will be inert.
You can use Yargs to send parameters and enable or disable 'watch' task.
Remember this command line to install required components:
npm i --save gulp browser-sync yargs runSequence
On gulpfile.js file:
browserSync.init({
port: 80,
notify: false,
cors: true,
browser: 'chrome',
open: 'local'
});
gulp.task('watch', ['browserSync'], function (){
gulp.watch('dev/*.html', browserSync.reload);
gulp.watch('dev/**/*.js', browserSync.reload);
gulp.watch('dev/**/*.css', browserSync.reload);
});
gulp.task('default', function(callback) {
var sequence = ['browserSync'];
if (args.sync){
sequence.push('watch')
}
runSequence(sequence,callback);
});
This if (args.sync) lines use truthy/falsy searching for sync values and enable/disable 'watch' task.
BrowserSync with watch:
gulp --sync true
BrowserSync without watch:
gulp or gulp --sync false