So I've been working on this project for a little while and I wanted to add my own css into the project. I'm trying to add a new scss file to the project and have it get built into the main css file, however Gulp doesn't want to import my scss file.
Here is the gulp code
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var watch = require('gulp-watch');
var batch = require('gulp-batch');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var webserver = require('gulp-webserver');
var paths = {
sass: ['src/scss/**'],
js: ['src/ng/_declare.js', 'src/ng/**'], //_declare.js needs to be first, so it gets added to bundle first.
static: ['src/js/**'],
core: ['./td_frontend_shared/**']
};
gulp.task('default', ['watch-dev']);
gulp.task('publish-dev', function(done){
});
gulp.task('sass', function(done) {
gulp.src('src/scss/import.scss')
.pipe(sass())
.on('error', function(err){
console.log('error building sass', err);
done();
})
.pipe(gulp.dest('public/dist/'))
.pipe(minifyCss({
keepSpecialComments: 0
}))
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest('public/dist/'))
.on('end', done);
});
Any help would be greatly appreciated!
You need to add the task in default if you want it run right away on using gulp in command line. You can do it like this...
gulp.task( 'default', function () {
gulp.start(
'sass'
);
} );
Try my repository for using SASS in HTML projects, it supports babel(optional) and auto reloads page when you change scss files ;)
https://github.com/shramee/gulp-sass-babel
Related
When I use gulp styles it just pipes the initial code to the destination without autoprefixing and when I'm trying to use gulp watch on styles function it starts but never ends.
I'm using Gulp 4
gulpfile
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
gulp.task('styles', function() {
return new Promise(function(resolve, reject){
gulp.src('style.css')
.pipe(autoprefixer())
.pipe(gulp.dest('build'))
resolve();
});
});
gulp.task('watch', function() {
gulp.watch('style.css', gulp.series('styles'));
});
Terminal output
PS C:\Users\OneDrive\testGulp> gulp watch
[19:00:23] Using gulpfile ~\OneDrive\testGulp\gulpfile.js
[19:00:23] Starting 'watch'...
File tree structure
I'm pretty new to Gulp, but I thought I had a handle on it, until I inherited a WordPress theme that needed some development work on.
I've been compiling my styles with no issues until it came to compiling the editor-styles.scss
This is the basic file structure for my styles:
theme-name
_static
styles
_admin
editor-styles.scss
editor-styles.min.css
admin-styles.scss
admin-styoles.min.css
login-styles.scss
login-styles.min.css
_layout
_block.scss
_header.scss
_utils
_mixins.scss
_variables.scss
styles.scss
styles.css
All changes to any scss file in the _layout and _utils folders are compiled correctly to the styles.css in the main index.
When I edit an scss file in the _admin folder, I need it to compile to the .min.css version in that same folder.
Instead, it's compiling and creating a new _admin folder in the main theme index with .css files.
I know why this is, because that is what I have told my compiling to do in my gulpfile.js
However, I can't get my head around how to split these two sources?
This is my gulpfile.js:
// MODULES
var gulp = require('gulp');
var sass = require('gulp-sass');
var js = require('gulp-uglify');
var imagemin = require('gulp-imagemin');
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var plumber = require('gulp-plumber');
var gutil = require('gulp-util');
var autoprefixer = require('gulp-autoprefixer');
var sourcemaps = require('gulp-sourcemaps');
var rename = require('gulp-rename');
var concat = require('gulp-concat');
// COMPILE AND MINIFY SASS
gulp.task('sass', function () {
return gulp.src('./wp-content/themes/theme-name/_static/styles/**/*.scss')
.pipe(plumber())
.pipe(sass.sync().on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./wp-content/themes/theme-name'))
.pipe(browserSync.stream())
done();
});
// MINIFY JS
gulp.task('js', function () {
return gulp.src('./wp-content/themes/theme-name/_static/js/*.js')
.pipe(js())
.on('error', onError)
.pipe(rename({
suffix: '.min'
}))
.pipe(concat('all.min.js'))
.pipe(gulp.dest('./wp-content/themes/theme-name/_static/js/min/'))
.pipe(browserSync.stream())
done();
});
// IMAGES
gulp.task('imagemin', function () {
return gulp.src('./wp-content/themes/theme-name/*')
.pipe(imagemin())
.pipe(gulp.dest('./wp-content/themes/theme-name/images'))
done();
});
// BROWSERSYNC
gulp.task('browser-sync', function(done) {
browserSync.init({
proxy: 'http://localhost/project-name'
})
done();
});
// RELOAD
gulp.task('reload', function (done) {
browserSync.reload();
done();
});
// ERROR LOG
var onError = function (err) {
console.log('An error occurred:', gutil.colors.magenta(err.message));
gutil.beep();
this.emit('end');
};
// WATCH
gulp.task('watch', function() {
gulp.watch('./wp-content/themes/theme-name/_static/styles/**/*.scss', gulp.series('sass'));
gulp.watch('./wp-content/themes/theme-name/_static/js/*.js', gulp.series('js'));
gulp.watch('./wp-content/themes/theme-name/images/*', gulp.series('imagemin'));
gulp.watch('./wp-content/themes/theme-name/**/*.php').on('change', browserSync.reload);
gulp.watch('*/theme-name/**/*.php').on('change', browserSync.reload);
});
gulp.task('default', gulp.series('sass', 'js', 'imagemin', 'browser-sync', 'watch'));
Thanks in advance for any help/advice - I'm banging my head against a wall and I'm sure it's something super simple I've overlooked.
There is more than one way to accomplish what you want. You could have separate watch statements triggering separate sass statements for your different directories. The advantage to that is you are not running all your .scss files everytime.
But consider this. Using the gulp-if plugin which allows you to make if/else decisions mid-stream.
const gulpif = require('gulp-if');
const path = require('path'); // needed to get the last directory name in a path
const condition = function (file) {
const match = path.basename(path.dirname(file.path));
return match === "_admin";
}
// COMPILE AND MINIFY SASS
gulp.task('sass', function () {
return gulp.src('./_static/styles/**/*.scss')
.pipe(plumber())
.pipe(sass.sync().on('error', sass.logError))
.pipe(sourcemaps.write())
// if condition is true use first gulp.dest, else use second
// condition is true if current file parent directory is _admin
.pipe(gulpif(condition, gulp.dest('./_static/styles/'), gulp.dest('./')))
.pipe(browserSync.stream());
// done(); // not needed with the return
});
The disadvantage of this method is that all .scss files are re-compiled each time.
If anyone needs to know how to run tasks for separate src and destinations, this is what works for me :) Thanks for your help Mark.
// This is my main sass files.
// It compiles all my sass files, EXCEPT any files in the _admin folder
// and compiles them to styles.css in my main theme index/root
gulp.task('sass', function () {
return gulp.src([
'./wp-content/themes/theme-name/_static/styles/**/*.scss',
'!./wp-content/themes/theme-name/_static/styles/_admin/*.scss'
])
.pipe(plumber())
.pipe(sass.sync().on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./wp-content/themes/andra-birkerts-design'))
.pipe(browserSync.stream())
done();
});
// This is my main admin sass file.
// It compiles just the sass files in the _admin folder
// and compiles them to filename.min.css in the same _admin folder
gulp.task('admin_sass', function () {
return gulp.src('./wp-content/themes/theme-name/_static/styles/_admin/*.scss')
.pipe(plumber())
.pipe(sass.sync().on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./wp-content/themes/theme-name/_static/styles/_admin'))
.pipe(browserSync.stream())
done();
});
gulp.task('default', gulp.series('sass', 'admin_sass', 'browser-sync', 'watch'));
In my gulfile.js, I'm using watch to compile scss files to css files. It runs without error and when I run gulp in powershell, it says that it is watching, but whenever I define a style in my scss file, it doesn't affect the css file which is created and I have to stop gulp and run it again. Actually, it's not watching carefully !
Here is my code.
Thanks for any help.
"use strict";
var gulp = require("gulp");
var sass = require("gulp-sass");
var minifyCSS = require("gulp-clean-css");
var uglify = require("gulp-uglify");
var rename = require("gulp-rename");
var changed = require("gulp-changed");
var { task, series } = require("gulp");
var SCSS_SRC = "./src/Assets/scss/**/*.scss";
var SCSS_DEST = "./src/Assets/css";
task("compile_scss", function() {
gulp
.src(SCSS_SRC)
.pipe(sass().on("error", sass.logError))
.pipe(minifyCSS())
.pipe(rename({ suffix: ".min" }))
.pipe(changed(SCSS_DEST))
.pipe(gulp.dest(SCSS_DEST));
});
task("watch_scss", function() {
gulp.watch(SCSS_SRC, series(["compile_scss"]));
});
task("default", series("watch_scss"));
You missed a return. The gulp doesn't know the task has completed. In your case you have to return the stream. Here is how your code should be:
"use strict";
var gulp = require("gulp");
var sass = require("gulp-sass");
var minifyCSS = require("gulp-clean-css");
var uglify = require("gulp-uglify");
var rename = require("gulp-rename");
var changed = require("gulp-changed");
var { task, series } = require("gulp");
var SCSS_SRC = "./src/Assets/scss/**/*.scss";
var SCSS_DEST = "./src/Assets/css";
function compile_scss() {
return gulp
.src(SCSS_SRC)
.pipe(sass().on("error", sass.logError))
.pipe(minifyCSS())
.pipe(rename({ suffix: ".min" }))
.pipe(changed(SCSS_DEST))
.pipe(gulp.dest(SCSS_DEST));
}
function watch_scss() {
gulp.watch(SCSS_SRC, series(compile_scss));
}
exports.default = series(watch_scss);
I also took the liberty of removing the "task()"s since
Reminder: This API isn't the recommended pattern anymore - export your tasks.
as said here.
For the problem be solved, as #Gabriel mentioned and according this link, I changed my code to this and the problem solved. But when I ran npm start, the watch stopped working; Therefore, I installed "concurrently" through npm and changed my start in package.json as mentioned here:
npm install concurrently
"start": "concurrently \"react-scripts start \" \"gulp \"",
"use strict";
var gulp = require("gulp");
var sass = require("gulp-sass");
var minifyCSS = require("gulp-clean-css");
var uglify = require("gulp-uglify");
var rename = require("gulp-rename");
var changed = require("gulp-changed");
var { task, series } = require("gulp");
var SCSS_SRC = "./src/Assets/scss/**/*.scss";
var SCSS_DEST = "./src/Assets/css";
function compile_scss() {
return gulp
.src(SCSS_SRC)
.pipe(sass().on("error", sass.logError))
.pipe(minifyCSS())
.pipe(rename({ suffix: ".min" }))
.pipe(changed(SCSS_DEST))
.pipe(gulp.dest(SCSS_DEST));
}
function watch_scss() {
gulp.watch(SCSS_SRC, series(compile_scss));
}
exports.default = series(watch_scss);
I have an Ionic V1 app and I did ionic setup sass.
I've added the scss in watchPatterns in ionic.config.json. Gulp detects the file change but doesn't compile the scss into the css. Tried a lot of things.
I did ionic setup sass twice. It seems that the sass only compiles into css once I do ionic setup sass.
Here's the gulpfile.js
var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sh = require('shelljs');
var paths = {
sass: ['./scss/**/*.scss']
};
gulp.task('default', ['sass']);
gulp.task('sass', function(done) {
gulp.src('./scss/ionic.app.scss')
.pipe(sass())
.on('error', sass.logError)
.pipe(gulp.dest('./www/css/'))
.pipe(minifyCss({
keepSpecialComments: 0
}))
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest('./www/css/'))
.on('end', done);
});
gulp.task('watch', function() {
gulp.watch(paths.sass, ['sass']);
});
gulp.task('install', ['git-check'], function() {
return bower.commands.install()
.on('log', function(data) {
gutil.log('bower', gutil.colors.cyan(data.id), data.message);
});
});
gulp.task('git-check', function(done) {
if (!sh.which('git')) {
console.log(
' ' + gutil.colors.red('Git is not installed.'),
'\n Git, the version control system, is required to download Ionic.',
'\n Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.',
'\n Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
);
process.exit(1);
}
done();
});
I updated to IONIC 2 few days and have the same problem.
Just add these 2 lines to your gulpfile.js:
gulp.task('serve:before', ['default','watch']);
gulp.task('run:before', ['default']);
It's working for me.
I had similar issue and the problem started, when I had upgraded ionic-cli to version 2.x. So I would suggest you to downgrade back to version 1.x.x.; for example I am using last subversion:
npm install -g ionic#1.7.16
For me, now everything works as it was before the upgrade (I am using same gulp file, I also rename "ionic.config.json" back to "ionic.project"). Hope it helps!
Check TomiL's answer, it might be able to help you. If not, try running two cmd's, first type ionic serve, and in the other one type gulp default, or whatever your task name is. here's my gulpfile
var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sh = require('shelljs');
var paths = {
sass: ['./scss/*.scss']
};
gulp.task('default', ['sass']);
gulp.task('sass', function(done) {
gulp.src('./scss/ionic.app.scss')
.pipe(sass())
.on('error', sass.logError)
.pipe(gulp.dest('./www/css/'))
.pipe(minifyCss({
keepSpecialComments: 0
}))
.pipe(rename({ extname: '.min.css' }))
.pipe(gulp.dest('./www/css/'))
.on('end', done);
});
gulp.task('watch', function() {
gulp.watch(paths.sass, ['sass']);
});
gulp.task('install', ['git-check'], function() {
return bower.commands.install()
.on('log', function(data) {
gutil.log('bower', gutil.colors.cyan(data.id), data.message);
});
});
gulp.task('git-check', function(done) {
if (!sh.which('git')) {
console.log(
' ' + gutil.colors.red('Git is not installed.'),
'\n Git, the version control system, is required to download Ionic.',
'\n Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.',
'\n Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
);
process.exit(1);
}
done();
});
I've got the same issue. I found out that it's because gulp doesn't compile scss files before 'ionic serve', so just add the task below solved the problem:
gulp.task('serve:before', ['default']);
I have a gulp 'default' task where I want to clean a folder before gulp continues to build my minified CSS and JS. This 'clean' task needs to run only once per default task. But I am having issues trying to get default task to reference the real build tasks.
So here is my gulpfile:
var gulp = require('gulp');
// including our plugins
var clean = require('gulp-clean');
var less = require('gulp-less');
var util = require('gulp-util');
var lsourcemaps = require('gulp-sourcemaps');
var rename = require('gulp-rename');
var filesize = require('gulp-filesize');
var ugly = require('gulp-uglify');
var path = require('path');
var plumber = require('gulp-plumber');
var minifyCSS = require('gulp-minify-css');
var concat = require('gulp-concat');
// DEFAULT TASK
gulp.task('default', ['clean'], function() {
.pipe(gulp.task('vendor'))
.pipe(gulp.task('css'))
});
// strips public folder for a build operation nice and clean ** obliterates! **
gulp.task('clean', function() {
return gulp.src('public/**', {read: false})
.pipe(clean());
});
// javascript vendor builds
gulp.task('vendor', function() {
return gulp.src(['bower_comps/angular/angular.js', 'bower_comps/angular-bootstrap/ui-bootstrap.js', 'bower_comps/angular-bootstrap/ui-bootstrap-tpls.js'])
//.pipe(filesize())
.pipe(ugly())
.pipe(concat('vendor.min.js'))
.pipe(gulp.dest('public/js'))
});
// builds CSS
gulp.task('css', function() {
return gulp.src('bower_comps/bootstrap-less/less/bootstrap.less')
.pipe(lsourcemaps.init())
.pipe(plumber({
errorHandler: function(err) {
console.log(err);
this.emit('end')
}
}))
.pipe(less({
paths: [path.join(__dirname, 'less', 'includes')]
}))
.pipe(minifyCSS())
.pipe(rename('site.min.css'))
.pipe(lsourcemaps.write('./maps'))
.pipe(gulp.dest('public/css/'))
.pipe(filesize())
});
So how am I going about this wrong?? Each of the individual tasks will run on their own just fine "gulp css", "gulp vendor". Its just when I put them into a default task (master task) with a 'pre-requisite' task of my clean that I run into problems.
Tony
Try these for your tasks:
gulp.task('clean', function() {
// Insert cleaning code here
});
gulp.task('vendor', ['clean'], function() {
// Insert your 'vendor' code here
});
gulp.task(‘css’, ['clean'], function() {
// insert your 'css' code here
});
gulp.task('build', [‘vendor’, ‘css’]);
gulp.task('default', ['build']);
'vendor' and 'css' will run concurrently only after 'clean' has finished. 'clean' will only run once despite being a prerequisite for both 'vendor' and 'css'.
The Problem ist when you call the default task, gulp randomly chooses the order of the task:
gulp.task('default', ['clean', 'move', 'scripts', 'css']);
To solve this problem each task should have dependencies. For example the move task should be performed after the clean task. So the move taks should look like this:
gulp.task('move', ['clean'], function () { //your code }
for more explanation: https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulptaskname-deps-fn
sorry for my bad english :-)