简体   繁体   中英

Grunt import from external JSON

Using grunt-replace (or another alternative) I need to read an external JSON file and use it to match the key and replace with the value.

Example, this works:

replace: {
    dist: {
        options: {
            patterns: [
            {
                json: {
                     "hello": "goodbye",
                }
            }
            ]
        },
        files: [
            {expand: true, flatten: true, src: ['index.html'], dest: 'production/'}
        ]
    }
},

However I need it to do something along the lines of:

assets: '<%= grunt.file.read("temp/assets.json") %>',
replace: {
    dist: {
        options: {
            patterns: [
            {
                json: {
                    include: '<%= assets %>'
                }
            }
            ]
        },
        files: [
            {expand: true, flatten: true, src: ['index.html'], dest: 'production/'}
        ]
    }
},

Use grunt.file.readJSON() (Ref: Grunt docs )

assets: grunt.file.readJSON('temp/assets.json');
replace: {
  dist: {
    options: {
        patterns: [
        {
            json: {
                include: '<%= assets %>'
            }
        }
        ]
    },
    files: [
        {expand: true, flatten: true, src: ['index.html'], dest: 'production/'}
    ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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