【发布时间】:2015-08-14 02:36:26
【问题描述】:
我在我的项目中使用gulp-angular,我的问题是我没有足够的节点和 gulp 经验来修改默认脚本任务。
我希望能够为我的应用程序的每个文件夹生成一个优化的 js 文件,该文件包含该特定文件夹的所有其他 js 文件,并为每个文件夹重复此过程。 something like that
这是几乎所有事情都完成的默认文件:** 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 文件,然后按我的意愿重命名它们......
有人可以解释我应该怎么做吗?
【问题讨论】:
标签: javascript angularjs node.js gulp