我是新来的咕噜声,所以如果这个问题有点傻话我会道歉。
我有以下grunt文件,我继承并且一直在修改/更新:
'use strict';
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);
var getVersion = function(){
var thisVer = require('./bower.json').version || '0.0.0';
var nextVer = '';
var nextVerList = thisVer.split('.');
var nextPatch = new String(new Number(nextVerList.pop()) + 1);
nextVerList.push(nextPatch);
nextVer = nextVerList.join('.');
return nextVer;
}
grunt.initConfig({
yeoman: { app: require('./bower.json').appPath || 'app',
dist: 'dist',
quickdist: 'release-quick',
version: getVersion()
},
clean: {
dist: {
force : true,
files: [{
dot: true,
src: [
'.tmp',
'<%= yeoman.dist %>/*',
'!<%= yeoman.dist %>/.git*'
]
}]
}
},
// Put files not handled in other tasks here
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'.htaccess',
'*.html',
'views/{,**/}*.html',
'lib/**/*',
'images/{,*/}*.{gif,webp}',
'styles/fonts/*'
]
}, {
expand: true,
cwd: '.tmp/images',
dest: '<%= yeoman.dist %>/images',
src: [
'generated/*'
]
}]
}
},
compass: {
options: {
sassDir: '<%= yeoman.app %>/scss',
cssDir: '<%= yeoman.app %>/styles',
generatedImagesDir: '<%= yeoman.app %>/images/generated',
imagesDir: '<%= yeoman.app %>/images',
javascriptsDir: '<%= yeoman.app %>/scripts',
fontsDir: '<%= yeoman.app %>/fonts',
importPath: '<%= yeoman.app %>/lib',
require: [ 'bootstrap-sass', 'compass/import-once/activate' ],
httpImagesPath: '/images',
httpGeneratedImagesPath: '/images/generated',
httpFontsPath: '/fonts',
relativeAssets: true
},
clean: {
options: {
clean: true,
trace: true,
force: true,
debugInfo: false
}
},
dist: {
options: {
debugInfo: false,
trace: true
}
}
},
autoprefixer: {
options: ['last 1 version'],
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/styles/',
src: '{,*/}*.css',
dest: '<%= yeoman.app %>/styles/'
}]
}
},
cssmin: {
dist: {
files: {
'<%= yeoman.dist %>/styles/styles.css': [
'.tmp/styles/{,*/}*.css',
'<%= yeoman.app %>/styles/{,*/}*.css'
]
}
}
},
imagemin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/images',
src: '{,*/}*.{png,jpg,jpeg}',
dest: '<%= yeoman.dist %>/images'
}]
}
},
svgmin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/images',
src: '{,*/}*.svg',
dest: '<%= yeoman.dist %>/images'
}]
}
},
ngAnnotate: {
dist: {
files: [
{
dest: '.tmp/scripts/scripts.js',
src: ['app/scripts/**/*.js'],
}
]
}
},
uglify: {
all_src : {
options : {
mangleProperties: false,
sourceMap : false,
},
files: [
{
dest : 'dist/scripts/scripts.min.js',
src: [ '.tmp/scripts/**/*.js', 'app/scripts/**/*.js' ],
},
{
expand: true,
cwd: 'app/js/',
src: ['*.js', '!*.min.js'],
dest: 'dist/js/',
ext: '.min.js'
}
]
}
},
injector: {
options: {
ignorePath: 'app/', // strips 'app/' from the urls of files
"addRootSlash": false // strips leading '/' from path files
},
local_dependencies: {
files: {
'<%= yeoman.app %>/index.html': [
'<%= yeoman.app %>/scripts/**/*.js',
'<%= yeoman.app %>/styles/**/*.css'],
}
},
bower_dependencies: {
options: {
starttag: '<!-- injector:bower:{{ext}} -->'
},
files: {
'<%= yeoman.app %>/index.html': 'bower.json'
}
}
},
});
grunt.registerTask('build', function (target) {
var preTasks = [], postTasks = [];
var baseTasks = [ 'compass:clean', 'compass:dist','autoprefixer'];
switch(target){
case 'dist':
preTasks = [ 'clean:dist' ];
postTasks = [ 'cssmin', 'imagemin', 'svgmin', 'ngAnnotate', 'uglify', 'copy:dist', 'injector:dist' ];
break;
case 'dev':
default:
preTasks = [ ];
postTasks = [ 'injector' ];
break;
}
var tasks = preTasks.concat(baseTasks).concat(postTasks);
console.log(tasks);
return grunt.task.run(tasks);
});
grunt.registerTask('default', [
'build:dev'
]);
grunt.registerTask('build-dist', [
'build:dist'
]);
/*
grunt.registerTask('build', [
'clean:dist',
'compass:clean',
'compass:dist',
'copy:styles',
'imagemin',
'svgmin',
'injector',
'autoprefixer',
'ngAnnotate',
'copy:dist',
'cssmin',
'uglify',
'htmlmin'
]);//*/
};
如果任务是dev,基于app文件夹中的文件夹,我试图让“injector”注入“yeoman.app”,如果任务是prod,则根据dist中的文件夹使用yeoman.dist文件夹中。
我遇到的问题是它似乎没有出现我可以在注入器配置下为app创建一个子项,一个用于dist,所以我运行它: 喷油器:应用程序 要么 注射器:DIST
结果,配置中的值似乎停留在设置配置时的值。
处理此问题的正确方法是什么?
答案 0 :(得分:0)
想出来。将var添加到配置中,在注入器代码中使用该var,并在运行任务列表之前设置该var:
'use strict';
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);
var getVersion = function(){
var thisVer = require('./bower.json').version || '0.0.0';
var nextVer = '';
var nextVerList = thisVer.split('.');
var nextPatch = new String(new Number(nextVerList.pop()) + 1);
nextVerList.push(nextPatch);
nextVer = nextVerList.join('.');
return nextVer;
}
var yeomanConfig = { app: require('./bower.json').appPath || 'app',
dist: 'dist',
quickdist: 'release-quick',
version: getVersion()
}
grunt.initConfig({
yeoman: yeomanConfig,
injectorTarget : yeomanConfig.app,
clean: {
dist: {
force : true,
files: [{
dot: true,
src: [
'.tmp',
'<%= yeoman.dist %>/*',
'!<%= yeoman.dist %>/.git*'
]
}]
}
},
// Put files not handled in other tasks here
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'.htaccess',
'*.html',
'views/{,**/}*.html',
'lib/**/*',
'images/{,*/}*.{gif,webp}',
'styles/fonts/*'
]
}, {
expand: true,
cwd: '.tmp/images',
dest: '<%= yeoman.dist %>/images',
src: [
'generated/*'
]
}]
}
},
compass: {
options: {
sassDir: '<%= yeoman.app %>/scss',
cssDir: '<%= yeoman.app %>/styles',
generatedImagesDir: '<%= yeoman.app %>/images/generated',
imagesDir: '<%= yeoman.app %>/images',
javascriptsDir: '<%= yeoman.app %>/scripts',
fontsDir: '<%= yeoman.app %>/fonts',
importPath: '<%= yeoman.app %>/lib',
require: [ 'bootstrap-sass', 'compass/import-once/activate' ],
httpImagesPath: '/images',
httpGeneratedImagesPath: '/images/generated',
httpFontsPath: '/fonts',
relativeAssets: true
},
clean: {
options: {
clean: true,
trace: true,
force: true,
debugInfo: false
}
},
dist: {
options: {
debugInfo: false,
trace: true
}
}
},
autoprefixer: {
options: ['last 1 version'],
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/styles/',
src: '{,*/}*.css',
dest: '<%= yeoman.app %>/styles/'
}]
}
},
cssmin: {
dist: {
files: {
'<%= yeoman.dist %>/styles/styles.css': [
'.tmp/styles/{,*/}*.css',
'<%= yeoman.app %>/styles/{,*/}*.css'
]
}
}
},
imagemin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/images',
src: '{,*/}*.{png,jpg,jpeg}',
dest: '<%= yeoman.dist %>/images'
}]
}
},
svgmin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/images',
src: '{,*/}*.svg',
dest: '<%= yeoman.dist %>/images'
}]
}
},
ngAnnotate: {
dist: {
files: [
{
dest: '.tmp/scripts/scripts.js',
src: ['app/scripts/**/*.js'],
}
]
}
},
uglify: {
all_src : {
options : {
mangleProperties: false,
sourceMap : false,
},
files: [
{
dest : 'dist/scripts/scripts.min.js',
src: [ '.tmp/scripts/**/*.js', 'app/scripts/**/*.js' ],
},
{
expand: true,
cwd: 'app/js/',
src: ['*.js', '!*.min.js'],
dest: 'dist/js/',
ext: '.min.js'
}
]
}
},
injector: {
options: {
ignorePath: '<%= injectorTarget %>/', // strips 'app/' from the urls of files
"addRootSlash": false // strips leading '/' from path files
},
local_dependencies: {
files: {
'<%= injectorTarget %>/index.html': [
'<%= injectorTarget %>/scripts/**/*.js',
'<%= injectorTarget %>/styles/**/*.css'],
}
},
bower_dependencies: {
options: {
starttag: '<!-- injector:bower:{{ext}} -->'
},
files: {
'<%= injectorTarget %>/index.html': 'bower.json'
}
}
},
});
grunt.registerTask('build', function (target) {
var preTasks = [], postTasks = [];
var baseTasks = [ 'compass:clean', 'compass:dist','autoprefixer'];
var baseTasks = [];
switch(target){
case 'prod':
grunt.config.set("injectorTarget", yeomanConfig.dist);
//preTasks = [ 'clean:dist' ];
//postTasks = [ 'cssmin', 'imagemin', 'svgmin', 'ngAnnotate', 'uglify', 'copy:dist', 'injector' ];
postTasks = [ 'injector' ];
break;
case 'dev':
default:
grunt.config.set("injectorTarget", yeomanConfig.app);
preTasks = [ ];
postTasks = [ 'injector' ];
break;
}
var tasks = preTasks.concat(baseTasks).concat(postTasks);
console.log(tasks);
return grunt.task.run(tasks);
});
grunt.registerTask('default', [
'build:dev'
]);
grunt.registerTask('Serve-Prod', [
'build:prod'
]);
/*
grunt.registerTask('build', [
'clean:dist',
'compass:clean',
'compass:dist',
'copy:styles',
'imagemin',
'svgmin',
'injector',
'autoprefixer',
'ngAnnotate',
'copy:dist',
'cssmin',
'uglify',
'htmlmin'
]);//*/
};