Gulp Sourcemaps - gulp

Trying to implement sass sourcemaps but for some reason it doesn't seem to be playing nice for sub folders. For example, below is the main.scss file that gets compiled:
#import 'test';
#import 'test1';
#import 'test2';
#import 'test3';
#import 'sub/test4';
The changes show up (using BrowserSync) and compile with no issues. However inspecting the code in dev tools. It does not seem to reference the last import, it references 'test3' instead. See screenshot Dev Tools Screen Shot
The sass file 'sub/test4' contains the salmon color for the body but the source map is saying that this is contained in the file 'test3'.
See below for the styles task that I am using:
gulp.task('styles', function(){
return gulp.src('assets/scss/**/*.scss')
.pipe(plumber({
errorHandler: function(err){
this.emit('end');
}
}))
.pipe(scssLint({ customReport: scssLintStylish }))
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'compressed', errLogToConsole: true}))
.on('error', notify.onError({ message: 'SASS Compile Fail'}))
.pipe(autoprefixer({
browsers: ['last 2 versions']
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('assets/css'))
.pipe(uglifycss())
.pipe(browserSync.stream({match: '**/*.css'}));
});
Tried various different things but nothing seems to work at all. Any help greatly appreciated!

Related

Gulp src does not find pattern

I'm tryng to create a new gulp task to run into my application and look for all '.fragment.sass' files.
I wrote:
gulp.task('sassFragments', () => {
return gulp
.src('./src/**/*.fragment.sass')
.pipe(debug())
.pipe(sassGlob())
.pipe(sass({ outputStyle: 'expanded' })).on('error', sass.logError)
.pipe(concat('fragments_style.css'))
.pipe(gulp.dest('./build/assets/css'))
.pipe(browserSync.reload({ stream: true }));
})
but no fragments_style.css is created in /build/assets/css folder.
I have another task which does similar using src('./src/**/*.sass') to generate a style.css file and works great!
I think there is a issue with .src method, that is not matching this '.fragment.sass' pattern.
Can anyone help me?
Gulp version: 3.9.1

Sourcemaps are in wrong location or have incorrect paths

I've been trying to get gulp sass and gulp sourcemaps to do exactly what I want and I'm finding it hard. I want to take a sass entry file (src/sass/index.scss), generate an output file (dist/css/index.css) and a separate sourcemap for that index file (dist/css/index.css.map) which has a sourceRoot set to the project base (absolute path: /home/damon/projects/test) and the sourcemap entries to be relative to that path.
Here's what I tried:
attempt 1: straight example code from gulp-sass:
var sassEntry = 'src/sass/index.scss';
gulp.src(sassEntry)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest('dist/css'));
Outcome: this inlines the sourcemap into the CSS file so I can't tell if it's right or not.
attempt 2: write it to separate file
gulp.src(sassEntry)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist/css'));
Outcome: writes a separate sourcemap, but the sourceRoot says '/sources/' (WTF is that?!, it doesn't exist and I never configured it)
and the paths are all relative to the sass entry file, not the project base, which is also going to be meaningless when my browser tries to locate the source files.
attempt 3: try to fix the sourceroot (also I found includeContent: false)
gulp.src(sassEntry)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('.',{includeContent: false, sourceRoot: __dirname}))
.pipe(gulp.dest('dist/css'));
Outcome: the sourceroot is now my working folder which is nice, the content isn't included which is nice, but the files in the sourcemap are still relative to the sass entry file not to the sourceRoot, so my map is still useless
attempt 4: Set the gulp.src base
gulp.src(sassEntry, { base: __dirname })
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('.',{includeContent: false, sourceRoot: __dirname}))
.pipe(gulp.dest('dist/css'));
Outcome: Infuriatingly, setting the base on gulp.src fixes the sourcemap - sourceRoot is still correct and the source file paths are relative to the sourceRoot, BUT it now outputs to dist/css/src/sass/index.css which is wrong. WTF!
attempt 5: use absolute paths
gulp.src(sassEntry, { base: __dirname })
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('.',{includeContent: false, sourceRoot: __dirname}))
.pipe(gulp.dest(__dirname + '/dist/css'));
Outcome: no change, it still outputs to the same deep structure in dist.
If anyone can enlighten me on how to do this I would be forever grateful.
While Sven's answer is perfectly good, I also found an answer to my own question by getting a deeper understanding of how gulp works (which I was trying to avoid), and apparently gulp stores each matched file with a path, so adding:
{ base: __dirname }
in the gulp.src makes it that each matched file has the full path from the base, which then causes them to output with the full relative path from wherever you set the base to. The solution I ended up with was to use gulp-flatten, which removes those relative paths from files in the pipeline, so my eventual function looked like this:
gulp.src(sassEntry, { base: __dirname })
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('.',{includeContent: false, sourceRoot: __dirname}))
.pipe(flatten())
.pipe(gulp.dest(__dirname + '/dist/css'));
easy once you understand more about what it's trying to do I guess.
Since your attempt 4 does everything you want except place the resulting files in the wrong location, the easiest fix would be to just change that location with gulp-rename after the sourcemaps have been generated:
gulp.src(sassEntry, { base: __dirname })
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('.', {includeContent: false, sourceRoot: __dirname}))
.pipe(rename({dirname:''}))
.pipe(gulp.dest('dist/css'));

