简体   繁体   English

angular gulp-如何根据每个src文件夹拆分dist文件夹中的js文件

[英]angular gulp - how to split js file in dist folder according to each src folder

i'm using gulp-angular for my projet and my problem is that i don't have enought experience with node and gulp to modify the default scripts task. 我为我的项目使用了gulp-angular ,而我的问题是我对节点和gulp的经验不足,无法修改默认脚本任务。

I want to be able to generate for each folder of my app to create an optimized js file who contains all the others js files of this specific folder and repeat this process for each folder. 我希望能够为我的应用程序的每个文件夹生成一个优化的js文件,该文件包含此特定文件夹的所有其他js文件,并对每个文件夹重复此过程。 something like that 这样的东西

here's the default file where almost everything is done: ** build.js ** 这是几乎完成所有操作的默认文件:** build.js **

'use strict';

var gulp = require('gulp');

var $ = require('gulp-load-plugins')({
  pattern: ['gulp-*', 'main-bower-files', 'uglify-save-license', 'del']
});

module.exports = function(options) {
  gulp.task('partials', function () {
    return gulp.src([
      options.src + '/app/**/*.html',
      options.tmp + '/serve/app/**/*.html'
    ])
      .pipe($.minifyHtml({
        empty: true,
        spare: true,
        quotes: true
      }))
      .pipe($.angularTemplatecache('templateCacheHtml.js', {
        module: 'gulpTest',
        root: 'app'
      }))
      .pipe(gulp.dest(options.tmp + '/partials/'));
  });

  gulp.task('html', ['inject', 'partials'], function () {
    var partialsInjectFile = gulp.src(options.tmp + '/partials/templateCacheHtml.js', { read: false });
    var partialsInjectOptions = {
      starttag: '<!-- inject:partials -->',
      ignorePath: options.tmp + '/partials',
      addRootSlash: false
    };

    var htmlFilter = $.filter('*.html');
    var jsFilter = $.filter('**/*.js');
    var cssFilter = $.filter('**/*.css');
    var assets;

    return gulp.src(options.tmp + '/serve/*.html')
      .pipe($.inject(partialsInjectFile, partialsInjectOptions))
      .pipe(assets = $.useref.assets())
      .pipe($.rev())
      .pipe(jsFilter)
      .pipe($.ngAnnotate())
      .pipe($.uglify({ preserveComments: $.uglifySaveLicense })).on('error', options.errorHandler('Uglify'))
      .pipe(jsFilter.restore())
      .pipe(cssFilter)
      .pipe($.csso())
      .pipe(cssFilter.restore())
      .pipe(assets.restore())
      .pipe($.useref())
      .pipe($.revReplace())
      .pipe(htmlFilter)
      .pipe($.minifyHtml({
        empty: true,
        spare: true,
        quotes: true,
        conditionals: true
      }))
      .pipe(htmlFilter.restore())
      .pipe(gulp.dest(options.dist + '/'))
      .pipe($.size({ title: options.dist + '/', showFiles: true }));
  });

  // Only applies for fonts from bower dependencies
  // Custom fonts are handled by the "other" task
  gulp.task('fonts', function () {
    return gulp.src($.mainBowerFiles())
      .pipe($.filter('**/*.{eot,svg,ttf,woff,woff2}'))
      .pipe($.flatten())
      .pipe(gulp.dest(options.dist + '/fonts/'));
  });

  gulp.task('other', function () {
    return gulp.src([
      options.src + '/**/*',
      '!' + options.src + '/**/*.{html,css,js}'
    ])
      .pipe(gulp.dest(options.dist + '/'));
  });

  gulp.task('clean', function (done) {
    $.del([options.dist + '/', options.tmp + '/'], done);
  });

  gulp.task('build', ['html', 'fonts', 'other']);
};

Once we run the cmd : 一旦我们运行cmd:

gulp build

All the html, css, js etc... files are minified, uglyfied,concatenated etc... and put in a dist folder with this structure: 所有的html,css,js等文件都经过缩小,丑化,串联等处理,并放置在具有以下结构的dist文件夹中:

──  dist/
│   │   ├──  styles/
│   │   │   │   ├──  app-e4a4643d.css
│   │   │   │   └──  vendor-2183d05f.css
│   │   ├──  scripts/
│   │   │   ├──  app-078d1fc3.js
│   │   │   ├──  vendor-b58797da.js
│   ├──  404.html
│   ├──  favico.ico
│   └──  index.html

Also, i don't understant how the names of the optimized files are generated. 另外,我也不了解如何生成优化文件的名称。

So to recap, i want to put in the dist/scripts folder an amount of javascript files equals to the amount of the existing views in the app and rename them as i wish... 因此,回顾一下,我想在dist / scripts文件夹中放入一定数量的javascript文件,使其等于应用程序中现有视图的数量,然后根据需要将其重命名...

Is there someone who can explain me what i shoud do? 有谁可以向我解释我应该做什么?

Start simple, first gulp two css files together to learn how it works step by step 首先启动简单的gulp两个css文件,以逐步了解其工作原理

  1. Create a gulp task and name it something like 'css' 创建一个gulp任务,并将其命名为“ css”

    gulp.task('css', function(){ //code goes here }) gulp.task('css',function(){//代码在这里})

  2. Create an array of the files you want to concatenate, using *.filetype to include everything in a directory 使用*.filetype创建要连接的文件数组,以将所有内容包括在目录中

    gulp.src(['filename1','filename2','every/css/file/inThis/dir/*.css]) gulp.src([ '文件名1', '文件名2','每/ CSS /文件/ inThis /目录/ *。CSS])

  3. Concatenate the result, which creates a main.css file in your public dir from the concatenated and compiled assets 合并结果,这会从已合并和编译的资产中在您的公共目录中创建一个main.css文件

    .pipe(concat('main.css')) .pipe(gulp.dest('public/css/')); .pipe(concat('main.css')).pipe(gulp.dest('public / css /'));

  4. Now in terminal run gulp css 现在在终端中运行gulp css

  5. All together below 一起在下面

    gulp.task('css', function () { gulp.src([ './bower_components/angular-material/angular-material.css', './assets/css/*.css' ]) .pipe(concat('main.css')) .pipe(gulp.dest('public/css/')); }); gulp.task('css',function(){gulp.src(['./bower_components/angular-material/angular-material.css','./assets/css/*.css']).pipe(concat ('main.css')).pipe(gulp.dest('public / css /'));});

Also last but not least if you want to try a little challenge, try compiling sass, I use node-bourbon 最后但并非最不重要的一点是,如果您想尝试一些挑战,尝试编译sass,我使用node-bourbon

Take a look at this gulpfile.js from the project template angularcoffee-boilerplate for Angular and Gulp development. 从用于Angular和Gulp开发的项目模板angularcoffee-boilerplate看这个gulpfile.js

.js and .coffee files are concatenated to one file with the following command: 使用以下命令将.js和.coffee文件连接为一个文件:

var obj = gulp.src(['script1.coffee','script2.coffee')
    .pipe(sourcemaps.init())
    .pipe(coffee({ bare: false, header: false }))
    .pipe(addsrc(['script1.js','script2.js'));

if (options.minify)
    obj = obj.pipe(uglify());

return obj
  .pipe(concat('all.min.js'))
  .pipe(sourcemaps.write('maps'))
  .pipe(gulp.dest(destinationpaths.js))
  .on('error', function (error) {
        console.error('' + error);
  })

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

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