why doesn't the compiled css file appear? - javascript

i cant understand why the compiled css file doesn't appear, i checked some videos,but cannot find the mistake,please help,my code is below,also i have pics
let gulp = require ('gulp');
let sass = require ('gulp-sass');
gulp.task('sass', function(){
return gulp.src('app/scss/style.scss')
.pipe(sass())
.pipe(gulp.dest('app/css'))
});
here is the pictures
[1]: https://i.stack.imgur.com/sCytp.png
[2]: https://i.stack.imgur.com/SHUIA.png
[3]: https://i.stack.imgur.com/RP1lx.png
[4]: https://i.stack.imgur.com/fGK0h.png

As the error shows in picture 2 you need to set a default sass compiler. Check the documentation for more examples
Install sass. npm install sass
Set default compiler let sass = require('gulp-sass')(require('sass'));
Result:
```
let gulp = require ('gulp');
let sass = require ('gulp-sass')(require('sass'));
gulp.task('sass', function(){
return gulp.src('app/scss/style.scss')
.pipe(sass())
.pipe(gulp.dest('app/css'))
});

Related

Gulp Watch doesn't detect changing in SASS SCSS folder

I have set up Gulp for the first time to compile SCSS to CSS using NanoCSS and gulp-css.
I have created a do-sass that successfully compiles SCSS and minifies the CSS.
However, when I place this in a watch task, it does not work.
If I change any of my SCSS files the do-sass command does not run. Can anyone tell me what I have done wrong?
const gulp = require('gulp');
const sass = require('gulp-sass');
const cssnano = require('gulp-cssnano');
gulp.task('sass', function(){
return gulp.src('scss/styles.scss')
.pipe(sass())
.pipe(gulp.dest('./'))
});
// Minifys .css and reload browser.
gulp.task('mini-css', function() {
return gulp.src('styles.css')
.pipe(cssnano())
.pipe(gulp.dest('./'));
});
// Compile and minify css.
gulp.task('do-sass', gulp.series('sass', 'mini-css'));
gulp.task('watch', function(){
gulp.watch('scss/*/.scss', gulp.series('do-sass'));
});
The glob pattern used by your watch task is probably incorrect: scss/*/.scss will not look for the SCSS files you want, because it will try to traverse one more directory deeper and look for files that are named .scss (without any file name, just the extension).
What you want is to use the double-star syntax, if you want to glob any SCSS files that are nested at any level:
scss/**/*.scss
Therefore, if you update your watch task as such, it should work:
gulp.task('watch', function(){
gulp.watch('scss/**/*.scss', gulp.series('do-sass'));
});

Fail compile Sass with gulp

I have a problem with my code. it will not compile when I write "Gulp sass" in the console, But nothing happened in my CSS folder. I have been sitting for a long time trying to make it work but it won't want to work for me and I am very grateful if anyone can help me with this
Here is the code:
var gulp = require('gulp');
var sass = require('gulp-sass');
// Compile
gulp.task('sass', function() {
return gulp.src('./src/Assests/scss/*.scss')
.pipe(sass())
.pipe(gulp.dest('./src/Assets/css'));
});`
and in the console it says
gulp sass
[10:41:41] Using gulpfile ~\my-app\gulpfile.js
[10:41:41] Starting 'sass'...
[10:41:41] Finished 'sass' after 8.78 ms`
Change your code to log errors sass().on('error', sass.logError)
You task:
gulp.task('sass', function() {
return gulp.src('./src/Assests/scss/*.scss')
.pipe(sass()
.on('error', sass.logError)
)
.pipe(gulp.dest('./src/Assets/css'));
});
please make sure the name of the folder is correct. Looks like you have 'Assests' instead of 'Assets'. Note the extra 's'

Add bootstrap-sass in fountainjs error

using fountainjs to generate a new angular project, I have issue in adding bootstrap-sass library to my project.
here is the my generate-project flow: Fountain->angular1->bower->babel->sass
then, since I need to use bootstrap, I installed it using bower, and included it in my index.scss with #import "../bower_components/bootstrap-sass/assets/stylesheets/bootstrap";
It works fine when I run gulp serve but when I build it using gulp build, the following error occured:
Error in plugin 'gulp-cssnano' Message: "bower-components/bootstrap-sass/assets/stylesheets/_bootstrap.scss" is not int the SourceMap
So, what goes wrong here and how to fix that?
I'm not certain of the fix, but a workaround that might help you move along is to comment out or remove the sourcemaps usage in your gulp.conf.js. This will allow your sass to compile, but it won't give you sourcemaps for your style code.
Below is the standard gulp.conf.js with the sourcemaps lines commented out.
const gulp = require('gulp');
const browserSync = require('browser-sync');
//const sourcemaps = require('gulp-sourcemaps');
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const conf = require('../conf/gulp.conf');
gulp.task('styles', styles);
function styles() {
return gulp.src(conf.path.src('index.scss'))
//.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'expanded'})).on('error', conf.errorHandler('Sass'))
.pipe(postcss([autoprefixer()])).on('error', conf.errorHandler('Autoprefixer'))
//.pipe(sourcemaps.write())
.pipe(gulp.dest(conf.path.tmp()))
.pipe(browserSync.stream());
}
On line 17 of /gulp_tasks/styles.js, replace this line:
.pipe(sourcemaps.write())
with:
.pipe(sourcemaps.write(undefined, { sourceRoot: null }))
The issue is caused by a bug in gulp-sourcemaps when including files from the node_modules or bower_components folder.

Laravel minify css and js files

I receive custom js and css script as a string through html form. This strings are then saved in files on filesystem. The question is how to minify those files every time they are generated/updated. I am using gulp and laravel elixir.
My first idea was to call exec("gulp something") but not sure how to configure gulp.
Requirments:
gulp-newer
minifyCss
gulp-uglify
First step:(install plugins)
minifyCss and gulp-uglify are used by laravel-elixir. You have to install gulp-newer by npm install gulp-newer --save-dev.
Second step:(define task)
You need to define different tasks for css and js.
CSS task
gulp.task('cssTask', function() {
return gulp.src(cssSrc)
.pipe(newer(cssDest))//compares the css source and css destination(css files)
.pipe(minifyCss())//minify css
.pipe(gulp.dest(cssDest));//save minified css in destination directory
});
Js task
gulp.task('jsTask', function() {
return gulp.src(jsSrc)
.pipe(newer(jsDest))//compares the js source and js destination(js files)
.pipe(uglify())//minify js
.pipe(gulp.dest(jsDest));//save minified js in destination directory
});
Third step:(define custom watch)
You should have a watch task which watches your source directories(cssSrc and jsSrc) and calls its related task.
gulp.task('custom', function() {//Watch task
gulp.watch(cssSrc, ['cssTask']);//watch your css source and call css task
gulp.watch(jsSrc, ['jsTask']);//watch your js source and call js task
});
Fourth step:(run custom watch)
Finally, you must run the custom task by gulp custom.
Conclusion:
Every time, when file is added to the source directory. Gulp will minify the file and store it in destination directory. This is tested locally and it works perfectly.
Completed Gulp file
var gulp = require('gulp');
var elixir = require('laravel-elixir');
var newer = require('gulp-newer');
var minifyCss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
cssSrc = 'cssSrc/*.css';//Your css source directory
cssDest = 'cssDest';//Your css destination directory
jsSrc = 'jsSrc/*.js';//Your js source directory
jsDest = 'jsDest';//Your js destination directory
gulp.task('cssTask', function() {
return gulp.src(cssSrc)
.pipe(newer(cssDest))//compares the css source and css destination(css files)
.pipe(minifyCss())//minify css
.pipe(gulp.dest(cssDest));//save minified css in destination directory
});
gulp.task('jsTask', function() {
return gulp.src(jsSrc)
.pipe(newer(jsDest))//compares the js source and js destination(js files)
.pipe(uglify())//minify js
.pipe(gulp.dest(jsDest));//save minified js in destination directory
});
gulp.task('custom', function() {//Watch task
gulp.watch(cssSrc, ['cssTask']);//watch your css source and call css task
gulp.watch(jsSrc, ['jsTask']);//watch your js source and call js task
});
Edited after comment:
I would like something like this: gulp custom srcFile destFile.
For that situation, you need to install new plugin which is called yargs.
Installation:
You can install yargs by npm install yargs --save-dev and then you have to pass source directory and destination directory when custom task is called.
gulp custom --cssSrc=cssSource --cssDest=cssDestination --jsSrc=jsSource --jsDest=jsDestination
For example:
gulp custom --cssSrc=cssSrc/*.css --cssDest=cssDest --jsSrc=jsSrc/*.js --jsDest=jsDest
The completed Gulp file:
var gulp = require('gulp');
var elixir = require('laravel-elixir');
var newer = require('gulp-newer');
var minifyCss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var argv = require('yargs').argv;
cssSrc = argv.cssSrc;
cssDest = argv.cssDest;
jsSrc = argv.jsSrc;
jsDest = argv.jsDest;
gulp.task('cssTask', function() {
return gulp.src(cssSrc)
.pipe(newer(cssDest))
.pipe(minifyCss())
.pipe(gulp.dest(cssDest));
});
gulp.task('jsTask', function() {
return gulp.src(jsSrc)
.pipe(newer(jsDest))
.pipe(uglify())
.pipe(gulp.dest(jsDest));
});
gulp.task('custom', function() {
gulp.watch(cssSrc, ['cssTask']);
gulp.watch(jsSrc, ['jsTask']);
});
Note: Every time, the source directory or destination directory is changed, gulp task must be called again.
Use the below gulp modules.
https://www.npmjs.com/package/gulp-minify-css - Minify all your css files.
https://www.npmjs.com/package/gulp-concat-css - Merge all your css in to one file.
https://www.npmjs.com/package/gulp-uglify - Minigy all your JS files.
https://www.npmjs.com/package/gulp-sass - SASS
Refer this answer:
Gulp minify multiple js files to one
Below is sample Gulp File Snippet.
var gulp = require('gulp'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
clean = require('gulp-clean'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
livereload = require('gulp-livereload'),
lr = require('tiny-lr'),
server = lr();
// Clean
gulp.task('clean', function() {
return gulp.src(['css', 'js', 'img'], {
read: false
})
.pipe(clean());
});
gulp.task('styles', function() {
return gulp.src('sass/styles.scss')
.pipe(sass({
style: 'expanded'
}))
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(gulp.dest('css'))
.pipe(notify({
message: 'Styles task complete'
}));
});
// Concatenate & Minify JS
gulp.task('scripts', function() {
return gulp.src('js/*.js')
.pipe(concat('all.js'))
.pipe(gulp.dest('dist'))
.pipe(rename('all.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist'));
});
// Watch
gulp.task('watch', function() {
// Watch .scss files
gulp.watch('sass/**/*.scss', ['styles']);
// Watch .js files
gulp.watch('js/**/*.js', ['scripts']);
// Watch image files
gulp.watch('img/**/*', ['images']);
// Create LiveReload server
var server = livereload();
// Watch any files in dist/, reload on change
gulp.watch(['sass/**', 'js/**', 'img/**', '*.html']).on('change', function(file) {
server.changed(file.path);
});
});
gulp.task('default', ['styles', 'scripts', 'watch']);
At first place, you need to store the content in a new file. A good place for this is resources/assets/js:
$jsContent = $request->get('js_custom_content');
file_put_contents(base_path('resources/assets/js/custom.js'), $jsContent);
Ok, once created the javascript file, you need to tell to elixir to minify the custom script:
// it will look for resources/assets/js/custom.js
// and it will compress the output to public/assets/js/custom.min.js
mix.scripts(['custom.js'], 'public/assets/js/custom.min.js');
Optionally, you can version it. That means that everytime the script's content change, gulp will generate a new version in order to avoid browser's cache issues:
mix.version(['public/assets/js/custom.min.js']);
Finally, load the script in your view:
<script type="text/javascript" src="{{ url('assets/js/custom.min.js')) }}"></script>
Or with version:
<script type="text/javascript" src="{{ url(elixir('assets/js/custom.min.js')) }}"></script>

Gulp JS doesn't output CSS file

I'm a Gulp JS beginner. Currently I'm trying to compile some simple less code to CSS. My Gulp task should output a new CSS file for this but after I run the task, nothing happens. I've included the gulp-util module to try to debug the task execution but it doesn't show me any errors.
Here's my Gulp file:
// gulpfile.js
// --- INIT
var gulp = require('gulp'),
util = require('gulp-util'),
less = require('gulp-less'), // compiles less to CSS
minify = require('gulp-minify-css'), // minifies CSS
concat = require('gulp-concat'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'); // minifies JS
// Paths variables
var paths = {
'dev': {
'less': './laravel/public/assets/less/'
},
'assets': {
'css': './laravel/public/assets/css/ ',
'js': './laravel/public/assets/js/',
'vendor': './laravel/public/assets/bower_vendor/'
}
};
// --- TASKS
// Auth CSS
gulp.task('auth.css', function(){
return gulp.src(paths.dev.less + 'auth.less')
.pipe(less().on('error', util.log))
.pipe(gulp.dest(paths.assets.css))
.pipe(minify({keepSpecialComments:0}))
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(paths.assets.css));
});
// --- WATCH
// --- DEFAULT
gulp.task('default', ['auth.css']);
I'm using Laravel 4. This file lives in the root directory. I have changed the paths but that doesn't seem to work either. Any ideas?
Try removing the extra space at the end of your path.assets.css.
Just fixed. It seems, the path had to be something like: "./public/assets/css/". Now the file has been generated.

Categories