Gulp-sass how to change dest output

I'm trying to set a shopify dev workflow, and i'm stuck in a problem. How can i change the dest() output in gulp-sass to use .liquid files in the assets folder?
gulp.task('sass', function() {
gulp.src('stylesheets/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./assets/'));
});
I want to get as output something like main.css.liquid, so i can use the .liquid methods.
Is that possible?
There's a good thread about it # Gulp. As said it seems that gulp-rename may be a great fit.
In your case, you can change your code to:
gulp.task('sass', function() {
gulp.src('stylesheets/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.rename('destinationpath/yourfile.liquid'));
.pipe(gulp.dest('./assets/'));
});

gulp autoprefixer gives invalid icon content of font-awesome

I've just setup the following Gulp task for SASS, using gulp-autoprefixer which causing a problem handling font-awesome icon "content".
The way it works (without gulp-autoprefixer)
gulp.task('sass', function() {
gulp.src(['./src/vendor/style.scss',
'./src/app/style.scss'])
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(concat('style.css'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/css'));
});
That works fine and it outputs the result I expect.
An example of the user-icon (without gulp-autoprefixer):
.fa-user:before {
content: "";
}
The way it breaks (with gulp-autoprefixer)
If I now add autoprefixer to this task - like:
gulp.task('sass', function() {
gulp.src(['./src/vendor/style.scss',
'./src/app/style.scss'])
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(concat('style.css'))
.pipe(prefix({
browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1'],
cascade: false
}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/css'));
});
The output breaks now. This is what I get for fa-user (with gulp-autoprefixer):
.fa-user:before {
content: "";
}
It seems like there is a problem with the charset (UTF-8 / UTF-16).
Is there any possibility to avoid that behaviour with gulp-autoprefixer?
Well, even it was a strange behavior (because it worked well without the gulp-autoprefixer), the solution was easier than I thought.
I've missed to add the UTF-8 charset meta-tag in the documents <HEAD>.
So this tag fixed it:
<meta charset="UTF-8">
I also came across this issue, changing the order of the sass compiler and prefixer fixed it for me. Prefixer first, then sass compiler:
.pipe(
autoprefixer({
browsers: ['> 1%', 'last 3 versions'],
cascade: false
})
)
.pipe(
sass({
outputStyle: 'compressed',
includePaths: []
}).on('error', error)
)

How to rename a compiled sass file in a Gulp task

I'm trying to write a simple gulp task that takes a scss file called manifest.scss and after compiling and minifying the file it saves the result into a destination folder as app.css
The following task does almost everything I want beside renaming the file (the output is build/css/manifest.css)
gulp.task('sass', function() {
gulp.src("src/sass/manifest.scss")
.pipe(sass({ style: 'compressed' }))
.pipe(minifyCSS())
.pipe(gulp.dest('build/css'));
});
So, I have tried gulp-rename and I have update the task as follows:
gulp.task('sass', function() {
gulp.src("src/sass/manifest.scss")
.pipe(sass({ style: 'compressed' }))
.pipe(minifyCSS())
.pipe(rename('app.css'))
.pipe(gulp.dest('build/css'));
});
This produces the build/css/app.css file but it is totally blank.
How can I rename the compiled file?
Thanks