Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoprefixer Breaks CSS Source Maps #368

Closed
bobrocke opened this issue Dec 30, 2014 · 7 comments
Closed

Autoprefixer Breaks CSS Source Maps #368

bobrocke opened this issue Dec 30, 2014 · 7 comments

Comments

@bobrocke
Copy link

Here's my gruntfile.js:

module.exports = function(grunt) {

    // Load Grunt tasks declared in the package.json file
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({
        sass: {
        options: {
            sourceMap: true
        },
            dist: {
                files: {
                    'user/themes/br/css-compiled/bobmess.css': 'user/themes/br/scss/bobmess.scss'
                }
            }
        },

        autoprefixer: {
            dist: {
                src: 'user/themes/br/css-compiled/bobmess.css'
            }
        },

        watch: {
            scss: {
                files: ['user/themes/br/scss/*.scss'],
                tasks: ['sass', 'autoprefixer'],
            },

            livereload: {
                files: ['user/**/*.{css,md,twig,js,yaml}'],
                options: {
                    livereload: true
                }
            }
        }
    });

    grunt.registerTask('default', ['watch']);
};

Running it creates a css file and a source map with LibSass via grunt-sass. But Chrome does not recognize the source map. If I comment out the autoprefixer task, the source map is recognized by Chrome. I've tried it with grunt-contrib-sass (which uses Ruby Sass) and I find the same behavior.

It seems as if autoprefixer is messing up the source map, or the reference to it, somehow.

@ai
Copy link
Member

ai commented Dec 30, 2014

Please, create issue in grunt-autoprefixer. Because it is most likely, that issue is configuration or in environment.

@ai ai closed this as completed Dec 30, 2014
@jonathantneal
Copy link
Member

This issue is not limited to grunt. For example: sindresorhus/gulp-autoprefixer#1, gulp-sourcemaps/gulp-sourcemaps#60, dlmanning/gulp-sass#106, appleboy/gulp-compass#92

We can produce a bogus sourcemap by adding gulp-sass to the current gulp example.

gulp.task('autoprefixer', function () {
    var postcss      = require('gulp-postcss');
    var sourcemaps   = require('gulp-sourcemaps');
    var autoprefixer = require('autoprefixer-core');
    var sass         = require('gulp-sass');

    return gulp.src('./src/*.css')
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(postcss([ autoprefixer({ browsers: ['last 2 version'] }) ]))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('./dest'));
});

One work-around has been to roll back gulp-sass to ~1.2.4 and to re-initialize sourcemaps before Autoprefixer is run.

gulp.task('autoprefixer', function () {
    var postcss      = require('gulp-postcss');
    var sourcemaps   = require('gulp-sourcemaps');
    var autoprefixer = require('autoprefixer-core');
    var sass         = require('gulp-sass');

    return gulp.src('./src/*.css')
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(sourcemaps.write('.'))
        .pipe(sourcemaps.init({ loadMaps: true }))
        .pipe(postcss([ autoprefixer({ browsers: ['last 2 version'] }) ]))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('./dest'));
});

@bobrocke
Copy link
Author

bobrocke commented Mar 1, 2015

I eventually got it working with:

module.exports = function(grunt) {

    // Load Grunt tasks declared in the package.json file
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({
        sass: {
            options: {
                sourceMap: true
            },
            dist: {
                files: {
                    'user/themes/br/css-compiled/bobmess.css': 'user/themes/br/scss/bobmess.scss'
                }
            }
        },

        autoprefixer: {
            options: {
                map: true
            },
            dist: {
                src: 'user/themes/br/css-compiled/bobmess.css'
            }
        },

        watch: {
            scss: {
                files: ['user/themes/br/scss/*.scss'],
                tasks: ['sass', 'autoprefixer'],
            },

            livereload: {
                files: ['user/**/*.{css,md,twig,js,yaml}'],
                options: {
                    livereload: true
                }
            }
        }
    });

    grunt.registerTask('default', ['watch']);
};

Explicitly turning on source maps for autoprefixer was the trick for me.

@bryanerayner
Copy link

I don't believe this issue should be closed. It seems that this is still a problem, and there hasn't really been an investigation put forth here, just some work arounds. That doesn't solve the problem.

@ai
Copy link
Member

ai commented Oct 16, 2015

@bryanerayner I have many user cases, where source map works perfect. Unfortunately, you should be very careful with paths to have source map between different tool. So, you should fix your environment, or move to PostCSS only solution (PostCSS has variable, mixins and nested too, and without complicated settings)'

@bryanerayner
Copy link

Thanks @ai. It's been difficult to nail this down, I'm stepping through the source with node-debug ATM and still trying to find the source of the problem. It might ultimately be caused by another part of the pipeline, but when stepping through it's ultimately being thrown by a part of gulp-postcss.

image

I'll report what I find here, or on the appropriate repo, wherever it ends up being. Would it be appropriate to add either a wiki page or a the gulp section of the readme, advising on known problems and solutions? It seems like there's a lot of finger pointing between all the plugins involved in this error, and it would be good to curb that somehow.

@ai
Copy link
Member

ai commented Oct 16, 2015

@bryanerayner you are right, but we can't add docs section to every plugin. So we should add it to gulp-postcss. Sorry, that I try wash Autoprefixer hands, but we have many popular plugins there and I try to support them all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants