如何用grunt-contrib-uglify给多文件生成source-map

这里也有个问题:http://stackoverflow.com/questions/14207983/how-to-specify-multiple-source-maps-in-uglify-grunt-task

参考了这里 http://flippinawesome.org/2013/07/01/building-a-javascript-library-with-grunt-js/

module.exports = function(grunt) {
  var bannerContent = '... banner template ...';
  var name = '<%= pkg.name %>-v<%= pkg.version%>';  

  grunt.initConfig({
    // pkg must be defined inside initConfig object
    pkg : grunt.file.readJSON('package.json'),
    // uglify configuration
    uglify: {
      options: {
        banner: bannerContent,
        sourceMapRoot: '../',
        sourceMap: 'distrib/'+name+'.min.js.map',
        sourceMapUrl: name+'.min.js.map'
      },
      target : {
        src : ['src/**/*.js'],
        dest : 'distrib/' + name + '.min.js'
      }
    },
    concat: { /* ... concat configuration ... */ },
    jshint: { /* ... jshint configuration ... */ }
  });

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.registerTask('default', ['jshint', 'concat', 'uglify']);
};

我的配置

uglify : {
            options : {
                banner:'/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',
                sourceMapRoot: '../',
                sourceMap:'dist/' + '.map',
                sourceMapUrl: '.min.js.map'
            },
            main:{
                files: [
                    {
                        expand: true,
                        cwd: 'dist/',
                        src: ['js/**/*.js', '!js/**/*-debug.js'],
                        dest: 'dist/'
                    }
                ]
            }
        }

最后只生成了一个 .min.js.map,只是对一个js的map,没达到目录下所有的js的source-map效果

阅读 9.6k
1 个回答

参考上面 http://stackoverflow.com/a/17503492

uglify : {
            options : {
                banner:'/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',
                sourceMapRoot: './dist/map/',
                sourceMap:function(path) { return path.replace(/js/,'map').replace('.js',".map")}
            },
            main:{
                files: [
                    {
                        expand: true,
                        cwd: 'dist/',
                        src: ['js/**/*.js', '!js/**/*-debug.js'],
                        dest: 'dist/'
                    }
                ]
            }
        }

解决

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