简体   繁体   English

Gulp合并凉亭文件

[英]Gulp to combine bower files

I'm aware this has probably been asked before, but I can't seem to be able to ask Google the right questions to find what I need. 我知道之前可能已经有人问过这个问题,但是我似乎无法向Google问正确的问题以找到我需要的东西。 So I'm possibly thinking about this in the wrong way. 因此,我可能会以错误的方式考虑这个问题。

Basically, I need to know if there is a way to use Gulp with Bower so that all css files in subdirectories under bower_components are combined into one styles.css , all js files in subdirectories under bower_components are combined into one scripts.js . 基本上,我需要知道是否有一种在Bower中使用Gulp的方法,以便将bower_components下的子目录中的所有css文件组合为一个styles.cssbower_components下的子目录styles.css所有js文件均组合为一个scripts.js Kind of how assetic works in symfony2 to combine assets into single files. 怎么样assetic在作品symfony2资产合并到单个文件。 Does each 'built' file in each bower_componets subdirectory have to be linked to manually (in the Gulp config file), or is it more common to loop through them programatically? 是不是每个bower_componets子目录中的每个“构建”文件都必须手动链接(在Gulp配置文件中),还是以编程方式遍历它们是更常见的吗?

Thanks 谢谢

Would something like the below help? 像下面的东西有帮助吗? It loops through all css files in my 'src' directory and spits out one css file in the 'dist' folder. 它循环遍历我的“ src”目录中的所有css文件,并在“ dist”文件夹中吐出一个css文件。 It does the same for my js files: 我的js文件也是如此:

