【问题标题】:Grunt cssmin / CleanCSS source map rebasingGrunt cssmin / CleanCSS 源地图变基
【发布时间】:2016-01-05 13:03:01
【问题描述】:

我正在使用具有以下“内容”文件夹结构的 cssmin:

src
|--dir1
|   |--style1.css
|   |--images
|      |--image1.png
|--dir2
   |--style2.css
   |--images
      |--image2.png
dist
|--styles.min.css
|--styles.min.css.map

styles.min.css 和 styles.min.css.map 是连接/缩小“src”文件夹中所有样式表的结果。

我第一次遇到的问题是 styles.min.css 包含错误位置的图像 URL(即“images/image1.png”而不是“../src/dir1/images/image1.png”),但值得庆幸的是grunt 配置修复了:

cssmin: {
        options: {
            rebase: true,
            sourceMap: true
        },
        all: {
            options: {
                keepSpecialComments: 0
            },
            files: {
                'content/dist/styles.min.css': ["content/src/dir1/style1.css", "content/src/dir2/style2.css"]
            }
        }
    }

新问题: 生成的 sourcemap ("styles.min.css.map") 包含如下来源: ["content/src/dir1/style1.css", "content/src /dir2/style2.css"] 而不是 ["../src/dir1/style1.css", "../src/dir2/style2.css"]。这意味着地图指向的位置不正确,例如:

“content/dist/content/src/dir1/style1.css”和“content/dist/content/src/dir2/style2.css”

我能做些什么来解决这个问题?

作为参考,我也尝试了 csswring,但是尽管 sourcemaps 工作正常,但我发现一般图像/导入 url rebase 工作不正常,所以回到 cssmin。

非常感谢!

【问题讨论】:

    标签: gruntjs source-maps gruntfile grunt-contrib-cssmin clean-css


    【解决方案1】:

    想出了我自己的解决方案。我编写了一个任务,它读取源映射 JSON,获取源数组,重新设置它们,然后再次写入文件。这个解决方案似乎对我很有效,希望如果他们处于类似情况,这也可以帮助其他人。只需运行您的 cssmin 任务,然后运行这个:

    grunt.registerTask("rebase-css-sourcemap-sources", "Rebases the CSS source map urls", function() {
    
                var filePath = "./content/dist/styles.min.css.map";
                if (grunt.file.exists(filePath)) {
                    var sourceMap = grunt.file.readJSON(filePath);
    
                    var sources = sourceMap.sources;
                    if (sources) {
                        for (var i = 0; i < sources.length; i++) {
                            sources[i] = sources[i].replace("content/src", "../src");
                        }
                        grunt.file.write(filePath, JSON.stringify(sourceMap));
                        grunt.log.ok("Rebased CSS source map source urls.");
                    }
                } else {
                    grunt.log.error("Source map file does not exist: " + filePath);
                }
            });
    

    虽然这个解决方案对我有用,但如果有人知道解决这个问题的替代方法,理想情况下只使用 cssmin,那就更好了。

    【讨论】:

      猜你喜欢
      • 2016-01-07
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多