将变量打印到模板

grunt print variable to template

我正在尝试将变量打印到横幅模板,但输出是空注释,是否可以将值插入模板?

我的代码:

//get the license text
var license = grunt.file.read('license.txt', {'encoding': 'utf-8'});

然后在 uglify 任务中我想将该文本添加为​​横幅

uglify: {
    options: {
        compress: true,
        mangle: true,
        sourceMap: false,
        banner: '/*! <%= license %> */'
    },
    target: {
        src: 'dist/app.min.js',
        dest: 'dist/app.min.js'
    }
},

但是输出的文件在任何代码开始之前只包含一个空注释

/*!  */

我可以 console.log(license) 确认它的值被正确检索。

我不确定,但似乎编码导致了问题。

不知道你的整个 grunt 设置,许可证变量应该放在配置对象中,例如:

var appConfig = {
  app: require('./bower.json').appPath || 'app',
  dist: 'dist',
  license: grunt.file.read('license.txt')
};

(注意 utf-8 编码已被删除)。

我的 uglify 任务略有不同 - 没有使用 compressmanglesourceMap 选项。您似乎也可以使用 targetmy_target:

uglify: {
  options: {
    banner: '/*! ' + '<%= yeoman.license %> */'
  },
  my_target: {
    files: {
      '<%= yeoman.dist %>/scripts/scripts.js': ['<%= yeoman.dist %>/scripts/scripts.js']
    }
  }
 }

这是有效的。简化的 grunt 文件:

module.exports = function (grunt) {

  ...

  // Configurable paths for the application
  var appConfig = {
    app: require('./bower.json').appPath || 'app',
    dist: 'dist',
    license: grunt.file.read('license.txt')
  };

  grunt.initConfig({

    yeoman: appConfig,

    ...

    uglify: {
      options: {
        banner: '/*! ' +
          '<%= yeoman.license %> */'
      },
      my_target: {
        files: {
          '<%= yeoman.dist %>/scripts/scripts.js': ['<%= yeoman.dist %>/scripts/scripts.js']
        }
      }
     },

    ...

  });

};