Gulp less combine several files, let others separate - gulp

Say I have
main1.less
main2.less
main3.less
other1.less
other2.less
other3.less
I need the output
main.css
other1.css
other2.css
other3.css
I do
gulp.task('less', function () {
return gulp.src(['./Content/css/**/*.less'])
.pipe(less({
paths: [path.join(__dirname, 'less', 'includes')]
}))
.pipe(gulp.dest(paths.webroot + 'css'));
});
Actually it generates a css for a less file... Is there a way to combine several files in one, and let others in separate files?

You can accomplish this using gulp-concat to combine the styles you want into one file from a separate task.
const gulp = require('gulp4');
const less = require('gulp-less');
const concat = require('gulp-concat');
const path = require('path');
const config = {
paths: [ path.join(__dirname, 'less', 'includes') ]
};
gulp.task('less:separate', () => gulp
.src(path.join(__dirname, 'Content', 'css', '**', 'other*.less'))
.pipe(less(config))
.pipe(gulp.dest(paths.webroot + 'css')));
gulp.task('less:together', () => gulp
.src(path.join(__dirname, 'Content', 'css', '**', 'main*.less'))
.pipe(less(config))
.pipe(concat('main.css')) // combine them into one file
.pipe(gulp.dest(paths.webroot + 'css')));
gulp.task('less', gulp.parallel('less:separate', 'less:together'));

Related

SCSS to Style.css

Long story story, the main developer that worked on this is no longer with the company and I have to take over the project that they built out - So all help would be appreciated with this.
I'm not too familiar with WordPress nor SCSS/Foundation so this will be a learning process. I need to make a css change and deploy it, but it's not showing the changes on my local environment at all.
Here is the knowledge that I have:
- Foundation was used to build this
- SCSS is being converted to Style.css
- https://cdn.site.pl/wp-content/themes/sites/style.css?ver=4.9.10 - There is a version being applied to the end of style.css
Here are the tasks that are in my gulpfile.js:
Here is his documentation:
The default gulp task runs both gulp scripts and gulp styles. To run
this task, navigate in Terminal to the project's htdocs directory, and
type:
gulp
I get this in response:
[14:00:37] Task never defined: default
[14:00:37] To list available tasks, try running: gulp --tasks
Below, I will be providing the whole gulpfile.js file
const argv = require('yargs').argv;
const {
src,
dest,
parallel,
series,
watch
} = require('gulp');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const concat = require('gulp-concat');
const postcss = require('gulp-postcss');
const cssnano = require('cssnano');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
// Usage in gulp: gulp task-name --theme=theme-name
// Default value is 'f-sites'
const themeName = argv.theme ? argv.theme : 'f-sites';
// GULP CONFIG
const config = {
themeName: themeName,
themeDirectory: `wp-content/themes/${themeName}`
};
// GULP TASKS
// SCSS/CSS TASKS
//==================================================
function sassToCss() {
const nodeModulesSassPaths = [
'node_modules/foundation-sites/scss',
'node_modules/slick-carousel/slick',
'node_modules/jquery-fancybox/source/scss',
'node_modules/font-awesome/scss'
];
const srcPaths = [
`${config.themeDirectory}/scss/theme.scss`,
`${config.themeDirectory}/scss/theme-rtl.scss`
];
return src(srcPaths, { sourcemaps: true })
.pipe(sass({ includePaths: nodeModulesSassPaths }).on('error', sass.logError))
.pipe(autoprefixer({ browsers: ['last 2 versions', 'ie >= 10'] }))
.pipe(dest(`${config.themeDirectory}/css`, { sourcemaps: true }));
}
function cssConcatLTR() {
const srcFiles = [
`${config.themeDirectory}/css/wordpress.css`,
'./node_modules/animate.css/animate.css',
`${config.themeDirectory}/css/theme.css`,
];
return src(srcFiles, { sourcemaps: true })
.pipe(concat('project.css'))
.pipe(dest(`${config.themeDirectory}/css`, { sourcemaps: true }));
}
function cssConcatRTL() {
const srcFiles = [
config.themeDirectory + '/css/wordpress.css',
'./node_modules/animate.css/animate.css',
config.themeDirectory + '/css/theme-rtl.css'
];
return src(srcFiles, { sourcemaps: true })
.pipe(concat('project-rtl.css'))
.pipe(dest(`${config.themeDirectory}/css`, { sourcemaps: true }));
}
function cssMinifyLTR() {
const plugins = [cssnano()];
return src(`${config.themeDirectory}/css/project.css`)
.pipe(postcss(plugins))
.pipe(rename('style.css'))
.pipe(dest(config.themeDirectory));
}
function cssMinifyRTL() {
const plugins = [cssnano()];
return src(`${config.themeDirectory}/css/project-rtl.css`)
.pipe(postcss(plugins))
.pipe(rename('style-rtl.css'))
.pipe(dest(config.themeDirectory));
}
exports.default = parallel(
series(
sassToCss,
parallel(cssConcatLTR, cssConcatRTL),
parallel(cssMinifyLTR, cssMinifyRTL)
),
series(jsConcat, jsMinify)
);
exports.styles = series(
sassToCss,
parallel(cssConcatLTR, cssConcatRTL),
parallel(cssMinifyLTR, cssMinifyRTL)
);
exports.scripts = series(jsConcat, jsMinify);
This may be caused by the version of gulp-cli, you can try to upgrade it.
Relevant issues:
https://github.com/gulpjs/gulp-cli/issues/191
https://github.com/gulpjs/gulp/issues/1634

