I get a minified bundle file, i want to get a non-minified bundle with it in the same dist.
gulpfile.js
'use strict';
var gulp = require('gulp'),
gulpLoad = require('gulp-load-plugins'),
browserify = require('browserify'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
del = require('del'),
pkg = require('./package.json'),
$ = gulpLoad(),
DIST = './dist',
SRC = './src/index.js';
gulp.task('clean', function(fn) {
return del(DIST, fn);
})
gulp.task('lint', function() {
return gulp.src(SRC)
.pipe($.jshint())
.pipe($.jshint.reporter('default'))
});
gulp.task('bundle', ['lint', 'clean'], function() {
var b = browserify();
return b.bundle()
.pipe(source('./ar-string.min.js'))
.pipe(buffer())
.pipe($.sourcemaps.init({loadMaps: true}))
.pipe($.uglify())
.on('error', $.util.log)
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(DIST));
});
gulp.task('default', function() {
gulp.watch([SRC, './gulpfile.js'], ['bundle']);
});
EDIT: Actually, I don't think this can be done in one pipe, we could have two, one which doesn't minify, and one which minifies and source maps:
b.bundle()
.pipe(source('./ar-string.js'))
.pipe(gulp.dest(DIST));
return b.bundle()
.pipe(source('./ar-string.min.js'))
.pipe(buffer())
.pipe($.sourcemaps.init({loadMaps: true}))
.pipe($.uglify())
.on('error', $.util.log)
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(DIST));
Related
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', done => {
gulp.src('./css/**/*.sass')
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'expanded'}).on('error', sass.logError))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('.'));
done()
});
gulp.task('sass:watch', done => {
gulp.watch('./css/**/*.sass', gulp.series('sass'));
done()
});
gulp.task('compressed-js', done => {
gulp.src('js/src/*.js')
.pipe(uglify())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('js'));
done()
});
gulp.task('compressed-js:watch', done => {
gulp.watch('./js/src/*.js', gulp.series('compressed-js'));
done()
});
In the phpstorm getting this error: "ReferenceError: sourcemaps is not defined"
The error is pretty self-explanatory: you're referring to the sourcemaps variable while it doesn't exist.
Normally, you would need to declare it at the top of your gulpfile, like so:
var sourcemaps = require('gulp-sourcemaps');
However, Gulp 4 has sourcemap functionality built-in, so you can rewrite your sass task like this:
gulp.task('sass', () =>
gulp.src('./css/**/*.sass', { sourcemaps: true })
.pipe(sass({ outputStyle: 'expanded' }).on('error', sass.logError))
.pipe(gulp.dest('.', { sourcemaps: '.' }))
);
Note that I removed the done callback, which isn't needed if you just return the stream.
Note that you will run into the same problem when running the compressed-js task. You'll need to define uglify and rename:
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
And make sure the packages you require are installed.
Can any one help me with a gulp file?
I want to start my express server when i run gulp dev. The express server starts in my app.js file. I tried to modify the app.js file but that causes an error.
How do i implement that ?
var gulp = require('gulp')
var sass = require('gulp-sass')
var header = require('gulp-header')
var cleanCSS = require('gulp-clean-css')
var rename = require('gulp-rename')
var uglify = require('gulp-uglify')
var pkg = require('./package.json')
var browserSync = require('browser-sync').create()
var server = require('gulp-develop-server')
var express = require('express')
var app = express()
// Copy third party libraries from /node_modules into /vendor
gulp.task('vendor', function () {
// Bootstrap
gulp.src([
'./node_modules/bootstrap/dist/**/*',
'!./node_modules/bootstrap/dist/css/bootstrap-grid*',
'!./node_modules/bootstrap/dist/css/bootstrap-reboot*'
])
.pipe(gulp.dest('./vendor/bootstrap'))
// Font Awesome
gulp.src([
'./node_modules/font-awesome/**/*',
'!./node_modules/font-awesome/{less,less/*}',
'!./node_modules/font-awesome/{scss,scss/*}',
'!./node_modules/font-awesome/.*',
'!./node_modules/font-awesome/*.{txt,json,md}'
])
.pipe(gulp.dest('./vendor/font-awesome'))
// jQuery
gulp.src([
'./node_modules/jquery/dist/*',
'!./node_modules/jquery/dist/core.js'
])
.pipe(gulp.dest('./vendor/jquery'))
// jQuery Easing
gulp.src([
'./node_modules/jquery.easing/*.js'
])
.pipe(gulp.dest('./vendor/jquery-easing'))
// Simple Line Icons
gulp.src([
'./node_modules/simple-line-icons/fonts/**'
])
.pipe(gulp.dest('./vendor/simple-line-icons/fonts'))
gulp.src([
'./node_modules/simple-line-icons/css/**'
])
.pipe(gulp.dest('./vendor/simple-line-icons/css'))
})
// Compile SCSS
gulp.task('css:compile', function () {
return gulp.src('./scss/**/*.scss')
.pipe(sass.sync({
outputStyle: 'expanded'
}).on('error', sass.logError))
.pipe(gulp.dest('./css'))
})
// Minify CSS
gulp.task('css:minify', ['css:compile'], function () {
return gulp.src([
'./css/*.css',
'!./css/*.min.css'
])
.pipe(cleanCSS())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./css'))
.pipe(browserSync.stream())
})
// CSS
gulp.task('css', ['css:compile', 'css:minify'])
// Minify JavaScript
gulp.task('js:minify', function () {
return gulp.src([
'./js/*.js',
'!./js/*.min.js'
])
.pipe(uglify())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./js'))
.pipe(browserSync.stream())
})
// JS
gulp.task('js', ['js:minify'])
// Default task
gulp.task('default', ['css', 'js', 'vendor'])
gulp.task('express', function () {
app.use(express.static(__dirname))
app.listen(6006, '0.0.0.0') // <- CHANGE PORT NUMBER TO PREFERRED
CHOICE
})
// Configure the browserSync task
gulp.task('browserSync', function () {
browserSync.init({
server: {
baseDir: './'
}
})
})
// Dev task
gulp.task('dev', ['express', 'css', 'js', 'browserSync'], function () {
gulp.watch('./scss/*.scss', ['css'])
gulp.watch('./js/*.js', ['js'])
gulp.watch('./*.html', browserSync.reload)
})
I get this error:
Uncaught ReferenceError: require is not defined
I know that in the browser you can not use require API, just trying to make it possible with browserify or requirejs or whatelse but it doesn't work in any way.
This is my gulpfile
var gulp = require('gulp'),
plumber = require('gulp-plumber'),
rename = require('gulp-rename');
var autoprefixer = require('gulp-autoprefixer');
var babel = require('gulp-babel');
var browserify = require('gulp-browserify');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var imagemin = require('gulp-imagemin'),
cache = require('gulp-cache');
var minifycss = require('gulp-minify-css');
var sass = require('gulp-sass');
var browserSync = require('browser-sync');
gulp.task('browser-sync', function() {
browserSync({
server: {
baseDir: "./"
}
});
});
gulp.task('bs-reload', function () {
browserSync.reload();
});
gulp.task('images', function(){
gulp.src('src/images/**/*')
.pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(gulp.dest('dist/images/'));
});
gulp.task('styles', function(){
gulp.src(['src/styles/**/*.scss'])
.pipe(plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}}))
.pipe(sass())
.pipe(autoprefixer('last 2 versions'))
.pipe(gulp.dest('dist/styles/'))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest('dist/styles/'))
.pipe(browserSync.reload({stream:true}))
});
gulp.task('scripts', function(){
return gulp.src('src/scripts/**/*.js')
.pipe(plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}}))
.pipe(concat('main.js'))
.pipe(babel())
.pipe(browserify())
.pipe(gulp.dest('dist/scripts/'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('dist/scripts/'))
.pipe(browserSync.reload({stream:true}))
});
gulp.task('default', ['browser-sync'], function(){
gulp.watch("src/styles/**/*.scss", ['styles']);
gulp.watch("src/scripts/**/*.js", ['scripts']);
gulp.watch("*.html", ['bs-reload']);
});
and this is my main.js, just an import of a plugin anime.js
import anime from 'animejs'
Also tried with requireJs but it seems a bit hard to integrate it in my gulpfile, also webpack seems to be hard code for me.
Gulp setup is working fine for me in local.
But i want like if i change files in local then it will build and uploaded to live server automatically (minified css and js/compiled css/js).
I have tried gulp-rsync, vinyl-ftp but couldn't succeed may be i don't know how to use above packages.
Here is a my gulp file.
var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync');
var useref = require('gulp-useref');
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');
var cssnano = require('gulp-cssnano');
var imagemin = require('gulp-imagemin');
var cache = require('gulp-cache');
var del = require('del');
var runSequence = require('run-sequence');
// Development Tasks
// -----------------
// Start browserSync server
gulp.task('browserSync', function() {
browserSync({
server: {
baseDir: 'app'
}
})
})
gulp.task('sass', function() {
return gulp.src('app/scss/**/*.scss') // Gets all files ending with .scss in app/scss and children dirs
.pipe(sass().on('error', sass.logError)) // Passes it through a gulp-sass, log errors to console
.pipe(gulp.dest('app/css')) // Outputs it in the css folder
.pipe(browserSync.reload({ // Reloading with Browser Sync
stream: true
}));
})
// Watchers
gulp.task('watch', function() {
gulp.watch('app/scss/**/*.scss', ['sass']);
gulp.watch('app/*.html', browserSync.reload);
gulp.watch('app/js/**/*.js', browserSync.reload);
})
// Optimization Tasks
// ------------------
// Optimizing CSS and JavaScript
gulp.task('useref', function() {
return gulp.src('app/*.html')
.pipe(useref())
.pipe(gulpIf('*.js', uglify()))
.pipe(gulpIf('*.css', cssnano()))
.pipe(gulp.dest('dist'));
});
// Optimizing Images
gulp.task('images', function() {
return gulp.src('app/images/**/*.+(png|jpg|jpeg|gif|svg)')
// Caching images that ran through imagemin
.pipe(cache(imagemin({
interlaced: true,
})))
.pipe(gulp.dest('dist/images'))
});
// Copying fonts
gulp.task('fonts', function() {
return gulp.src('app/fonts/**/*')
.pipe(gulp.dest('dist/fonts'))
})
// Cleaning
gulp.task('clean', function() {
return del.sync('dist').then(function(cb) {
return cache.clearAll(cb);
});
})
gulp.task('clean:dist', function() {
return del.sync(['dist/**/*', '!dist/images', '!dist/images/**/*']);
});
// Build Sequences
// ---------------
gulp.task('default', function(callback) {
runSequence(['sass', 'browserSync'], 'watch',
callback
)
})
gulp.task('build', function(callback) {
runSequence(
'clean:dist',
'sass',
['useref', 'images', 'fonts'],
callback
)
})
And here is a my folder structure.
This is main folder
It's a development folder
And this one is production folder in which all files are build.
When i run a server in the root of the application and browse to /dist, my app works.
When i run the webserver in the Dist folder, I only see the index.html, but react won't render the page.
my gulp file is this:
var gulp = require('gulp');
var plug = require('gulp-load-plugins')({lazy: true});
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var del = require('del');
var sass = require('gulp-sass');
gulp.task('build', ['sass'], function () {
return browserify({
extensions: ['.jsx', '.js'],
entries: './src/app.jsx',
})
.transform(babelify.configure({ignore: /(node_modules)/}))
.bundle()
.on("error", function (err) {
console.log("Error : " + err.message);
})
.pipe(source('app.js'))
.pipe(buffer())
.pipe(plug.uglify())
.pipe(gulp.dest('./dist/'));
});
gulp.task('clean', function () {
return del('dist/**', {force: true});
});
gulp.task('copy-files', ['clean'], function () {
return gulp.src(['./src/index.html', './favicon.ico','./src/contact.html'])
.pipe(gulp.dest('./dist/'));
});
gulp.task('copy-images', ['copy-files'], function () {
return gulp.src(['./src/img/**/*'])
.pipe(gulp.dest('./dist/img'));
});
gulp.task('sass', ['copy-images'], function () {
return gulp.src('./src/css/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./dist/css'));
});
Am I doing something wrong in my gulp file, or is there an other cause?