【问题标题】:Relative paths in CSS and Grunt minification?CSS和Grunt缩小中的相对路径?
【发布时间】:2014-12-25 12:24:22
【问题描述】:

在我问这个问题之前,我想指出 StackOverflow 上有 several similar questions posted,但它们都没有为问题提供准确的解决方案。


问题

我有一个工作流设置,其中 Grunt 将多个 css 文件合并并缩小为一个用于生产环境的文件。

我面临的问题是字体和图像路径在运行 Grunt 后会中断,因为它们仍然指向它们现有的相对文件路径。

举个例子:

static/homepage/startup/common-files/css/icon-font.css 内,我有以下 CSS 规则:

@font-face{font-family:Flat-UI-Icons;src:url(../fonts/Startup-Icons.eot);

在我的 Gruntfile 中,我指定我希望缩小的 css 文件的输出为位于 static/css/custom/homepage/style.min.css。这样做的问题是文件路径发生变化,导致不再找到所有字体和图像依赖项并在浏览器中返回 404 状态。

我做了什么来尝试解决这个问题

我认为有 2 个选项可以解决此问题:

  1. 复制所有相关文件,使它们与style.min.css 所在的新目录相关。这样做的缺点是它可能很快变得混乱并破坏我的项目文件夹结构!
  2. 手动更改路径。但是话又说回来,这有什么意义呢? Grunt 专为自动执行任务而设计!

有人知道如何解决这个问题吗?我已经在这上面浪费了将近 10 个小时,我开始放弃了。人们声称已经在Github issue page 上解决了这个问题,但没有人真正说出他们是如何解决的。

编辑:

我查看了clean-css library 源代码,您似乎可以将relativeTo 属性传递给缩小器对象。我对此感到一团糟,但我仍然陷入困境。如果我对此有任何进一步的了解,我会回来报告。

编辑:

好的,我终于找到了一些解释relativeTo(和其他)属性的文档。我仍然坚持我的文件结构应该是什么配置....

relativeTo - path to resolve relative @import rules and URLs
root - path to resolve absolute @import rules and rebase relative URLs
roundingPrecision - rounding precision; defaults to 2; -1 disables rounding
target - path to a folder or an output file to which rebase all URLs

这是我的 Grunt 配置文件供参考:

    module.exports = function(grunt) {
        grunt.initConfig({
            concat: {
                homepage: {
                    src: [
                        'static/homepage/startup/common-files/css/icon-font.css', 
                        'static/homepage/startup/flat-ui/bootstrap/css/bootstrap.css',
                        'static/homepage/startup/flat-ui/css/flat-ui.css',
                        'static/homepage/static/css/style.css'
                    ],
                    dest: 'static/css/custom/homepage/compiled.css',
                }
            },
            cssmin: {

                homepage: {
                    src: 'static/css/custom/homepage/compiled.css',
                    dest: 'static/css/custom/homepage/style.min.css'
                }   
            }                           
        });             

        grunt.loadNpmTasks('grunt-contrib-concat');
        grunt.loadNpmTasks('grunt-contrib-uglify');
        grunt.loadNpmTasks('grunt-contrib-cssmin');
        grunt.loadNpmTasks("grunt-css-url-rewrite");
        grunt.registerTask('build', ['concat', 'cssmin']);
        grunt.registerTask('default', ["build"]);
};

谢谢。

【问题讨论】:

  • 我也有同样的问题,花了几个小时尝试使用“relativeTo”、“target”和“root”但没有成功:/
  • 我可能正在接近解决方案!在这里查看我的评论(我的用户名是 jjmpsp):github.com/yeoman/grunt-usemin/issues/225
  • 好的,我明天试试看。 (我已经感觉到痛了^^)
  • 好吧,我终于设法让它工作了。您应该删除 concat 任务并仅使用 cssmin。然后尝试使用options.target,也许是'static/css/custom/homepage/style.min.css'。对于像我这样使用 usemin 的人,你可以通过手动设置流程来移除 concat 任务。

标签: css gruntjs relative-path minify grunt-contrib-concat


【解决方案1】:

static/css/custom/homepage 中创建一个less 文件为styles.less

@import你的css相对:

@import "../../../homepage/startup/common-files/css/icon-font.css";
@import "../../../homepage/startup/flat-ui/bootstrap/css/bootstrap.css";
@import "../../../homepage/startup/flat-ui/css/flat-ui.css";
@import "../../../homepage/static/css/style.css";

然后在你的 gruntfile.js 中:

module.exports = function(grunt) {
    grunt.initConfig({
      less: {
        dist: {
            options: {
                compress: true,
                sourceMap: false,
                // Add any other path/to/fonts here
                paths: ['static/homepage/startup/common-files']
            },
            files: {
                'public/css/dist/style.min.css': 'public/css/default/styles.less'
            }
        }
    }       
  });             

  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks("grunt-css-url-rewrite");
  grunt.registerTask('build', ["less:dist"]);
  grunt.registerTask('default', ["build"]);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多