简体   繁体   English

使用多级源映射调试Javascript

[英]Debug Javascript with multi-level source maps

I have a lot of javascript files, which goes through grunt uglify and gets minified individually, and further, I am performing grunt concat on these to get a single bundled minified file with source map. 我有很多JavaScript文件,这些文件经过grunt uglify并分别缩小,并且进一步,我对它们进行grunt concat以获得带有源映射的单个捆绑的缩小文件。

Ex. 防爆。

a.js, b.js, c.js -> Uglify -> a.min.js, b.min.js,c.min.js -> concat -> bundle.min.js a.js,b.js,c.js ->丑化-> a.min.js,b.min.js,c.min.js ->的concat -> bundle.min.js

With dev tools and source map, from bundle.min.js, I can trace back only up to a.min.js/b.min.js/c.min.js. 使用来自bundle.min.js的开发工具和源映射,我只能追溯到a.min.js / b.min.js / c.min.js。 Where as my goal is to use source map to trace back up to a.js/b.js/c.js. 我的目标是使用源映射来追溯到a.js / b.js / c.js。

Your requirement can be achieved, however you'll need to change the order of your tasks to the following instead: 您的要求可以实现,但是您需要将任务的顺序更改为以下内容:


a.js, b.js, c.js --> concat --> bundle.js --> uglify --> bundle.min.js a.js, b.js, c.js --> 的concat --> bundle.js --> 丑化 --> bundle.min.js


Note: The order of the tasks has been changed to concatenate the individual .js files before uglifying the resultant output. 注意:更改了任务的顺序,以在拼合结果输出之前连接各个.js文件。

Why is it necessary to change the order of tasks? 为什么必须更改任务顺序?

Because grunt-contrib-uglify provides the sourceMapIn option, whilst grunt-contrib-concat simply doesn't. 因为grunt-contrib- sourceMapIn提供了sourceMapIn选项,而grunt-contrib-concat却没有。 Besides it's typical practice to concatenate files before uglifying them. 此外,通常的做法是在合并文件之前对其进行串联

The sourceMapIn option is described as follows: sourceMapIn选项的描述如下:

sourceMapIn sourceMapIn

Type: String Function 类型: String Function

Default: undefined 默认值: undefined

The location of an input source map from an earlier compilation, eg from CoffeeScript. 输入源映射的位置来自早期的编译(例如来自CoffeeScript)。 If a function is provided, the uglify source is passed as the argument and the return value will be used as the sourceMap name. 如果提供了函数,则将uglify源作为参数传递,并将返回值用作sourceMap名称。 This only makes sense when there's one source file. 仅在只有一个源文件时才有意义。

Gruntfile.js Gruntfile.js

Your Gruntfile.js can be configured something like this: 您的Gruntfile.js可以配置如下:

module.exports = function(grunt) {

  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-concat');

  grunt.initConfig({
    concat: {
      options: {
        // ...
        sourceMap: true,
        sourceMapName: 'dist/js/bundle.map' // Specify path/name for sourceMap
      },
      my_target: {
        src: ['src/js/a.js', 'src/js/b.js', 'src/js/c.js'],
        dest: 'dist/js/bundle.js',
      },
    },
    uglify: {
      options: {
        // ...
        sourceMap: {
          includeSources: true
        },
        sourceMapIn: 'dist/js/bundle.map', // Specify the same path/name as
                                           // the `sourceMapName` value
                                           // in the `concat` task
      },
      my_target: {
        files: {
          'dist/js/bundle.min.js': ['dist/js/bundle.js']
        }
      }
    }
  });

  // Note we run the `concat` task before the `uglify` task.
  grunt.registerTask('default', ['concat:my_target', 'uglify:my_target']);
};

Notes: 笔记:

  1. The path value specified for the concat.options.sourceMapName and uglify.options.sourceMapIn must be the same, eg dist/js/bundle.map . concat.options.sourceMapNameuglify.options.sourceMapIn指定的路径值必须相同,例如dist/js/bundle.map

  2. The concat task must run before the uglify task. concat任务必须在uglify任务之前运行。

  3. The src and dest paths for both tasks will need to be defined as per your projects requirements. 这两个任务的srcdest路径将需要根据您的项目要求进行定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM