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

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

本文关键字:文件夹 dist 拆分 文件 js SRC gulp 何根 Angular      更新时间:2023-09-26

我正在使用gulp-angular为我的项目,我的问题是,我没有足够的经验与节点和gulp修改默认的脚本任务。

我希望能够生成我的应用程序的每个文件夹,以创建一个优化的js文件,该文件包含此特定文件夹的所有其他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']);
};

一旦我们运行cmd:

gulp build

所有的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

另外,我不明白优化后的文件名是如何生成的。

所以回顾一下,我想在dist/scripts文件夹中放入一定量的javascript文件,等于应用程序中现有视图的数量,并按我的意愿重新命名它们…

有没有人能告诉我该怎么做?

从简单的开始,首先把两个css文件放在一起,一步一步地学习它是如何工作的

  1. 创建一个gulp任务并命名为"css"

    饮而尽。任务(css,函数(){//代码会在这里})

  2. 创建要连接的文件数组,使用*.filetype包含目录

    中的所有文件

    gulp.src([‘filename1’,‘filename2’,‘每个/css/文件/这/dir/* . css)

  3. 连接结果,这将从连接和编译的资源

    在公共目录中创建一个main.css文件。

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

  4. 现在终端运行gulp css

  5. 饮而尽。任务('css',函数(){gulp.src (["。/bower_components/angular-material/angular-material.css’,"。/资产/css/* . css"]).pipe (concat (main.css)).pipe (gulp.dest('公共/css/'));});

最后但并非最不重要的是如果你想尝试一点挑战,尝试编译sass,我使用node-bourbon

看一下Angular和Gulp开发的项目模板angularcoffee-boilerplate中的gulpfile.js。

使用以下命令将

.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);
  })