首页 > 解决方案 > 如何使用 GruntJs Terser JS Minify 创建 Sourcemap

问题描述

我正在使用 Terser for Gruntjs 来缩小 js 文件。我希望能够创建一个源映射,但我不知道如何将我在选项部分(https://www.npmjs.com/package/terser#source-map-options)中看到的示例转换为我的 gruntfile .js。

这是我正在缩小的部分,我添加了我认为 sourceMap 选项所在的部分:

grunt.initConfig({

    terser: {
        pages: {
            options: {
                mangle: {
                    properties: false
                },
                sourceMap: {
                    // source map options goes here I think but not certain what
                }
            },
            files: [
                {   expand: true, 
                    src: '**/*.js', 
                    dest: 'wwwroot/js', 
                    cwd: 'wwwroot/js',
                    ext: '.min.js'
                }
            ]
        }
    }
});

我在任何地方都找不到 gruntjs 示例,因此任何输入或帮助都会很棒

标签: gruntjs

解决方案


我找到了答案并进行了测试。请注意以下sourceMap条目options

grunt.initConfig({

    terser: {
        pages: {
            options: {
                mangle: {
                    properties: false
                },
                sourceMap: true
            },
            files: [
                {   expand: true, 
                    src: '**/*.js', 
                    dest: 'wwwroot/js', 
                    cwd: 'wwwroot/js',
                    ext: '.min.js'
                }
            ]
        }
    }
});

推荐阅读