【问题标题】:Grunt-injector - Handling Multiple TargetsGrunt-injector - 处理多个目标
【发布时间】:2015-11-20 19:43:55
【问题描述】:

我是 grunt 的新手,所以如果这个问题有点愚蠢,我深表歉意。

我已经继承并一直在修改/更新以下 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,我正在尝试让“injector”在 yeoman.app 上运行,基于 app 文件夹中的文件夹,如果任务是 prod,我正在 yeoman.dist 上运行,基于 dist 中的文件夹文件夹。

我遇到的问题是它没有出现我可以在注入器配置下创建一个子项,一个用于应用程序,一个用于dist,所以我运行它: 注射器:应用程序 或者 注射器:dist

因此,配置中的值似乎停留在设置配置时的值。

处理这个问题的正确方法是什么?

【问题讨论】:

    标签: node.js gruntjs


    【解决方案1】:

    想通了。在配置中添加一个 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'
      ]);//*/
    
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多