Grunt concat + uglify with sourcemaps

2020-02-07 17:13发布

I use concat to merge JS files into one file and uglify to minimize the JavaScript. How can I create a sourcemaps file that uses the source JS files?

My current gruntfile:

concat: {
    options: {
        // define a string to put between each file in the concatenated output
        separator: ';'
    },
    dist: {
        // the files to concatenate
        src: ['<%= config.src %>/js/**/*.js'],
        // the location of the resulting JS file
         dest: '<%= config.dist %>/js/main.js'
    }
},

uglify: {
    dist: {
        files: {
            '<%= config.dist %>/js/main.min.js': ['<%= concat.dist.dest %>']
        }
    }
},

2条回答
一纸荒年 Trace。
2楼-- · 2020-02-07 17:40

You need to enable source maps on both the concat and uglify tasks, and you must specify the sourceMapIn option for the uglify task.

Here's a sample grunt config:

concat : {
  options : {
    sourceMap :true
  },
  dist : {
    src  : ['www/js/**/*.js'],
    dest : '.tmp/main.js'
  }
},
uglify : {
  options : {
    sourceMap : true,
    sourceMapIncludeSources : true,
    sourceMapIn : '.tmp/main.js.map'
  },
  dist : {
    src  : '<%= concat.dist.dest %>',
    dest : 'www/main.min.js'
  }
}
查看更多
我命由我不由天
3楼-- · 2020-02-07 17:41

Per the grunt-contrib-uglify docs, you can enable sourcemap generation as part of the uglify process.

Your uglify config would look something like:

uglify: {
        dist: {
            files: {
                '<%= config.dist %>/js/main.min.js': ['<%= concat.dist.dest %>']
            },
            options: {
                sourceMap: true
        }
    },
查看更多
登录 后发表回答