简体   繁体   English

CSS和Grunt缩小中的相对路径?

[英]Relative paths in CSS and Grunt minification?

Before I ask this question I want to point out that there are several similar questions posted here on StackOverflow, but none of them provide an accurate solution to the problem. 之前我问这个问题,我想指出的是,也有一些 类似的 问题 张贴在这里在计算器上,但他们没有提供准确的解决问题的方法。


The problem 问题

I have a workflow setup where Grunt combines and minifies multiple css files into one single file for a production environment. 我有一个工作流设置,其中Grunt将多个CSS文件合并并最小化为一个用于生产环境的单个文件。

The problem I'm facing is that font and image paths are breaking after running Grunt, as they're still pointing towards their existing relative file paths. 我面临的问题是字体和图像路径在运行Grunt后中断,因为它们仍指向它们现有的相对文件路径。

As an example: 举个例子:

Within static/homepage/startup/common-files/css/icon-font.css I have the following CSS rule: static/homepage/startup/common-files/css/icon-font.css我有以下CSS规则:

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

In my Gruntfile, I specify that I want the output of my minified css file to be style.min.css located at static/css/custom/homepage/ . 在我的Gruntfile中,我指定要缩小的CSS文件的输出为位于static/css/custom/homepage/ style.min.css The problem with this is that the filepath changes, resulting in all font and image dependencies to no longer be found and return a 404 status in the browser. 问题是文件路径更改,导致不再找到所有字体和图像相关性,并在浏览器中返回404状态。

What I've done to try solve this 我为解决这个问题所做的工作

I've figured that there are 2 options to fix this issue: 我发现有2个选项可以解决此问题:

  1. Copy all dependant files so that they're relative to the new directory where style.min.css resides. 复制所有从属文件,以便它们相对于style.min.css所在的新目录。 The downside to this is that it could quickly become messy and ruin my project folder structure! 不利的一面是它可能很快变得混乱并且破坏我的项目文件夹结构!
  2. Change the paths manually by hand. 手动更改路径。 But then again, what's the point in this? 但是话又说回来,这有什么意义呢? Grunt was designed for automating tasks! Grunt专为自动化任务而设计!

Does anybody know how to fix this problem? 有人知道如何解决此问题吗? I've wasted nearly 10 hours on this and I'm starting to give up. 我在此上浪费了将近10个小时,现在我开始放弃。 People have claimed to have fixed the issue over at the Github issue page , but nobody really says how they fixed it. 人们声称已在Github问题页面上解决了该问题 ,但没有人真正说出他们如何解决此问题。

EDIT: 编辑:

I've looked through the clean-css library source code and it seems that you can pass a relativeTo property to the minifier object. 我浏览了clean-css库源代码,似乎可以将relativeTo属性传递给minifier对象。 I've had a mess around with this but I'm still stuck. 我对此一团糟,但我仍然陷于困境。 I'll report back if I get any further with this. 如果对此有任何进一步的了解,我会报告。

EDIT: 编辑:

Okay I've finally found some documentation which explains what relativeTo (and other) properties do. 好的,我终于找到了一些文档,这些文档解释了relativeTo (和其他)属性的作用。 I'm still stuck on exactly what my configuration should be for my file structure though.... 不过,我仍然坚持我的文件结构应该是什么配置...。

 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 

Here's my Grunt configuration file for reference: 这是我的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"]); }; 

Thanks. 谢谢。

Create a less file in static/css/custom/homepage as styles.less static/css/custom/homepage创建一个更少的文件作为styles.less

@import your css relatively: @import相对地@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";

Then in your gruntfile.js: 然后在您的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"]);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM