javascript - Destination not defined error on grunt replace -


i trying use grunt replace change names of files , add random number file prevent caching of images, css , js files.

so running following code

module.exports = function replace(grunt) {      var randomversion = ((new date()).valueof().tostring()) + (math.floor((math.random() * 1000000) + 1).tostring());    var replace = {          options: {          variables: {            'randomversion': randomversion          },          overwrite: true        },        files: [{          src: './target/dist/index.html',          dest: './target/dist/index.' + randomversion + '.html'        }]    };      console.log(randomversion);      grunt.config.set('replace', replace);  };

but "destination not defined

can shed light on this?

thanks

i not code trying achieve, proceed answer according understanding.

first, trying use grunt-replace library? if so, think incorrect. library replacing values within contents of source files, , not file paths themselves.

it looks me want copy source index.html file file path unique identifier in it. may trying wrap in custom task, doing not necessary.

the following sample gruntfile.js of how achieve task. note solution requires installation of grunt-contrib-clean , grunt-contrib-copy libraries.

module.exports = function (grunt) {      grunt.initconfig({         clean: {             dist: ['./target/dist/index.*.html']         },          copy: {             dist: {                 src: './target/src/index.html',                 dest: './target/dist/index.<%= date.now() %>.html'                    }         }     });      grunt.loadnpmtasks('grunt-contrib-clean');     grunt.loadnpmtasks('grunt-contrib-copy');      grunt.registertask('default', ['clean', 'copy']);  }; 

there few things solution worth pointing out:

  • the index.html file is not in /dist folder because not intended distribution, is, rather, source.
  • before creating new versions of index.*.html, delete existing versions in /dist folder don't have accumulation of each version.
  • instead of using "random" number our versions, use timestamp. helpful because each version have greater number 1 before it.

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)