【问题标题】:Can't get Sass Source Maps working with grunt-autoprefixer无法让 Sass 源映射与 grunt-autoprefixer 一起使用
【发布时间】:2014-12-18 21:37:19
【问题描述】:

我在我的项目中使用带有这些依赖项的 Grunt 0.4.5:

"grunt": "~0.4.5",
"grunt-contrib-concat": "~0.5.0",
"grunt-contrib-uglify": "~0.6.0",
"grunt-contrib-watch": "~0.6.1",
"grunt-contrib-sass": "~0.8.1"

我刚刚注意到 grunt-autoprefixer 没有被添加到我的 package.json 文件中。那是因为它没有 grunt-contrib 前缀吗?无论如何,当我从监视任务中删除 autoprefixer 时,Source Maps 对我有用,但是一旦我再次添加它,编译的 css 文件末尾的注释就会被删除。

这是我的自动前缀配置:

    autoprefixer: {
        options: {
            browsers: ['last 2 version', 'ie 8', 'ie 9']
        },
        single_file: {
              options: {
                // Target-specific options go here.
              },
              src: 'library/css/style.css',
              dest: 'library/css/style.css'
            },
        sourcemap: {
            options: {
                map: true
            }
        },
    },

我的 Sass 配置如下所示:

    sass: {
        dist: {
            options: {
                style: 'expanded',
                debugInfo: true,
                sourcemap: true
            },
            files: {
                'library/css/style.css': 'library/scss/style.scss'
            }
        } 
    },

这是我的监视任务:

    watch: {
        options: {
            livereload: true,
            },
        scripts: {
            files: ['library/js/*.js'],
            tasks: ['concat', 'uglify'],
            options: {
                spawn: false,
            },
        },
        css: {
            files: ['library/scss/*.scss'],
            tasks: ['sass', 'autoprefixer'],
            sourceComments: 'normal',
            options: {
                spawn: false,
            }
        },
        page: {
            files: ['*.php', '*.html']
        }
    }

老实说,我完全不明白为什么 autoprefixer 必须干扰源映射,我尝试使用 false 而不是 true,按照 grunt-autoprefixer repo 中的示例手动指定源映射,但无论如何我做了评论 - /*# sourceMappingURL=style.css.map */ 被从文件中删除,并且源映射在 chrome 中不起作用。

编辑:抱歉,我刚刚使用:

        sourcemap: {
            options: {
                map: true
            },
            src: 'library/css/style.css',
            dest: 'library/css/style.css' // -> dest/css/file.css, dest/css/file.css.map
        },

我很想知道,出于性能原因,我现在是否需要在 grunt-contrib-sass 配置中指定源映射选项?编译大约需要 2.4 秒,所以每 0.1 秒都很重要!

【问题讨论】:

    标签: sass gruntjs source-maps autoprefixer


    【解决方案1】:

    我发现以下内容对我有用:

    sass: {
      options: {
        sourceMap: true,
        sourceMapEmbed: false
      },
    
      dist: {
        files: {
          'css/style.css': 'css/src/style.scss',
          'css/debug.css': 'css/src/debug.scss',
        }
      }
    },
    autoprefixer: {
      //prefix all files
      multiple_files: {
        expand: true,
        flatten: true,
        src: 'css/*.css',
        dest: 'css/'
      },
      options: {
        map: true,
        annotation: false
      }
    },
    

    如果没有指定 annotation: false,我发现 autoprefixer 总是用无法工作的 data-uri sourceMappingURL 覆盖 /*# sourceMappingURL=style.css.map */(我的浏览器检查器不会显示原始 .scss 源)。

    我还发现,如果您有多个 scss/css 文件,则在接受的答案中使用 sourcemap 不起作用。我相信 sourcemap 允许您为每个 scss->css 映射设置一组不同的选项,但我希望我的所有设置都相同。

    希望这会有所帮助!

    【讨论】:

    • 工作就像一个魅力(竖起大拇指)!
    【解决方案2】:

    抱歉,我刚刚使用:

        sourcemap: {
            options: {
                map: true
            },
            src: 'library/css/style.css',
            dest: 'library/css/style.css' // -> dest/css/file.css, dest/css/file.css.map
        },
    

    我很想知道,出于性能原因,我现在是否需要在 grunt-contrib-sass 配置中指定 sourcemap 选项?编译大约需要 2.4 秒,所以每 0.1 秒都很重要!我会做一些测试看看,据我所见,源映射现在默认是由 sass 生成的,所以我可能只需要在 autoprefixer 中指定它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 2013-06-21
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 2019-07-05
      相关资源
      最近更新 更多