Local preprocessing with Gulp, Stylus, and Browser-sync

I'm trying to setup CSS preprocessing and browser-sync for a client on windows. Prepros seems to be junk, and he can't use codeKit because it's Mac only.
I gave him these resources: https://webdesign.tutsplus.com/tutorials/supercharge-your-local-wordpress-development--cms-28303 and https://browsersync.io/docs/gulp
The preprocessing works great, but the browser has to be manually refreshed to see the CSS changes.
Can you spot what is incorrect in this code? MAMP is also involved... so it could be something else. I'm trying out different configurations to trouble-shoot.
// gulpfile.js
var gulp = require('gulp');
var browserSync = require('browser-sync');
var stylus = require('gulp-stylus');
gulp.task('setup-server', function() {
var files = [
'./style.css',
'./*.php',
];
browserSync.init(files, {
proxy: 'https://website-name.dev',
});
});
gulp.task('compile-stylus', function () {
return gulp.src('stylus/*.styl')
.pipe( stylus({
// options
}))
.pipe( gulp.dest('./') ) // root of theme
.pipe( browserSync.stream() )
;
});
gulp.task('default', ['setup-server', 'compile-stylus'], function() {
gulp.watch('stylus/**/*.styl', ['compile-stylus']);
});
file structure
project-root
gulpfile.js
/stylus
/partials
style.styl
Try these changes:
var browserSync = require("browser-sync").create();
var reload = browserSync.reload;
and the following change at the end of the task:
gulp.task('compile-stylus', function () {
return gulp.src('stylus/*.styl')
.pipe( stylus({
// options
}))
.pipe( gulp.dest('./') ) // root of theme
.pipe( browserSync.reload({stream: true}));
});
And I assume you meant gulpfile.js not gruntfile.js in your folder structure.

node-hbsfy not compiling templates with Gulp

I'm simply trying to compile hbs templates via Browserify and Gulp, but the compilation process fails as soon as any HTML markup is encountered from my hbs file.
I've confirmed this by removing the HTML code within the hbs file, at which point Browserify runs as expected.
Here is a simplified version of my Gulp task:
const _gulp = require('gulp');
const _browserify = require('browserify');
const _remapify = require('remapify');
const _hbsfy = require('hbsfy');
const _vinylSourceStream = require('vinyl-source-stream');
const _vinylBuffer = require('vinyl-buffer');
_gulp.task('js:dev', () => {
return _browserify({entries: './src/js/app.js', debug: true})
.plugin(_remapify, [
{
src: '**/*.hbs', // glob for the files to remap
cwd: './src/markup/components',
expose: 'components' // this will expose './src/markup/components' as 'components'
}
])
.transform(_hbsfy)
.bundle()
.pipe(_vinylSourceStream('app.js'))
.pipe(_vinylBuffer())
.pipe(_gulp.dest('dist'))
});
The hbs template:
<div class="menu"> </div>
The main JS file:
(function (){
const _handlebars = require('hbsfy/runtime');
function init () {
_handlebars.registerPartial('menu', require('components/menu.hbs'));
}
document.addEventListener('DOMContentLoaded', init);
})();
What could be going wrong? It's as if the hbsfy transform isn't running properly...
It seems this issue is actually caused by the remapify plugin. Compilation works as expected using pathmodify instead.
Here is my updated Gulp file:
const _gulp = require('gulp');
const _browserify = require('browserify');
const _pathmodify = require('pathmodify');
const _hbsfy = require('hbsfy');
const _vinylSourceStream = require('vinyl-source-stream');
const _vinylBuffer = require('vinyl-buffer');
_gulp.task('js:dev', () => {
return _browserify({entries: './src/js/app.js', debug: true})
.plugin(_pathmodify, {
mods: [
_pathmodify.mod.dir('components', process.cwd() + '/src/markup/partials/components')
]
})
.transform(_hbsfy)
.bundle()
.pipe(_vinylSourceStream('app.js'))
.pipe(_vinylBuffer())
.pipe(_gulp.dest('dist'))
});

Gulp Browserify with glob and uglify/factor-bundle

I'm currently getting into browserify. I like it so far but before I start using it I want to automate it. Gulp is the build system of my choice.
So what I actually want to do is:
Get js/app/**.js, bundle it to js/bundle/ and extract common dependencies into js/bundle/common.js. In addition uglify everything and add source maps.
Well. The gulp support for browserify kinda seems poor, at least my google researches were pretty disappointing.
Anyway. What I've got so far.
var gulp = require('gulp'),
browserify = require('browserify'),
factor = require('factor-bundle');
// ...
// gulp task
return browserify({
entries: ['js/app/page1.js', 'js/app/page2.js'],
debug: true
})
.plugin(factor, {
o: ['js/bundle/page1.js', 'js/bundle/page2.js']
})
.bundle()
.pipe(source('common.js'))
.pipe(gulp.dest('js/bundle/'));
Well this is neither uglifying nor adding sourcemaps and much less using a glob pattern. I can find an official recipe which shows me how to use the pipe to add additional transformations like uglify. But it's only for a single file.
as an outputs parameter to factor-bundle, use streams instead of file paths. You can do whatever you want with the streams then.
var indexStream = source("index.js");
var testStream = source("tests.js");
var commonStream = bundler.plugin('factor-bundle', { outputs: [indexStream, testStream] })
.bundle()
.pipe(source('common.js'));
return merge(indexStream, commonStream, testStream)
.pipe(buffer())
.pipe(sourcemaps.init({ debug: true, loadMaps: true }))
.pipe(uglify())
.pipe(gulp.dest('js/bundle/'))
Thanks to Liero's answer, I got something very similar working. Here's the complete gulpfile:
const gulp = require('gulp');
const browserify = require('browserify');
const factor = require('factor-bundle');
const source = require('vinyl-source-stream');
const sourcemaps = require('gulp-sourcemaps');
const buffer = require('gulp-buffer');
const merge = require('gulp-merge');
gulp.task('bfb', function () {
const fejs = 'public/javascripts/' // location of source JS
const fejsb = fejs + 'b/'; // location of bundles
const modules = [ // aka entry points
'accounts',
'invoice',
'invoices',
// etc...
];
const inputs = [];
const streams = [];
modules.forEach(function (module) {
inputs.push(fejs + module + '.js');
streams.push(source(module + '.js'));
});
const bundler = browserify(inputs, {});
const commonStream = bundler.plugin(factor, { outputs: streams })
.bundle()
.pipe(source('common.js'));
streams.push(commonStream);
return merge(streams)
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
//.pipe(uglify()) // haven't tested this bit
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(fejsb));
});

How can I input and output multiple files with gulp and browserify

I'm sure there's a way to do this, but I couldn't find it. What I want is to pass multiple files into browserify and output multiple files - this is useful for a case where a site has multiple SPA's, with each having its own requires.
Say I have app1.js, app2.js, etc, with each loading in different pages and having independent require('..') statements. I'm looking for a task that does something like this:
gulp.task('browserify', function() {
return
gulp.src('src/**/*.js')
.pipe(browserify) //
.pipe(gulp.dest('dist'));
});
Any idea what's a simple way to accomplish this? thanks.
I stumbled upon this problem actually earlier this week. The problem of "creating multiple bundles". This should work:
var gulp = require('gulp'),
source = require('vinyl-source-stream'),
browserify = require('browserify'),
es = require('event-stream');
gulp.task('default', function() {
// Your main files
var files = [
'./app/main-a.js',
'./app/main-b.js'
];
// Create a stream array
var tasks = files.map(function(entry) {
return browserify({ entries: [entry] })
.bundle()
.pipe(source(entry))
.pipe(gulp.dest('./dist'));
});
return es.merge.apply(null, tasks);
});
Please do not use the gulp-browserify plugin, as it's blacklisted by now. Use browserify itself instead.
Same with Globs:
gulp.task('withglob', function() {
return glob('./app/main-**.js', function(err, files) {
var tasks = files.map(function(entry) {
return browserify({ entries: [entry] })
.bundle()
.pipe(source(entry))
.pipe(rename({
extname: '.bundle.js'
}))
.pipe(gulp.dest('./dist'));
});
return es.merge.apply(null, tasks);
})
});
Btw.: That's the reason