简体   繁体   English

如何合并和清理具有重叠选择器/属性的 2 css 文件?

[英]How to merge and clean 2 css files with overlapping selectors/properties?

Here I am, trying to do a decent job at maintaining a site and adding new elements to it.我在这里,试图在维护网站和向其添加新元素方面做得不错。 But before that, I'd like to clean its css files.但在此之前,我想清理它的 css 个文件。

That site is using 2 stylesheets, a v1 and a v2.该站点使用 2 个样式表,一个 v1 和一个 v2。 Basically, the v2 adds new rules but also overrides rules/properties from the v1.基本上,v2 添加了新规则,但也覆盖了 v1 中的规则/属性。 So I can't just get rid of v1 because v2 doesn't contain all rules and properties.所以我不能只删除 v1,因为 v2 不包含所有规则和属性。

Here's a quick example这是一个简单的例子

From v1从 v1

.globalFooter {
    background-color: #eee;
    color: #fff;
    font-size: 15px;
}

From v2从 v2

.globalFooter {
    font-size: 18px;
    display: inline;
}

And so, the result should be like所以,结果应该是这样的

.globalFooter {
    background-color: #eee;
    color: #fff;
    font-size: 18px;
    display: inline;
}

I thought about manually merging v2 into v1, moving the new elements and overriding properties, but v2 is 3000 lines long...我考虑过手动将 v2 合并到 v1 中,移动新元素并覆盖属性,但是 v2 有 3000 行长......

So then I tried to find a tool that would do that automatically but I didn't find anything solid.因此,我试图找到一个可以自动执行此操作的工具,但我没有找到任何可靠的东西。 I need something that can merge the two files and consider that the 2nd file is loaded after, so it can overrides rules and properties of the 1st one.我需要一些可以合并这两个文件的东西,并考虑到第二个文件是在之后加载的,所以它可以覆盖第一个文件的规则和属性。

Does someone know a good tool/script doing that?有人知道这样做的好工具/脚本吗?

Thanks谢谢

After only a few minutes of searching i found this: CSS Compressor 经过仅几分钟的搜索,我发现了这一点: CSS Compressor

Basically is allows you to upload a few css files, select different options such as combining properties, and then it merges the files into one sheet. 基本上,您可以上传几个CSS文件,选择其他选项(例如合并属性),然后将文件合并为一张纸。 I havent used it, but I've heard it work quite well with only a few discrepancies. 我还没有使用过它,但是我听说它工作得很好,只有一些差异。

Check it out and let us know if it works 检查一下,让我们知道是否可行

There is also This and This list of other helpful tools 还有This and This其他有用工具的列表

With this combination i solved and optimize the css to the maximum. 通过这种组合,我解决了CSS并将其优化到最大程度。

After configuring Grunt, install the following plugins: 配置Grunt之后,请安装以下插件:

Gruntfile.js Gruntfile.js

Create the task to minify, merge selectors & properties, remove duplicates, etc. 创建任务以缩小,合并选择器和属性,删除重复项等。

cssmin: {
    options: {
        advanced: true, 
        mergeAdjacent: true,
        removeDuplicates: true,
        shorthandCompacting: false 
    },
    files: {    
        src: 'your/path/style.css',
        dest: 'your/path/style.min.css' 
    }  
},

After create a task to parse CSS, add prefixes and optimize more. 创建用于解析CSS的任务后,添加前缀并进行更多优化。

postcss: {
    options: {
        processors: [
            require('postcss-flexbugs-fixes'),
            require('autoprefixer')({
                browsers: [  
                    'last 2 versions',  
                    'Edge >= 12',
                    'Explorer >= 9']
            }),
            require('cssnano')
        ],
        map: false,
    }, 
    files: {    
        src: 'your/path/style.min.css',
        dest: 'your/path/style.min.css' 
    }
}

Register a shortcut to the tasks 注册任务的快捷方式

grunt.registerTask('css', ['cssmin', 'postcss']);

And Voilà!! 还有Voilà!

grunt css 

I was looking for exactly the same thing as you do and couldn't believe that there is nothing easily to be found, even though the usecase seems to me pretty common.我一直在寻找与您完全相同的东西,并且无法相信没有任何东西可以轻易找到,即使用例在我看来很常见。

Nevertheless, I found perfect tool which does exactly what you asked for - CSSO .尽管如此,我还是找到了完美的工具,它完全可以满足您的要求 - CSSO It merges same classes together and omits duplicate CSS rules.它将相同的类合并在一起并省略重复的 CSS 规则。

Thanks Tiago Fontella on debian/Linux 在Tibian / Linux上感谢Tiago Fontella

merge css using 使用合并css

 cat a.css b.css d.css > merged.css 

apt-get update
apt-get upgrade
apt-get install curl   
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install build-essential nodejs
mkdir node-project
cd node-project
npm init
npm install -g grunt (or npm install grunt?) 
npm install grunt-postcss --save-dev
npm install grunt-contrib-cssmin --save-dev
npm install --save-dev grunt-cssnano
npm install --save-dev clean-css
npm i postcss-flexbugs-fixes
npm i autoprefixer

script must be in node-project: Gruntfile.js use nano Gruntfile.js to create it 脚本必须在节点项目中:Gruntfile.js使用nano Gruntfile.js创建它

   module.exports = function(grunt) {
       grunt.initConfig({
           cssmin: {
              options: {
                  advanced: true,
                  mergeAdjacent: true,
                  removeDuplicates: true,
                  shorthandCompacting: false
              },
              files: {
                  src: 'your/path/style.css',
                  dest: 'your/path/style.min.css'
              }
           },
           postcss: {
              options: {
                  processors: [
                      require('postcss-flexbugs-fixes'),
                      require('autoprefixer')({
                          browsers: [
                              'last 2 versions',
                              'Edge >= 12',
                              'Explorer >= 9']
                      }),
                      require('cssnano')
                  ],
                  map: false,
              },
              files: {
                  src: 'your/path/style.min.css',
                  dest: 'your/path/style.min.css'
              }
           }
       });
       grunt.loadNpmTasks('grunt-contrib-cssmin');
       grunt.loadNpmTasks('grunt-postcss');
       grunt.registerTask('css', ['cssmin', 'postcss']);
   };

You could use concat during build time to merge the two files 您可以在构建期间使用concat合并两个文件

First you need to download it from npm.com/package/concat 首先,您需要从npm.com/package/concat下载它

then merge and require the new file 然后合并并需要新文件

In node or package.json for automation, the merge would look something like this; 在用于自动化的node或package.json中,合并看起来像这样;

concat ./v1.js ./v2.js -o /vOutput.js

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

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