javascript - How to minify a JSON text file with grunt? - Stack Overflow

I need to minify a set of JSON files (language files) with Gruntgrunt-contrib-uglify:grunt.initConfig(

I need to minify a set of JSON files (language files) with Grunt/grunt-contrib-uglify:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
        build: {
            src: 'src/main/app/resources/locales/*/*.json',
            dest: 'target/resources/locales/*/*.json'
        }
    }
});

What I'm getting is:

Files: src/main/app/resources/locales/en/messages.json -> target/*/*.json
Minifying with UglifyJS...Reading src/main/app/resources/locales/en/messages.json...OK
>> Uglifying source "src/main/app/resources/locales/en/messages.json" failed.
Warning: Uglification failed. Use --force to continue.

I'm starting to wonder, does uglify work on pure JSON files, or is it intended for JavaScript source files only? The JSON file is surely well formed, it runs in the application in its non-minimized form.

I need to minify a set of JSON files (language files) with Grunt/grunt-contrib-uglify:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
        build: {
            src: 'src/main/app/resources/locales/*/*.json',
            dest: 'target/resources/locales/*/*.json'
        }
    }
});

What I'm getting is:

Files: src/main/app/resources/locales/en/messages.json -> target/*/*.json
Minifying with UglifyJS...Reading src/main/app/resources/locales/en/messages.json...OK
>> Uglifying source "src/main/app/resources/locales/en/messages.json" failed.
Warning: Uglification failed. Use --force to continue.

I'm starting to wonder, does uglify work on pure JSON files, or is it intended for JavaScript source files only? The JSON file is surely well formed, it runs in the application in its non-minimized form.

Share Improve this question asked Sep 30, 2013 at 12:30 MaDaMaDa 10.8k10 gold badges50 silver badges84 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

The grunt-json-minify (as indicated in user2806181's answer) works OK (thanks, by the way), but it only modifies files in place. Inspired by the answer, I've found one that's a bit more advanced: grunt-minjson:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    minjson: {
        build: {
            files: { 
                'target/locales/en/messages.json':
                    'resource/locales/en/messages.json'
            }
        }
    }
});

I guess UglifyJS does not work on pure JSON, because its not valid javascript.

Take a look at this link: https://github./mishoo/UglifyJS2/issues/156

But there are several grunt-plugins you can use, for example: https://npmjs/package/grunt-json-minify

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743656892a4485528.html

相关推荐

  • javascript - How to minify a JSON text file with grunt? - Stack Overflow

    I need to minify a set of JSON files (language files) with Gruntgrunt-contrib-uglify:grunt.initConfig(

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信