简体   繁体   English

使用gulp concat for CSS如何将main.css连接到我的所有其他CSS

[英]Using gulp concat for css how do I concat main.css to all of my other css

I have a 4 css files. 我有4个CSS文件。 A main.css, first.css, last.css, middle.css. 一个main.css,first.css,last.css,middle.css。 I am using gulp-clean-css to minify all of these files like this. 我正在使用gulp-clean-css来缩小所有这些文件。

gulp.task('pack-css', ['clean-css'], function () {
    return gulp.src(['assets/css/*.css'])
        .pipe(cleanCss())
        .pipe(rev())
        .pipe(gulp.dest('build/css'))
        .pipe(rev.manifest('build/rev-manifest.json', {
            merge: true
        }))
        .pipe(gulp.dest(''));
});

This works fine. 这很好。 It minimizes each css file and puts into the build/css folder keeping the original name. 它将每个css文件最小化,并保留原始名称到build / css文件夹中。 My question is, how do I use gulp-concat to only concat the main.css to the other 3 css files and keep the original file name? 我的问题是,如何使用gulp-concat仅将main.css连接到其他3个CSS文件并保留原始文件名? Before I started using gulp, or minifying, I would just put a @import "main.css"; 在开始使用gulp或缩小之前,我只需要输入@import“ main.css”; at the top of each css page. 在每个CSS页面的顶部。 But now I am using 2 lines of code like this 但是现在我正在使用两行这样的代码

<link rel="stylesheet" href="build/css/main-08c0322a8a.css" type="text/css"/>
    <link rel="stylesheet" href="build/css/first-498ed67d44.css" type="text/css"/>

Your question is still a little unclear and it isn't clear what you have tried(you didn't even include the full file above which would more clearly enumerate the plugins you are currently using) and why. 您的问题仍然有点不清楚,也不清楚您尝试了什么(您甚至没有包括上面的完整文件,它会更清楚地枚举您当前正在使用的插件)以及原因。 You ask the question regarding gulp-concat , but there is nothing in your code referencing that module. 您询问有关gulp-concat的问题,但是您的代码中没有引用该模块的内容。 If I'm understanding your question, then there are two options that I'm aware of: 如果我了解您的问题,那么我知道有两种选择:

OPTION 1: 选项1:

var gulp = require('gulp');
var cleanCSS = require('gulp-clean-css');
var concat = require('gulp-concat');

gulp.task('minify-css', ['minify-first', 'minify-second', 'minify-third']);

gulp.task('minify-first', function() {
  return gulp.src(['./assets/css/main.css', './assets/css/first.css'])
    .pipe(cleanCSS())
    .pipe(concat('first.min.css'))
    .pipe(gulp.dest('./build/css/'));
});

gulp.task('minify-second', function() {
  return gulp.src(['./assets/css/main.css', './assets/css/second.css'])
    .pipe(cleanCSS())
    .pipe(concat('second.min.css'))
    .pipe(gulp.dest('./build/css/'));
});

gulp.task('minify-third', function() {
  return gulp.src(['./assets/css/main.css', './assets/css/third.css'])
    .pipe(cleanCSS())
    .pipe(concat('third.min.css'))
    .pipe(gulp.dest('./build/css/'));
});

OPTION 2: 选项2:

gulp.task('minify-css', ['minify-first', 'minify-second', 'minify-third']);
gulp.task('minify-first', minifyCss('first'));
gulp.task('minify-second', minifyCss('second'));
gulp.task('minify-third', minifyCss('third'));
function minifyCss(srcFileName) {
    return function () {
        return gulp.src(['./assets/css/main.css', `./assets/css/${srcFileName}.css`])
                .pipe(cleanCSS())
                .pipe(concat(`${srcFileName}.min.css`))
                .pipe(gulp.dest('./build/css/'));
    };
}

The rev plugin that you are utilizing above is the one that is adding the hash key to the destination css file. 您上面使用的rev插件是将哈希密钥添加到目标css文件的插件。 This is done as a cache-busting measure which is generally regarded as a good thing. 这是作为消除缓存的措施来完成的,通常认为这是一件好事。 You might be dealing with static file caching in a different manner though and might not need it. 您可能会以其他方式处理静态文件缓存,但可能不需要它。 If you do want to continue using it, there are definitely build steps you could add to update the files containing those references so that you wouldn't manually have to update the file names in those files every time you make a change to one of your css files and run a new build. 如果您确实想继续使用它,则肯定可以添加一些构建步骤来更新包含这些引用的文件,这样您就不必在每次更改其中一个文件时手动更新这些文件中的文件名。 css文件并运行新版本。

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

相关问题 我如何包含所有js文件,就像我们将所有css文件添加到一个main.css中一样 - How can I include all js files like we add all css files in one main.css 如何更改ionic中的main.css类 - How to change a class of the main.css in ionic main.css 文件未加载 - main.css file not loading Concat 2 css文件 - Concat 2 css files 如何使用Gulp 4连接进口的js库? - How do I concat imported js libraries using Gulp 4? 如何在 Grunt/Gulp/Webpack 中使用 Bower 中的图像和字体连接 css 文件 - How to concat css files with images and fonts form Bower in Grunt/Gulp/Webpack 如何从 main.css 文件中提取 @media 打印而不需要单独的 print.css 文件 - How to extract @media print from main.css file without need for seperate print.css file Webpack css 加载器问题“!!../../node_modules/css-loader/index.js!./main.css” - Webpack css loader issues "!!../../node_modules/css-loader/index.js!./main.css" 对于Angular2项目,我如何连接从typescript生成的所有JavaScript文件并将它们添加到我的index.html文件中 - For Angular2 project, In gulp how do I concat all my JavaScript files that were generated from typescript and add them to my index.html file 如何在Angular模板中的CSS属性中连接字符串值 - How to concat a string value in CSS property in Angular template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM