I used this gulp file until recently. Now everything still works, the CSS still gets compiled except injecting the compiled CSS back into the browser.
Thanks for your time!
var gulp = require('gulp');
var gutil = require('gulp-util');
var bs = require('browser-sync').create();
// Include plugins
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var less = require('gulp-less');
var minifycss = require('gulp-clean-css');
var streamqueue = require('streamqueue');
gulp.task('bs', function () {
var files = [
'libraries/file1.css',
];
bs.init(files, {
proxy: 'localhost',
port: '80',
baseDir: './',
startPath: 'joomla'
})
gulp.watch("libraries/file1.css", ['fcss']);
});
gulp.task('fcss', function() {
var themes = ['.theme1','.theme2', '.theme3'];
themes.forEach( function(theme) {
return gulp.src(['libraries/'+theme+'.css',
'libraries/file1.css'
])
.pipe(concat('style'+theme+'.css'))
.pipe(gulp.dest('media/css'))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss({advanced:false, keepSpecialComments : 0}))
.pipe(gulp.dest('media/css'))
.pipe(bs.stream());
});
});
Try running browsersync before you minify css:
gulp.task('fcss', function() {
var themes = ['.theme1','.theme2', '.theme3'];
themes.forEach( function(theme) {
return gulp.src(['libraries/'+theme+'.css',
'libraries/file1.css'
])
.pipe(concat('style'+theme+'.css'))
.pipe(bs.stream())
.pipe(gulp.dest('media/css'))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss({advanced:false, keepSpecialComments : 0}))
.pipe(gulp.dest('media/css'));
});
});
Related
I have gulpfile.js from https://github.com/gulpjs/gulp.
I just change folders name for mine.
Task 'gulp' work normally, first 'gulp watch' too. But any another save any less file don't do anything. Just "Finished 'styles' after 1.34 s"
(JS works good)
Any advice?
main.less have less imports with all filles
Folders looks like:
/www
/js
/css
main.less
file.less
file2.less
/lib
file3.less
var gulp = require('gulp');
var less = require('gulp-less');
var babel = require('gulp-babel');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var cleanCSS = require('gulp-clean-css');
var del = require('del');
var paths = {
styles: {
src: 'www/css/main.less',
dest: 'www/css/'
},
scripts: {
src: 'www/js/js/**/*.js',
dest: 'www/js/'
}
};
function clean() {
return del([ 'assets' ]);
}
function styles() {
return gulp.src(paths.styles.src)
.pipe(less())
.pipe(cleanCSS())
// pass in options to the stream
.pipe(rename({
basename: 'main',
suffix: '.min'
}))
.pipe(gulp.dest(paths.styles.dest));
}
function scripts() {
return gulp.src(paths.scripts.src, { sourcemaps: true})
.pipe(babel())
.pipe(uglify())
.pipe(concat('all.min.js'))
.pipe(gulp.dest(paths.scripts.dest));
}
function watch() {
gulp.watch("www/js/js**/*.js", scripts);
gulp.watch("www/css/**/*.less", styles);
}
exports.clean = clean;
exports.styles = styles;
exports.scripts = scripts;
exports.watch = watch;
var build = gulp.series(clean, gulp.parallel(styles, scripts));
gulp.task('build', build);
gulp.task('default', build);
As #Jakub thought, you never actually call your watch task in the code you presented! You probably want something like this:
var build = gulp.series(clean, gulp.parallel(styles, scripts), watch);
I have implementing gulp task in the project.
I have installed gulp-assests-version-replace to make version control file of css file.
Now I want to run above task after creating the app.css. It runs before creating the app.css, so version control file cannot be created.
Below is the gulp-file for running the gulp tasks.
var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var changed = require('gulp-changed');
var concat = require('gulp-concat');
var del = require('del');
var gulpif = require('gulp-if');
var gutil = require('gulp-util');
var imagemin = require('gulp-imagemin');
var jshint = require('gulp-jshint');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var assetsVersionReplace = require('gulp-assets-version-replace');
var uglify = require('gulp-uglify');
var path = {
assets: 'skin/frontend/custom_theme/default/'
};
var config = {
tasks: {
css: {
autoprefixerOptions: {
// For "browserslist" see "package.json"
cascade: false,
supports: false // See: https://github.com/filamentgroup/select-css/issues/17
},
dest: path.assets + 'css/build',
sassOptions: {
outputStyle: 'compressed'
},
src: path.assets + 'css/src/**/*.scss'
},
version: {
autoprefixerOptions: {
// For "browserslist" see "package.json"
cascade: false,
supports: false // See: https://github.com/filamentgroup/select-css/issues/17
},
dest: path.assets + 'css/build',
sassOptions: {
outputStyle: 'compressed'
},
src: path.assets + 'css/build/app.css'
}
}
}
gulp.task('default', ['clean'], function () {
gulp.start('css');
gulp.start('assetsVersionReplace');
});
gulp.task('clean', function () {
return del([
path.assets + 'css/build',
path.assets + 'js/build',
path.assets + 'js/min',
path.assets + 'img/build'
]);
});
gulp.task('css', function () {
return gulp.src(config.tasks.css.src)
.pipe(plumber({
errorHandler: reportError
}))
.pipe(sass(config.tasks.css.sassOptions))
.pipe(autoprefixer(config.tasks.css.autoprefixerOptions))
.pipe(gulp.dest(config.tasks.css.dest));
});
gulp.task('assetsVersionReplace', function () {
return gulp.src(config.tasks.version.src)
.pipe(assetsVersionReplace({
replaceTemplateList: [
'app/design/frotend/custom_theme/default/template/page/head.phtml'
]
}))
.pipe(gulp.dest(config.tasks.version.dest))
});
gulp.task('watch', function () {
// Run "default" immediately
gulp.start('default');
// CSS
gulp.watch(config.tasks.css.src, ['css']);
gulp.watch(config.tasks.version.src, ['assetsVersionReplace']);
});
How can I run assetsVersionReplace task after creating the css (or after runnung 'CSS' task)?
Either change
gulp.task('assetsVersionReplace', function () {
to
gulp.task('assetsVersionReplace', ['css'], function () {
or, if that isn't an option, add a helper task
gulp.task('assetsVersionReplaceAfterCss', ['css'], function () {
gulp.start('assetsVersionReplace');
}
and use that instead
Why don't you use gulp series.
gulp.task('default',
gulp.series(
'css',
'assetsVersionReplace'
)
);
When I launch gulp generates me the files, but when it enters the watch mode, understands that there was a change but does not overwrite files:
My gulp sample
var argv = require('yargs').argv;
var gulp = require('gulp');
var gulpif = require('gulp-if');
var uglify = require('gulp-uglify');
var dependencies = [
'core-js/client/*.min.js',
'zone.js/dist/zone.js',
'reflect-metadata/Reflect.js',
'systemjs/dist/system.src.js',
'rxjs/**/*.js',
'#angular/**/*.js',
'angular2-in-memory-web-api/*.js',
];
var modules = [
// Example: 'iconv-lite/**/*',
];
........
gulp.task("javascript", () => {
return gulp.src([
'./bower_components/jquery/dist/jquery.min.js',
'./bower_components/bootstrap/dist/js/bootstrap.min.js',
])
.pipe(gulp.dest("./build/js"))
});
gulp.task('styles', () => {
var postcss = require('gulp-postcss');
var autoreset = require('postcss-autoreset');
var simplevars = require('postcss-simple-vars');
var nested = require('postcss-nested');
var lost = require('lost');
var cssnext = require('postcss-cssnext');
var processors = [
simplevars(),
nested(),
lost(),
cssnext(),
];
return gulp.src([
'./src/css/*.css',
'./bower_components/openSans/openSans.css',
'./bower_components/components-font-awesome/css/font-awesome.min.css',
'./bower_components/bootstrap/dist/css/bootstrap.min.css',
])
.pipe(postcss(processors))
.pipe(gulp.dest('./build/css'));
});
gulp.task('ts', ['ts-app', 'ts-electron']);
gulp.task('build', ['templates', 'resources', 'styles', 'ts', 'libs', 'modules', 'images', 'javascript']);
gulp.task('watch', ['build'], () => {
gulp.watch('./src/app/**/*', ['ts-app']);
gulp.watch('./src/electron/**/*', ['ts-electron']);
gulp.watch('./src/resources/**/*', ['resources']);
gulp.watch('./src/css/**/*', ['styles']);
gulp.watch('./src/templates/**/*', ['templates']);
gulp.watch('./src/img/**/*', ['images']);
gulp.watch('./src/js/**/*', ['javascript']);
});
Platform: Win7x64
NPM: 3.10.8
NODE: v6.7.0
Gulp version:
{
"gulp": "^3.9.1",
"gulp-if": "^2.0.1",
"gulp-postcss": "^6.1.1",
"gulp-pug": "^3.0.3",
"gulp-typescript": "^2.13.6",
"gulp-uglify": "^1.5.4",
}
I also tried to manually put gulp.dest("./build/", {overwrite: true})(in all gulp.dest) but nothing change :/
What can I do to better understand the problem?
This is my gulpfile:
// including plugins
var gulp = require('gulp')
, rename = require('gulp-rename')
, uglify = require("gulp-uglify")
, cssmin = require("gulp-cssmin");
// task css
gulp.task('minify-css', function () {
gulp.src([
'./www/assets/css/bootstrap.min.css',
'./www/assets/css/admin.css',
'!./www/assets/css/build.min.css'
])
.pipe(cssmin())
.pipe(rename('build.min.css'))
.pipe(gulp.dest('./www/assets/css'));
});
// task js
gulp.task('minify-js', function () {
gulp.src([
'./www/assets/js/jquery.min.js',
'./www/assets/js/boostrap.min.js',
'./www/assets/js/vue.min.js',
'./www/assets/js/vue-resource.min.js',
'./www/assets/js/MiniModel.js',
'./www/assets/js/AdminUsers.js',
'!./www/assets/js/build.min.js'
])
.pipe(uglify())
.pipe(rename('build.min.js'))
.pipe(gulp.dest('./www/assets/js'));
});
gulp.task('default', ['minify-css', 'minify-js']);
After run ./node_modules/.bin/gulp the output is:
build.css only has admin.css
build.js only has AdminUsers.js
Why is missing a lot of source files on output?
More details:
Ubuntu: 14.10
Node version: v0.10.25
Gulp version: 3.9.1
UPDATE1: Im using in index.html like this:
<script type="text/javascript" src="/mini-vue/www/assets/js/build.min.js"></script>
<script type="text/javascript">
var usersmodel = new MiniModel(Vue, 'users', '/mini-vue/www/');
var adminusers = new AdminUsers(Vue, usersmodel);
adminusers.reload(1);
</script>
try using this
var minifyCSS = require('gulp-minify-css'),
minifyJS = require('gulp-minify'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
gulp.task('minify-js', function() {
return gulp.src('./www/assets/js/**/*.js')
.pipe(minifyJS())
.pipe(concat('build.min.js'))
.pipe(uglify({ mangle: false }))
.pipe(gulp.dest('./www/assets/js'));
});
gulp.task('minify-css', function() {
return gulp.src('./www/assets/css/**/*.css')
.pipe(minifyCSS())
.pipe(concat('build.min.css'))
.pipe(gulp.dest('./www/assets/css'));
});
gulp.task('default', ['minify-css', 'minify-js']);
I just want to work on a piece of HTML, which I want to later copy paste as it is and insert into an existing HTML document. I have something like this:
<div id="someDiv">
</div>
<script src="js/someScript.js" inline></script>
<style>
div#someDiv {
font-weight: bold;
}
</style>
I am using gulp-inline-source to get the script in the built file, I do some basic html minification.
Here is my gulpfile.js:
var gulp = require('gulp');
var del = require('del');
var jshint = require('gulp-jshint');
var inlinesource = require('gulp-inline-source');
var htmlmin = require('gulp-htmlmin');
var inlineCss = require('gulp-inline-css');
gulp.task('clean', function(cb) {
del(['assets/*'], cb)
});
gulp.task('lint', function() {
return gulp.src('./src/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('inlinesource-htmlmin', function() {
return gulp.src('./src/*.html')
.pipe(inlinesource())
.pipe(inlineCss())
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('./assets'));
});
gulp.task('default', ['clean', 'lint', 'inlinesource-htmlmin']);
It looks like gulp-inline-css is wrapping my content into html and body tags because it does not happen when I comment out the .pipe(inlineCss()) line.
Here is what I get:
<html><body><div id="someDiv" style="font-weight: bold"></div><script>$(document).ready(function(){console.log("logging")});</script></body></html>
Any idea how I could prevent that from happening?
I ended up using gulp-replace to remove the tags after inlineCss like this:
var gulp = require('gulp');
var del = require('del');
var jshint = require('gulp-jshint');
var inlinesource = require('gulp-inline-source');
var htmlmin = require('gulp-htmlmin');
var inlineCss = require('gulp-inline-css');
var replace = require('gulp-replace')
gulp.task('clean', function(cb) {
del(['assets/*'], cb)
});
gulp.task('lint', function() {
return gulp.src('./src/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('inlinesource-htmlmin', function() {
return gulp.src('./src/*.html')
.pipe(inlinesource())
.pipe(inlineCss())
.pipe(replace(/<(html|body)>/g, ''))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('./assets'));
});
gulp.task('default', ['clean', 'lint', 'inlinesource-htmlmin']);