// Config
var requireJsRuntimeConfig = vm.runInNewContext(fs.readFileSync('src/app/require.config.js') + '; require;');
    requireJsOptimizerConfig = merge(requireJsRuntimeConfig, {
        out: 'scripts.js',
        baseUrl: './src',
        name: 'app/startup',
        paths: {
            requireLib: 'bower_modules/requirejs/require'
        },
        include: [
            'requireLib',
            'components/nav-bar/nav-bar',
            'components/home-page/home',
            'text!components/about-page/about.html'
        ],
        insertRequire: ['app/startup'],
        bundles: {
            // If you want parts of the site to load on demand, remove them from the 'include' list
            // above, and group them into bundles here.
            // 'bundle-name': [ 'some/module', 'another/module' ],
            // 'another-bundle-name': [ 'yet-another-module' ]
        }
    });

    // Discovers all AMD dependencies, concatenates together all required .js files, minifies them
    gulp.task('js', function () {
        return rjs(requireJsOptimizerConfig)
            .pipe(uglify({ preserveComments: 'some' }))
            .pipe(gulp.dest('./dist/'));
    });

    // Concatenates CSS files, rewrites relative paths to Bootstrap fonts, copies Bootstrap fonts
    gulp.task('css', function () {
        var bowerCss = gulp.src('src/bower_modules/components-bootstrap/css/bootstrap.min.css')
                .pipe(replace(/url\((')?\.\.\/fonts\//g, 'url($1fonts/')),
            appCss = gulp.src('src/css/*.css'),
            combinedCss = es.concat(bowerCss, appCss).pipe(concat('css.css')),
            fontFiles = gulp.src('./src/bower_modules/components-bootstrap/fonts/*', { base: './src/bower_modules/components-bootstrap/' });
        return es.concat(combinedCss, fontFiles)
            .pipe(gulp.dest('./dist/'));
    });

there is a way and its honestly really simple. 有一种方法,它真的很简单。 you can install "gulp-run" to your npm devDependencies and then use the run to execute bower install. 您可以将“ gulp-run”安装到您的npm devDependencies,然后使用运行来执行Bower安装。

var gulp = require('gulp'),
    del = require('del'),
    run = require('gulp-run'),
    sass = require('gulp-sass'),
    cssmin = require('gulp-minify-css'),
    browserify = require('browserify'),
    uglify = require('gulp-uglify'),
    concat = require('gulp-concat'),
    jshint = require('gulp-jshint'),
    browserSync = require('browser-sync'),
    source = require('vinyl-source-stream'),
    buffer = require('vinyl-buffer'),
    reactify = require('reactify'),
    package = require('./package.json'),
    reload = browserSync.reload;

/**
 * Running Bower
 */
gulp.task('bower', function() {
  run('bower install').exec();
})

/**
 * Cleaning lib/ folder
 */
.task('clean', function(cb) {
  del(['lib/**'], cb);
})

/**
 * Running livereload server
 */
.task('server', function() {
  browserSync({
    server: {
     baseDir: './' 
    }
  });
})

/**
 * sass compilation
 */
.task('sass', function() {
  return gulp.src(package.paths.sass)
  .pipe(sass())
  .pipe(concat(package.dest.style))
  .pipe(gulp.dest(package.dest.lib));
})
.task('sass:min', function() {
  return gulp.src(package.paths.sass)
  .pipe(sass())
  .pipe(concat(package.dest.style))
  .pipe(cssmin())
  .pipe(gulp.dest(package.dest.lib));
})

/**
 * JSLint/JSHint validation
 */
.task('lint', function() {
  return gulp.src(package.paths.js)
  .pipe(jshint())
  .pipe(jshint.reporter('default'));
})

/** JavaScript compilation */
.task('js', function() {
  return browserify(package.paths.app)
  .transform(reactify)
  .bundle()
  .pipe(source(package.dest.app))
  .pipe(gulp.dest(package.dest.lib));
})
.task('js:min', function() {
  return browserify(package.paths.app)
  .transform(reactify)
  .bundle()
  .pipe(source(package.dest.app))
  .pipe(buffer())
  .pipe(uglify())
  .pipe(gulp.dest(package.dest.lib));
})

/**
 * Compiling resources and serving application
 */
.task('serve', ['bower', 'clean', 'lint', 'sass', 'js', 'server'], function() {
  return gulp.watch([
    package.paths.js, package.paths.jsx, package.paths.html, package.paths.sass
  ], [
   'lint', 'sass', 'js', browserSync.reload
  ]);
})
.task('serve:minified', ['bower', 'clean', 'lint', 'sass:min', 'js:min', 'server'], function() {
  return gulp.watch([
    package.paths.js, package.paths.jsx, package.paths.html, package.paths.sass
  ], [
   'lint', 'sass:min', 'js:min', browserSync.reload
  ]);
});

what is really beautiful with this setup I just posted is this is making a custom gulp run called "serve" that will run your setup with a development server (with live reload and much better error intelligence) all you have to do is go to your directory and type "gulp serve" and it'll run bower install and build everything for you. 我刚刚发布的此设置的真正美丽之处在于,它使一个名为“ serve”的自定义gulp运行,它将与开发服务器一起运行您的设置(具有实时重载和更好的错误情报),所有您需要做的就是目录,然后输入“ gulp serve”,它将运行bower install并为您构建所有内容。 obviously the folder structure is different so you will need to make some modifications, but hopefully this shows how you can run bower with gulp :) 显然,文件夹结构是不同的,因此您将需要进行一些修改,但是希望这显示了如何使用gulp运行bower :)

Something like the below was what I was looking for, except I don't like having to add the paths manually. 我一直在寻找以下类似的东西,除了我不喜欢必须手动添加路径。 This is why I prefer something like CommonJS for Javascript. 这就是为什么我更喜欢CommonJS for Javascript之类的原因。 I definitely remember seeing a way in which CSS files were picked up automatically from the bower_components folder, I believe it was in the Wordpress Roots project (based on the settings/overrides in bower.json). 我绝对记得曾经看到过一种从bower_components文件夹中自动拾取CSS文件的方式,我相信它是在Wordpress Roots项目中(基于bower.json中的设置/替代)。

gulp.task('css', function () {

    var files = [
        './public/bower_components/angular-loading-bar/build/loading-bar.css',
        './public/bower_components/fontawesome/css/font-awesome.css'
    ];

    return gulp.src(files, { 'base': 'public/bower_components' })
               .pipe(concat('lib.css'))
               .pipe(minifyCss())
               .pipe(gulp.dest('./public/build/css/')
    );
});

gulp.task('js-lib', function () {
    var files = [
        'public/bower_components/jquery/dist/jquery.js',
        'public/bower_components/bootstrap-sass/assets/javascripts/bootstrap.js'
    ];


    return gulp.src(files, { 'base': 'public/bower_components/' })
               .pipe(sourcemaps.init())
               .pipe(uglify({ mangle: false }))
               .pipe(concat('lib.js'))
               .pipe(sourcemaps.write('./'))
               .pipe(gulp.dest('./public/build/js/')
    );
});

您可以使用gulp-main-bower-files库在.bower.json中添加包的主文件,而不是手动声明它们

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

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